Add check if sending queue is in progress

[MAILPOET-3608]
This commit is contained in:
Jan Lysý
2021-05-19 10:18:08 +02:00
committed by Veljko V
parent 74dfdd1e4b
commit 6c54c5900c

View File

@ -94,15 +94,34 @@ class SendingQueue {
$this->enforceSendingAndExecutionLimits($timer);
foreach (self::getRunningQueues() as $queue) {
if (!$queue instanceof SendingTask) continue;
ScheduledTaskModel::touchAllByIds([$queue->taskId]);
$task = $queue->task();
if (!$task instanceof ScheduledTask) continue;
if ($this->isInProgress($task)) continue;
$this->startProgress($task);
try {
ScheduledTaskModel::touchAllByIds([$queue->taskId]);
$this->processSending($queue, $timer);
} catch (\Exception $e) {
$this->stopProgress($task);
throw $e;
}
$this->stopProgress($task);
}
}
private function processSending(SendingTask $queue, int $timer): void {
$this->loggerFactory->getLogger(LoggerFactory::TOPIC_NEWSLETTERS)->addInfo(
'sending queue processing',
['task_id' => $queue->taskId]
);
$newsletter = $this->newsletterTask->getNewsletterFromQueue($queue);
if (!$newsletter) {
continue;
return;
}
// pre-process newsletter (render, replace shortcodes/links, etc.)
$newsletter = $this->newsletterTask->preProcessNewsletter($newsletter, $queue);
@ -112,7 +131,7 @@ class SendingQueue {
['task_id' => $queue->taskId]
);
$queue->delete();
continue;
return;
}
// clone the original object to be used for processing
$_newsletter = (object)$newsletter->asArray();
@ -134,7 +153,7 @@ class SendingQueue {
$queue->status = ScheduledTaskEntity::STATUS_PAUSED;
$queue->save();
$this->wp->setTransient(self::EMAIL_WITH_INVALID_SEGMENT_OPTION, $newsletter->subject);
continue;
return;
}
// get subscribers
@ -206,7 +225,6 @@ class SendingQueue {
$this->enforceSendingAndExecutionLimits($timer);
}
}
}
public function getBatchSize(): int {
return $this->throttlingHandler->getBatchSize();
@ -393,4 +411,27 @@ class SendingQueue {
}
}
}
private function isInProgress(ScheduledTask $task): bool {
if (!empty($task->inProgress)) {
// Do not run multiple instances of the task
return true;
}
return false;
}
private function startProgress(ScheduledTask $task): void {
$task->inProgress = true;
$task->save();
}
private function stopProgress(ScheduledTask $task): void {
$task->inProgress = false;
$task->save();
}
public function getExecutionLimit(): int {
// Convert seconds to minutes
return (int)round($this->cronHelper->getDaemonExecutionLimit() * 2 / 60);
}
}