chg: refactor background jobs tool to receive jobId instead of entity.

pull/7939/head
Luciano Righetti 2021-10-28 10:06:41 +02:00
parent 145b54f401
commit a617c089c2
3 changed files with 58 additions and 46 deletions

View File

@ -169,7 +169,7 @@ class JobsController extends AppController
}
$job = $this->Job->getBackgroundJobsTool()->getJob($id);
$status = $job ? $job->status() : null;
$status = $job ? $job->status() : 'Unknown';
return $this->__jobStatusConverter($status);
}

View File

@ -123,7 +123,7 @@ class BackgroundJobsTool
* @param array $args Arguments passed to the job.
* @param boolean|null $trackStatus Whether to track the status of the job.
* @param array $metadata Related to the job.
* @param Job|null $job Relational database record representing the job.
* @param int|null $jobId Id of the relational database record representing the job.
* @return string Background Job Id.
* @throws InvalidArgumentExceptiony
*/
@ -133,11 +133,11 @@ class BackgroundJobsTool
array $args = [],
$trackStatus = null,
array $metadata = [],
Job $job = null
int $jobId = null
): string {
if ($this->settings['use_resque']) {
return $this->resqueEnqueue($queue, self::CMD_TO_SHELL_DICT[$command], $args, $trackStatus, $job);
return $this->resqueEnqueue($queue, self::CMD_TO_SHELL_DICT[$command], $args, $trackStatus, $jobId);
}
$this->validateQueue($queue);
@ -160,8 +160,9 @@ class BackgroundJobsTool
$this->update($backgroundJob);
if ($job) {
$job->saveField('process_id', $backgroundJob->id());
if ($jobId) {
$job = $this->getJobById($jobId);
$job->save(['process_id' => $backgroundJob->id()]);
}
return $backgroundJob->id();
@ -175,7 +176,7 @@ class BackgroundJobsTool
* @param string $class Class of the job.
* @param array $args Arguments passed to the job.
* @param boolean $trackStatus Whether to track the status of the job.
* @param Job|null $job Relational database record representing the job.
* @param int|null $jobId Id of the relational database record representing the job.
* @return string Job Id.
*/
private function resqueEnqueue(
@ -183,7 +184,7 @@ class BackgroundJobsTool
string $class,
$args = [],
$trackStatus = null,
Job $job = null
int $jobId = null
): string {
CakeLog::notice('[DEPRECATION] CakeResque background jobs engine will be deprecated in future MISP versions, please migrate to the light-weight background jobs processor following this guide: [link].');
@ -195,8 +196,9 @@ class BackgroundJobsTool
$trackStatus
);
if ($job) {
$job->saveField('process_id', $process_id);
if ($jobId) {
$job = $this->getJobById($jobId);
$job->save(['process_id' => $process_id]);
}
return $process_id;
@ -489,4 +491,12 @@ class BackgroundJobsTool
return $redis;
}
private function getJobById(int $jobId): ?Job
{
$job = ClassRegistry::init('Job');
$job->id = $jobId;
return $job;
}
}

View File

@ -3125,6 +3125,7 @@ class Event extends AppModel
return !$banError;
}
if (Configure::read('MISP.background_jobs')) {
/** @var Job $job */
$job = ClassRegistry::init('Job');
$jobId = $job->createJob($user, Job::WORKER_EMAIL, 'publish_alert_email', "Event: $id", 'Sending...');
@ -3140,7 +3141,7 @@ class Event extends AppModel
],
true,
[], // metadata
$job
$jobId
);
return true;
@ -4534,6 +4535,7 @@ class Event extends AppModel
public function publishSightingsRouter($id, array $user, $passAlong = null, array $sightingUuids = [])
{
if (Configure::read('MISP.background_jobs')) {
/** @var Job $job */
$job = ClassRegistry::init('Job');
$message = empty($sightingUuids) ? __('Publishing sightings.') : __('Publishing %s sightings.', count($sightingUuids));
$jobId = $job->createJob($user, Job::WORKER_PRIO, 'publish_event', "Event ID: $id", $message);
@ -4550,7 +4552,7 @@ class Event extends AppModel
$args,
true,
[], // metadata
$job
$jobId
);
}
@ -4561,8 +4563,9 @@ class Event extends AppModel
{
if (Configure::read('MISP.background_jobs')) {
/** @var Job $job */
$job = ClassRegistry::init('Job');
$job->createJob($user, Job::WORKER_PRIO, 'publish_event', "Event ID: $id", 'Publishing.');
$jobId = $job->createJob($user, Job::WORKER_PRIO, 'publish_event', "Event ID: $id", 'Publishing.');
return $this->getBackgroundJobsTool()->enqueue(
BackgroundJobsTool::PRIO_QUEUE,
@ -4571,12 +4574,12 @@ class Event extends AppModel
'publish',
$id,
$passAlong,
$job->id,
$jobId,
$user['id']
],
true,
[], // metadata
$job
$jobId
);
}
@ -4687,18 +4690,15 @@ class Event extends AppModel
public function sendContactEmailRouter($id, $message, $creator_only, $user)
{
if (Configure::read('MISP.background_jobs')) {
/** @var Job $job */
$job = ClassRegistry::init('Job');
$job->create();
$data = array(
'worker' => 'email',
'job_type' => 'contact_alert',
'job_input' => 'Owner ' . ($creator_only ? 'user' : 'org') . ' of event #' . $id,
'status' => 0,
'retries' => 0,
'org_id' => $user['org_id'],
'message' => 'Contacting.',
$jobId = $job->createJob(
$user,
Job::WORKER_EMAIL,
'contact_alert',
'Owner ' . ($creator_only ? 'user' : 'org') . ' of event #' . $id,
'Contacting.'
);
$job->save($data);
return $this->getBackgroundJobsTool()->enqueue(
BackgroundJobsTool::EMAIL_QUEUE,
@ -4709,11 +4709,11 @@ class Event extends AppModel
$message,
$creator_only,
$user['id'],
$job->id
$jobId
],
true,
[], // metadata
$job
$jobId
);
return true;
@ -6000,20 +6000,16 @@ class Event extends AppModel
public function enrichmentRouter($options)
{
if (Configure::read('MISP.background_jobs')) {
/** @var Job $job */
$job = ClassRegistry::init('Job');
$job->create();
$data = array(
'worker' => 'prio',
'job_type' => 'enrichment',
'job_input' => 'Event ID: ' . $options['event_id'] . ' modules: ' . json_encode($options['modules']),
'status' => 0,
'retries' => 0,
'org_id' => $options['user']['org_id'],
'org' => $options['user']['Organisation']['name'],
'message' => 'Enriching event.',
$jobId = $job->createJob(
$options['user'],
Job::WORKER_PRIO,
'enrichment',
'Event ID: ' . $options['event_id'] . ' modules: ' . json_encode($options['modules']),
'Enriching event.'
);
$job->save($data);
$jobId = $job->id;
$this->getBackgroundJobsTool()->enqueue(
BackgroundJobsTool::PRIO_QUEUE,
@ -6027,7 +6023,7 @@ class Event extends AppModel
],
true,
[], // metadata
$job
$jobId
);
return true;
@ -6791,7 +6787,13 @@ class Event extends AppModel
if (Configure::read('MISP.background_jobs') && count($attributes) > 5) { // on background process just big attributes batch
/** @var Job $job */
$job = ClassRegistry::init('Job');
$job->createJob($user, Job::WORKER_PRIO, "process_freetext_data", 'Event: ' . $id, 'Processing...');
$jobId = $job->createJob(
$user,
Job::WORKER_PRIO,
"process_freetext_data",
'Event: ' . $id,
'Processing...'
);
$tempData = array(
'user' => $user,
@ -6800,7 +6802,7 @@ class Event extends AppModel
'default_comment' => $default_comment,
'proposals' => $proposals,
'adhereToWarninglists' => $adhereToWarninglists,
'jobId' => $job->id,
'jobId' => $jobId,
);
try {
@ -6815,7 +6817,7 @@ class Event extends AppModel
],
true,
[], // metadata
$job
$jobId
);
return 'Freetext ingestion queued for background processing. Attributes will be added to the event as they are being processed.';
@ -6831,14 +6833,14 @@ class Event extends AppModel
if (Configure::read('MISP.background_jobs')) {
/** @var Job $job */
$job = ClassRegistry::init('Job');
$job->createJob($user, Job::WORKER_PRIO, "process_module_results_data", 'Event: ' . $id, 'Processing...');
$jobId = $job->createJob($user, Job::WORKER_PRIO, "process_module_results_data", 'Event: ' . $id, 'Processing...');
$tempData = array(
'user' => $user,
'misp_format' => $resolved_data,
'id' => $id,
'default_comment' => $default_comment,
'jobId' => $job->id
'jobId' => $jobId
);
try {
@ -6853,7 +6855,7 @@ class Event extends AppModel
],
true,
[], // metadata
$job
$jobId
);
return 'Module results ingestion queued for background processing. Related data will be added to the event as it is being processed.';