2013-11-06 10:52:18 +01:00
|
|
|
<?php
|
|
|
|
App::uses('AppModel', 'Model');
|
2016-08-25 11:38:37 +02:00
|
|
|
|
2018-07-19 11:48:22 +02:00
|
|
|
class Job extends AppModel
|
|
|
|
{
|
2021-03-11 11:48:36 +01:00
|
|
|
const STATUS_WAITING = 1,
|
|
|
|
STATUS_RUNNING = 2,
|
|
|
|
STATUS_FAILED = 3,
|
|
|
|
STATUS_COMPLETED = 4;
|
2020-06-07 22:25:32 +02:00
|
|
|
|
2018-07-19 11:48:22 +02:00
|
|
|
public $belongsTo = array(
|
2021-03-11 11:48:36 +01:00
|
|
|
'Org' => array(
|
|
|
|
'className' => 'Organisation',
|
|
|
|
'foreignKey' => 'org_id',
|
|
|
|
'order' => array(),
|
|
|
|
'fields' => array('id', 'name', 'uuid')
|
|
|
|
),
|
|
|
|
);
|
2016-06-04 01:08:16 +02:00
|
|
|
|
2018-07-19 11:48:22 +02:00
|
|
|
public function beforeValidate($options = array())
|
|
|
|
{
|
|
|
|
parent::beforeValidate();
|
|
|
|
$date = date('Y-m-d H:i:s');
|
|
|
|
if (empty($this->data['Job']['id'])) {
|
|
|
|
$this->data['Job']['date_created'] = $date;
|
|
|
|
}
|
2020-06-26 17:14:18 +02:00
|
|
|
$this->data['Job']['date_modified'] = $date;
|
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 cache($type, $user)
|
|
|
|
{
|
|
|
|
$extra = null;
|
|
|
|
$extra2 = null;
|
|
|
|
$shell = 'Event';
|
|
|
|
$this->create();
|
|
|
|
$data = array(
|
|
|
|
'worker' => 'cache',
|
|
|
|
'job_type' => 'cache_' . $type,
|
|
|
|
'job_input' => $user['Role']['perm_site_admin'] ? 'All events.' : 'Events visible to: ' . $user['Organisation']['name'],
|
|
|
|
'status' => 0,
|
|
|
|
'retries' => 0,
|
|
|
|
'org_id' => $user['Role']['perm_site_admin'] ? 0 : $user['org_id'],
|
|
|
|
'message' => 'Fetching events.',
|
|
|
|
);
|
2018-11-23 14:11:33 +01:00
|
|
|
$this->save($data);
|
|
|
|
$id = $this->id;
|
|
|
|
$this->Event = ClassRegistry::init('Event');
|
2019-04-15 10:48:00 +02:00
|
|
|
if (in_array($type, array_keys($this->Event->export_types)) && $type !== 'bro') {
|
2018-11-23 14:11:33 +01:00
|
|
|
$process_id = CakeResque::enqueue(
|
|
|
|
'cache',
|
|
|
|
$shell . 'Shell',
|
2019-03-21 09:44:47 +01:00
|
|
|
array('cache', $user['id'], $id, $type),
|
2018-11-23 14:11:33 +01:00
|
|
|
true
|
|
|
|
);
|
|
|
|
} elseif ($type === 'bro') {
|
2018-07-19 11:48:22 +02:00
|
|
|
$type = 'bro';
|
2018-11-23 14:11:33 +01:00
|
|
|
$process_id = CakeResque::enqueue(
|
|
|
|
'cache',
|
|
|
|
$shell . 'Shell',
|
2019-04-15 10:48:00 +02:00
|
|
|
array('cachebro', $user['id'], $id),
|
2018-11-23 14:11:33 +01:00
|
|
|
true
|
|
|
|
);
|
2018-10-05 14:48:41 +02:00
|
|
|
} else {
|
2018-11-23 14:11:33 +01:00
|
|
|
throw new MethodNotAllowedException('Invalid export type.');
|
|
|
|
}
|
2018-07-19 11:48:22 +02:00
|
|
|
$this->saveField('process_id', $process_id);
|
|
|
|
return $id;
|
|
|
|
}
|
2020-06-07 22:25:32 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int|null $jobId
|
|
|
|
* @param string|null $message
|
|
|
|
* @param int|null $progress
|
2020-06-26 17:14:18 +02:00
|
|
|
* @return bool|null
|
2020-06-07 22:25:32 +02:00
|
|
|
*/
|
|
|
|
public function saveProgress($jobId = null, $message = null, $progress = null)
|
|
|
|
{
|
2020-06-26 17:14:18 +02:00
|
|
|
if ($jobId === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$jobData = array(
|
|
|
|
$this->primaryKey => $jobId,
|
|
|
|
);
|
|
|
|
if ($message !== null) {
|
|
|
|
$jobData['message'] = $message;
|
|
|
|
}
|
|
|
|
if ($progress !== null) {
|
|
|
|
$jobData['progress'] = $progress;
|
|
|
|
if ($progress >= 100) {
|
|
|
|
$jobData['status'] = self::STATUS_COMPLETED;
|
2020-06-07 22:25:32 +02:00
|
|
|
}
|
2020-06-26 17:14:18 +02:00
|
|
|
}
|
|
|
|
try {
|
2021-02-24 20:28:37 +01:00
|
|
|
if ($this->save($jobData, ['atomic' => false])) {
|
2020-06-07 22:25:32 +02:00
|
|
|
return true;
|
|
|
|
}
|
2020-06-26 17:14:18 +02:00
|
|
|
$this->log("Could not save progress for job $jobId because of validation errors: " . json_encode($this->validationErrors), LOG_NOTICE);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->logException("Could not save progress for job $jobId", $e, LOG_NOTICE);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int|null $jobId
|
|
|
|
* @param bool $success
|
|
|
|
* @param string|null $message
|
|
|
|
* @return bool|null
|
|
|
|
*/
|
|
|
|
public function saveStatus($jobId = null, $success = true, $message = null)
|
|
|
|
{
|
|
|
|
if ($jobId === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$message) {
|
|
|
|
$message = $success ? __('Job done.') : __('Job failed.');
|
2020-06-07 22:25:32 +02:00
|
|
|
}
|
|
|
|
|
2020-06-26 17:14:18 +02:00
|
|
|
$jobData = array(
|
|
|
|
$this->primaryKey => $jobId,
|
|
|
|
'status' => $success ? self::STATUS_COMPLETED : self::STATUS_FAILED,
|
|
|
|
'message' => $message,
|
|
|
|
'progress' => 100,
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
if ($this->save($jobData)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$this->log("Could not save status for job $jobId because of validation errors: " . json_encode($this->validationErrors), LOG_NOTICE);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->logException("Could not save progress for job $jobId", $e, LOG_NOTICE);
|
|
|
|
}
|
|
|
|
return false;
|
2020-06-07 22:25:32 +02:00
|
|
|
}
|
2016-06-06 10:09:55 +02:00
|
|
|
}
|