chg: move metadata parameter to last, refactor Server calls to background jobs to new tool

pull/7939/head
Luciano Righetti 2021-10-29 17:25:33 +02:00
parent 8590c9f17e
commit c16c2784d2
4 changed files with 54 additions and 50 deletions

View File

@ -5790,7 +5790,6 @@ class EventsController extends AppController
$id
],
true,
[], // metadata
$job
);

View File

@ -70,16 +70,19 @@ class BackgroundJobsTool
public const
CMD_EVENT = 'event',
CMD_SERVER = 'server';
CMD_SERVER = 'server',
CMD_ADMIN = 'admin';
public const ALLOWED_COMMANDS = [
self::CMD_EVENT,
self::CMD_SERVER
self::CMD_SERVER,
self::CMD_ADMIN
];
public const CMD_TO_SHELL_DICT = [
self::CMD_EVENT => 'EventShell',
self::CMD_SERVER => 'ServerShell'
self::CMD_SERVER => 'ServerShell',
self::CMD_ADMIN => 'AdminShell'
];
public const JOB_STATUS_PREFIX = 'job_status';
@ -122,9 +125,9 @@ class BackgroundJobsTool
* @param string $command Command of the job.
* @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 int|null $jobId Id of the relational database record representing the job.
* @return string Background Job Id.
* @param array $metadata Related to the job.
* @throws InvalidArgumentExceptiony
*/
public function enqueue(
@ -132,8 +135,8 @@ class BackgroundJobsTool
string $command,
array $args = [],
$trackStatus = null,
array $metadata = [],
int $jobId = null
int $jobId = null,
array $metadata = []
): string {
if ($this->settings['use_resque']) {
@ -338,6 +341,10 @@ class BackgroundJobsTool
{
$this->validateQueue($queue);
if ($this->settings['use_resque']) {
return CakeResque::getQueueSize($queue);
}
return $this->RedisConnection->llen($queue);
}

View File

@ -3140,7 +3140,6 @@ class Event extends AppModel
$oldpublish
],
true,
[], // metadata
$jobId
);
@ -4551,7 +4550,6 @@ class Event extends AppModel
BackgroundJobsTool::CMD_EVENT,
$args,
true,
[], // metadata
$jobId
);
}
@ -4578,7 +4576,6 @@ class Event extends AppModel
$user['id']
],
true,
[], // metadata
$jobId
);
}
@ -4712,7 +4709,6 @@ class Event extends AppModel
$jobId
],
true,
[], // metadata
$jobId
);
@ -6022,7 +6018,6 @@ class Event extends AppModel
$jobId
],
true,
[], // metadata
$jobId
);
@ -6816,7 +6811,6 @@ class Event extends AppModel
$filePath
],
true,
[], // metadata
$jobId
);
@ -6854,7 +6848,6 @@ class Event extends AppModel
$filePath
],
true,
[], // metadata
$jobId
);

View File

@ -2007,26 +2007,28 @@ class Server extends AppModel
$jobType = 'jobGenerateCorrelation';
$jobTypeText = 'generate correlation';
}
/** @var Job $job */
$job = ClassRegistry::init('Job');
$job->create();
$data = array(
'worker' => 'default',
'job_type' => $jobTypeText,
'job_input' => 'All attributes',
'status' => 0,
'retries' => 0,
'org' => 'ADMIN',
'message' => 'Job created.',
$jobId = $job->createJob(
'SYSTEM',
Job::WORKER_PRIO,
$jobTypeText,
'All attributes',
'Job created.'
);
$job->save($data);
$jobId = $job->id;
$process_id = CakeResque::enqueue(
'default',
'AdminShell',
array($jobType, $jobId),
true
$this->getBackgroundJobsTool()->enqueue(
BackgroundJobsTool::DEFAULT_QUEUE,
BackgroundJobsTool::CMD_ADMIN,
[
'updateAfterPull',
$jobType,
$jobId
],
true,
$jobId
);
$job->saveField('process_id', $process_id);
}
return true;
}
@ -3417,7 +3419,7 @@ class Server extends AppModel
}
}
if ($k != 'scheduler') {
$worker_array[$k]['jobCount'] = CakeResque::getQueueSize($k);
$worker_array[$k]['jobCount'] = $this->getBackgroundJobsTool()->getQueueSize($k);
}
if (!isset($queue['workers'])) {
$workerIssueCount++;
@ -3896,27 +3898,30 @@ class Server extends AppModel
public function updateDatabaseAfterPullRouter($submodule_name, $user) {
if (Configure::read('MISP.background_jobs')) {
/** @var Job $job */
$job = ClassRegistry::init('Job');
$job->create();
$data = array(
'worker' => 'prio',
'job_type' => __('update_after_pull'),
'job_input' => __('Updating: ' . $submodule_name),
'status' => 0,
'retries' => 0,
'org_id' => $user['org_id'],
'org' => $user['Organisation']['name'],
'message' => 'Update the database after PULLing the submodule(s).',
$jobId = $job->createJob(
$user,
Job::WORKER_PRIO,
'update_after_pull',
__('Updating: ' . $submodule_name),
'Update the database after PULLing the submodule(s).'
);
$job->save($data);
$jobId = $job->id;
$process_id = CakeResque::enqueue(
'prio',
'AdminShell',
array('updateAfterPull', $submodule_name, $jobId, $user['id']),
true
$this->getBackgroundJobsTool()->enqueue(
BackgroundJobsTool::PRIO_QUEUE,
BackgroundJobsTool::CMD_ADMIN,
[
'updateAfterPull',
$submodule_name,
$jobId,
$user['id']
],
true,
$jobId
);
$job->saveField('process_id', $process_id);
return array('job_sent' => true, 'sync_result' => __('unknown'));
} else {
$result = $this->updateAfterPull($submodule_name, $user['id']);