new: [internal] Method Job::createJob

pull/7779/head
Jakub Onderka 2021-09-27 23:18:59 +02:00
parent 9f6905be3e
commit ac4f042868
4 changed files with 50 additions and 87 deletions

View File

@ -99,18 +99,7 @@ class ServerShell extends AppShell
if (!empty($this->args[3])) {
$jobId = $this->args[3];
} else {
$this->Job->create();
$data = array(
'worker' => 'default',
'job_type' => 'pull',
'job_input' => 'Server: ' . $serverId,
'status' => 0,
'retries' => 0,
'org' => $user['Organisation']['name'],
'message' => 'Pulling.',
);
$this->Job->save($data);
$jobId = $this->Job->id;
$jobId = $this->Job->createJob($user,Job::WORKER_DEFAULT, 'pull', 'Server: ' . $serverId, 'Pulling.');
}
$force = false;
if (!empty($this->args[4]) && $this->args[4] === 'force') {
@ -339,20 +328,8 @@ class ServerShell extends AppShell
if (!empty($this->args[2])) {
$jobId = $this->args[2];
} else {
$this->Job->create();
$data = array(
'worker' => 'default',
'job_type' => 'cache_feeds',
'job_input' => 'Feed: ' . $scope,
'status' => 0,
'retries' => 0,
'org' => $user['Organisation']['name'],
'message' => 'Starting feed caching.',
);
$this->Job->save($data);
$jobId = $this->Job->id;
$jobId = $this->Job->createJob($user,Job::WORKER_DEFAULT, 'cache_feeds', 'Feed: ' . $scope, 'Starting feed caching.');
}
$this->Job->read(null, $jobId);
try {
$result = $this->Feed->cacheFeedInitiator($user, $jobId, $scope);
} catch (Exception $e) {

View File

@ -295,21 +295,11 @@ class AttachmentScan extends AppModel
if ($canScan) {
$job = ClassRegistry::init('Job');
$job->create();
$job->save(array(
'worker' => 'default',
'job_type' => 'virus_scan',
'job_input' => ($type === self::TYPE_ATTRIBUTE ? 'Attribute: ' : 'Shadow attribute: ') . $attribute['id'],
'status' => 0,
'retries' => 0,
'org' => 'SYSTEM',
'message' => 'Scanning...',
));
$jobId = $job->createJob('SYSTEM', Job::WORKER_DEFAULT, 'virus_scan', ($type === self::TYPE_ATTRIBUTE ? 'Attribute: ' : 'Shadow attribute: ') . $attribute['id'], 'Scanning...');
$processId = CakeResque::enqueue(
'default',
Job::WORKER_DEFAULT,
'AdminShell',
array('scanAttachment', $type, $attribute['id'], $job->id),
array('scanAttachment', $type, $attribute['id'], $jobId),
true
);
$job->saveField('process_id', $processId);

View File

@ -3155,24 +3155,12 @@ class Event extends AppModel
}
if (Configure::read('MISP.background_jobs')) {
$job = ClassRegistry::init('Job');
$job->create();
$data = array(
'worker' => 'email',
'job_type' => 'publish_alert_email',
'job_input' => 'Event: ' . $id,
'status' => 0,
'retries' => 0,
'org_id' => $user['org_id'],
'org' => $user['Organisation']['name'],
'message' => 'Sending...',
);
$job->save($data);
$jobId = $job->id;
$jobId = $job->createJob($user, Job::WORKER_EMAIL, 'publish_alert_email', "Event: $id", 'Sending...');
$process_id = CakeResque::enqueue(
'email',
'EventShell',
array('alertemail', $user['id'], $jobId, $id, $oldpublish),
true
Job::WORKER_EMAIL,
'EventShell',
array('alertemail', $user['id'], $jobId, $id, $oldpublish),
true
);
$job->saveField('process_id', $process_id);
return true;
@ -4540,17 +4528,9 @@ class Event extends AppModel
if (Configure::read('MISP.background_jobs')) {
$job = ClassRegistry::init('Job');
$message = empty($sightingUuids) ? __('Publishing sightings.') : __('Publishing %s sightings.', count($sightingUuids));
$job->create();
$job->save([
'worker' => 'prio',
'job_type' => 'publish_event',
'job_input' => 'Event ID: ' . $id,
'org_id' => $user['org_id'],
'org' => $user['Organisation']['name'],
'message' => $message,
]);
$jobId = $job->createJob($user, Job::WORKER_PRIO, 'publish_event', "Event ID: $id", $message);
$command = ['publish_sightings', $id, $passAlong, $job->id, $user['id']];
$command = ['publish_sightings', $id, $passAlong, $jobId, $user['id']];
if (!empty($sightingUuids)) {
$randomFileName = $this->generateRandomFileName() . '.json';
App::uses('File', 'Utility');
@ -4562,7 +4542,7 @@ class Event extends AppModel
$command[] = $randomFileName;
}
$processId = CakeResque::enqueue('prio', 'EventShell', $command, true);
$processId = CakeResque::enqueue(Job::WORKER_PRIO, 'EventShell', $command, true);
$job->saveField('process_id', $processId);
return $processId;
}
@ -4574,31 +4554,17 @@ class Event extends AppModel
{
if (Configure::read('MISP.background_jobs')) {
$job = ClassRegistry::init('Job');
$job->create();
$data = array(
'worker' => 'prio',
'job_type' => 'publish_event',
'job_input' => 'Event ID: ' . $id,
'status' => 0,
'retries' => 0,
'org_id' => $user['org_id'],
'org' => $user['Organisation']['name'],
'message' => 'Publishing.'
);
$job->save($data);
$jobId = $job->id;
$jobId = $job->createJob($user, Job::WORKER_PRIO, 'publish_event', "Event ID: $id", 'Publishing.');
$process_id = CakeResque::enqueue(
'prio',
'EventShell',
array('publish', $id, $passAlong, $jobId, $user['id']),
true
Job::WORKER_PRIO,
'EventShell',
array('publish', $id, $passAlong, $jobId, $user['id']),
true
);
$job->saveField('process_id', $process_id);
return $process_id;
} else {
$result = $this->publish($id, $passAlong);
return $result;
}
return $this->publish($id, $passAlong);
}
public function publish_sightings($id, $passAlong = null, array $sightingsUuidsToPush = [])

View File

@ -8,6 +8,10 @@ class Job extends AppModel
STATUS_FAILED = 3,
STATUS_COMPLETED = 4;
const WORKER_EMAIL = 'email',
WORKER_PRIO = 'prio',
WORKER_DEFAULT = 'default';
public $belongsTo = array(
'Org' => array(
'className' => 'Organisation',
@ -19,7 +23,6 @@ class Job extends AppModel
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;
@ -67,6 +70,33 @@ class Job extends AppModel
return $id;
}
/**
* @param array|string $user
* @param string $worker
* @param string $jobType
* @param string$jobInput
* @param string $message
* @return int Job ID
* @throws Exception
*/
public function createJob($user, $worker, $jobType, $jobInput, $message = '')
{
$job = [
'worker' => $worker,
'status' => 0,
'retries' => 0,
'org_id' => $user === 'SYSTEM' ? 0 : $user['org_id'],
'job_type' => $jobType,
'job_input' => $jobInput,
'message' => $message,
];
$this->create();
if (!$this->save($job, ['atomic' => false])) { // no need to start transaction for single insert
throw new Exception("Could not save job.");
}
return (int)$this->id;
}
/**
* @param int|null $jobId
* @param string|null $message