chg: use new bg job tool in post model, refactor command

pull/7939/head
Luciano Righetti 2021-11-03 10:58:35 +01:00
parent efd55c362c
commit c3c0c15182
2 changed files with 47 additions and 29 deletions

View File

@ -307,23 +307,35 @@ class EventShell extends AppShell
public function postsemail()
{
$this->ConfigLoad->execute();
if (empty($this->args[0]) || empty($this->args[1]) || empty($this->args[2]) ||
empty($this->args[3]) || empty($this->args[4]) || empty($this->args[5])) {
if (
empty($this->args[0]) || empty($this->args[1]) || empty($this->args[2]) ||
empty($this->args[3]) || empty($this->args[4]) || empty($this->args[5])
) {
die('Usage: ' . $this->Server->command_line_functions['event_management_tasks']['data']['Posts email'] . PHP_EOL);
}
$userId = $this->args[0];
$postId = $this->args[1];
$eventId = $this->args[2];
$userId = intval($this->args[0]);
$postId = intval($this->args[1]);
$eventId = intval($this->args[2]);
$title = $this->args[3];
$message = $this->args[4];
$processId = $this->args[5];
$this->Job->id = $processId;
$this->Job->id = intval($this->args[5]);
$result = $this->Post->sendPostsEmail($userId, $postId, $eventId, $title, $message);
$job['Job']['progress'] = 100;
$job['Job']['message'] = 'Emails sent.';
$job['Job']['date_modified'] = date("Y-m-d H:i:s");
$this->Job->save($job);
if ($result) {
$this->Job->save([
'progress' => 100,
'message' => 'Emails sent.',
'date_modified' => date('Y-m-d H:i:s'),
'status' => Job::STATUS_COMPLETED
]);
} else {
$this->Job->save([
'date_modified' => date('Y-m-d H:i:s'),
'status' => Job::STATUS_FAILED
]);
}
}
public function enqueueCaching()

View File

@ -32,27 +32,33 @@ class Post extends AppModel
{
if (Configure::read('MISP.background_jobs')) {
$user = $this->User->findById($user_id);
/** @var Job $job */
$job = ClassRegistry::init('Job');
$job->create();
$data = array(
'worker' => 'email',
'job_type' => 'posts_alert',
'job_input' => 'Post: ' . $post_id,
'status' => 0,
'retries' => 0,
'org_id' => $user['User']['org_id'],
'org' => $user['Organisation']['name'],
'message' => 'Sending...',
$jobId = $job->createJob(
$user['User'],
Job::WORKER_EMAIL,
'posts_alert',
'Post: ' . $post_id,
'Sending...'
);
$job->save($data);
$jobId = $job->id;
$process_id = CakeResque::enqueue(
'email',
'EventShell',
array('postsemail', $user_id, $post_id, $event_id, $title, $message, $jobId),
true
$this->getBackgroundJobsTool()->enqueue(
BackgroundJobsTool::EMAIL_QUEUE,
BackgroundJobsTool::CMD_EVENT,
[
'postsemail',
$user_id,
$post_id,
$event_id,
$title,
$message,
$jobId
],
true,
$jobId
);
$job->saveField('process_id', $process_id);
return true;
} else {
return $this->sendPostsEmail($user_id, $post_id, $event_id, $title, $message);