2013-11-06 10:52:18 +01:00
|
|
|
<?php
|
|
|
|
App::uses('AppController', 'Controller');
|
|
|
|
|
2021-03-14 17:08:14 +01:00
|
|
|
/**
|
|
|
|
* @property Job $Job
|
|
|
|
*/
|
2018-07-19 11:48:22 +02:00
|
|
|
class JobsController extends AppController
|
|
|
|
{
|
|
|
|
public $components = array('Security' ,'RequestHandler', 'Session');
|
2016-06-04 01:08:16 +02:00
|
|
|
|
2018-07-19 11:48:22 +02:00
|
|
|
public $paginate = array(
|
2021-03-14 17:08:14 +01:00
|
|
|
'limit' => 20,
|
2021-03-14 20:28:43 +01:00
|
|
|
'recursive' => 0,
|
2021-03-14 17:08:14 +01:00
|
|
|
'order' => array(
|
|
|
|
'Job.id' => 'desc'
|
|
|
|
),
|
2018-07-19 11:48:22 +02:00
|
|
|
);
|
2016-06-04 01:08:16 +02:00
|
|
|
|
2018-07-19 11:48:22 +02:00
|
|
|
public function index($queue = false)
|
|
|
|
{
|
|
|
|
if (!Configure::read('MISP.background_jobs')) {
|
|
|
|
throw new NotFoundException('Background jobs are not enabled on this instance.');
|
|
|
|
}
|
|
|
|
$this->loadModel('Server');
|
|
|
|
$issueCount = 0;
|
|
|
|
$workers = $this->Server->workerDiagnostics($issueCount);
|
2019-10-01 13:36:37 +02:00
|
|
|
$queues = array('email', 'default', 'cache', 'prio', 'update');
|
2021-03-14 17:08:14 +01:00
|
|
|
if ($queue && in_array($queue, $queues, true)) {
|
2018-07-19 11:48:22 +02:00
|
|
|
$this->paginate['conditions'] = array('Job.worker' => $queue);
|
|
|
|
}
|
|
|
|
$jobs = $this->paginate();
|
|
|
|
foreach ($jobs as &$job) {
|
|
|
|
if ($job['Job']['process_id'] !== false) {
|
|
|
|
$job['Job']['job_status'] = $this->__jobStatusConverter(CakeResque::getJobStatus($job['Job']['process_id']));
|
2021-03-14 17:08:14 +01:00
|
|
|
$job['Job']['failed'] = $job['Job']['job_status'] === 'Failed';
|
2018-07-19 11:48:22 +02:00
|
|
|
} else {
|
2021-03-14 17:08:14 +01:00
|
|
|
$job['Job']['job_status'] = 'Unknown';
|
2018-07-19 11:48:22 +02:00
|
|
|
}
|
2021-03-14 17:08:14 +01:00
|
|
|
$job['Job']['worker_status'] = isset($workers[$job['Job']['worker']]) && $workers[$job['Job']['worker']]['ok'];
|
2018-07-19 11:48:22 +02:00
|
|
|
}
|
|
|
|
$this->set('list', $jobs);
|
|
|
|
$this->set('queue', $queue);
|
|
|
|
}
|
2016-08-22 02:52:51 +02:00
|
|
|
|
2018-07-19 11:48:22 +02:00
|
|
|
public function getError($id)
|
|
|
|
{
|
|
|
|
$fields = array(
|
|
|
|
'Failed at' => 'failed_at',
|
|
|
|
'Exception' => 'exception',
|
|
|
|
'Error' => 'error'
|
|
|
|
);
|
|
|
|
$this->set('fields', $fields);
|
|
|
|
$this->set('response', CakeResque::getFailedJobLog($id));
|
|
|
|
$this->render('/Jobs/ajax/error');
|
|
|
|
}
|
2016-06-04 01:08:16 +02:00
|
|
|
|
2018-07-19 11:48:22 +02:00
|
|
|
private function __jobStatusConverter($status)
|
|
|
|
{
|
|
|
|
switch ($status) {
|
|
|
|
case 1:
|
|
|
|
return 'Waiting';
|
|
|
|
case 2:
|
|
|
|
return 'Running';
|
|
|
|
case 3:
|
|
|
|
return 'Failed';
|
|
|
|
case 4:
|
|
|
|
return 'Completed';
|
|
|
|
default:
|
|
|
|
return 'Unknown';
|
|
|
|
}
|
|
|
|
}
|
2016-06-04 01:08:16 +02:00
|
|
|
|
2018-07-19 11:48:22 +02:00
|
|
|
public function getGenerateCorrelationProgress($id)
|
|
|
|
{
|
2021-03-14 17:08:14 +01:00
|
|
|
$progress = $this->Job->find('first', [
|
|
|
|
'fields' => ['progress'],
|
|
|
|
'conditions' => ['id' => $id],
|
|
|
|
'recursive' => -1,
|
|
|
|
]);
|
2018-07-19 11:48:22 +02:00
|
|
|
if (!$progress) {
|
|
|
|
$progress = 0;
|
|
|
|
} else {
|
2021-03-14 17:08:14 +01:00
|
|
|
$progress = (int)$progress['Job']['progress'];
|
2018-07-19 11:48:22 +02:00
|
|
|
}
|
2021-03-14 17:08:14 +01:00
|
|
|
return $this->RestResponse->viewData($progress, 'json');
|
2018-07-19 11:48:22 +02:00
|
|
|
}
|
2016-06-04 01:08:16 +02:00
|
|
|
|
2018-07-19 11:48:22 +02:00
|
|
|
public function getProgress($type)
|
|
|
|
{
|
|
|
|
$org_id = $this->Auth->user('org_id');
|
|
|
|
if ($this->_isSiteAdmin()) {
|
|
|
|
$org_id = 0;
|
|
|
|
}
|
2016-08-10 13:54:14 +02:00
|
|
|
|
2018-07-19 11:48:22 +02:00
|
|
|
if (is_numeric($type)) {
|
|
|
|
$progress = $this->Job->find('first', array(
|
|
|
|
'conditions' => array(
|
|
|
|
'Job.id' => $type,
|
|
|
|
'org_id' => $org_id
|
|
|
|
),
|
|
|
|
'fields' => array('id', 'progress'),
|
|
|
|
'order' => array('Job.id' => 'desc'),
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
$progress = $this->Job->find('first', array(
|
|
|
|
'conditions' => array(
|
|
|
|
'job_type' => $type,
|
|
|
|
'org_id' => $org_id
|
|
|
|
),
|
|
|
|
'fields' => array('id', 'progress'),
|
|
|
|
'order' => array('Job.id' => 'desc'),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
if (!$progress) {
|
|
|
|
$progress = 0;
|
|
|
|
} else {
|
|
|
|
$progress = $progress['Job']['progress'];
|
|
|
|
}
|
|
|
|
if ($this->_isRest()) {
|
|
|
|
return $this->RestResponse->viewData(array('progress' => $progress . '%'), $this->response->type());
|
|
|
|
} else {
|
|
|
|
return new CakeResponse(array('body' => json_encode($progress), 'type' => 'json'));
|
|
|
|
}
|
|
|
|
}
|
2016-06-04 01:08:16 +02:00
|
|
|
|
2018-07-19 11:48:22 +02:00
|
|
|
public function cache($type)
|
|
|
|
{
|
|
|
|
if (Configure::read('MISP.disable_cached_exports')) {
|
|
|
|
throw new MethodNotAllowedException('This feature is currently disabled');
|
|
|
|
}
|
|
|
|
if ($this->_isSiteAdmin()) {
|
|
|
|
$target = 'All events.';
|
|
|
|
} else {
|
|
|
|
$target = 'Events visible to: '.$this->Auth->user('Organisation')['name'];
|
|
|
|
}
|
|
|
|
$id = $this->Job->cache($type, $this->Auth->user());
|
|
|
|
if ($this->_isRest()) {
|
|
|
|
return $this->RestResponse->viewData(array('job_id' => $id), $this->response->type());
|
|
|
|
} else {
|
|
|
|
return new CakeResponse(array('body' => json_encode($id), 'type' => 'json'));
|
|
|
|
}
|
|
|
|
}
|
2018-03-14 02:28:49 +01:00
|
|
|
|
2018-07-19 11:48:22 +02:00
|
|
|
public function clearJobs($type = 'completed')
|
|
|
|
{
|
|
|
|
if ($this->request->is('post')) {
|
2021-03-14 20:28:43 +01:00
|
|
|
if ($type === 'all') {
|
2018-07-19 11:48:22 +02:00
|
|
|
$conditions = array('Job.id !=' => 0);
|
|
|
|
$message = __('All jobs have been purged');
|
2021-03-14 20:28:43 +01:00
|
|
|
} else {
|
|
|
|
$conditions = array('Job.progress' => 100);
|
|
|
|
$message = __('All completed jobs have been purged');
|
2018-07-19 11:48:22 +02:00
|
|
|
}
|
|
|
|
$this->Job->deleteAll($conditions, false);
|
|
|
|
$this->Flash->success($message);
|
|
|
|
$this->redirect(array('action' => 'index'));
|
|
|
|
}
|
|
|
|
}
|
2013-11-06 10:52:18 +01:00
|
|
|
}
|