Add check if sending queue is in progress
[MAILPOET-3608]
This commit is contained in:
@ -94,15 +94,34 @@ class SendingQueue {
|
|||||||
$this->enforceSendingAndExecutionLimits($timer);
|
$this->enforceSendingAndExecutionLimits($timer);
|
||||||
foreach (self::getRunningQueues() as $queue) {
|
foreach (self::getRunningQueues() as $queue) {
|
||||||
if (!$queue instanceof SendingTask) continue;
|
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(
|
$this->loggerFactory->getLogger(LoggerFactory::TOPIC_NEWSLETTERS)->addInfo(
|
||||||
'sending queue processing',
|
'sending queue processing',
|
||||||
['task_id' => $queue->taskId]
|
['task_id' => $queue->taskId]
|
||||||
);
|
);
|
||||||
$newsletter = $this->newsletterTask->getNewsletterFromQueue($queue);
|
$newsletter = $this->newsletterTask->getNewsletterFromQueue($queue);
|
||||||
if (!$newsletter) {
|
if (!$newsletter) {
|
||||||
continue;
|
return;
|
||||||
}
|
}
|
||||||
// pre-process newsletter (render, replace shortcodes/links, etc.)
|
// pre-process newsletter (render, replace shortcodes/links, etc.)
|
||||||
$newsletter = $this->newsletterTask->preProcessNewsletter($newsletter, $queue);
|
$newsletter = $this->newsletterTask->preProcessNewsletter($newsletter, $queue);
|
||||||
@ -112,7 +131,7 @@ class SendingQueue {
|
|||||||
['task_id' => $queue->taskId]
|
['task_id' => $queue->taskId]
|
||||||
);
|
);
|
||||||
$queue->delete();
|
$queue->delete();
|
||||||
continue;
|
return;
|
||||||
}
|
}
|
||||||
// clone the original object to be used for processing
|
// clone the original object to be used for processing
|
||||||
$_newsletter = (object)$newsletter->asArray();
|
$_newsletter = (object)$newsletter->asArray();
|
||||||
@ -134,7 +153,7 @@ class SendingQueue {
|
|||||||
$queue->status = ScheduledTaskEntity::STATUS_PAUSED;
|
$queue->status = ScheduledTaskEntity::STATUS_PAUSED;
|
||||||
$queue->save();
|
$queue->save();
|
||||||
$this->wp->setTransient(self::EMAIL_WITH_INVALID_SEGMENT_OPTION, $newsletter->subject);
|
$this->wp->setTransient(self::EMAIL_WITH_INVALID_SEGMENT_OPTION, $newsletter->subject);
|
||||||
continue;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// get subscribers
|
// get subscribers
|
||||||
@ -206,7 +225,6 @@ class SendingQueue {
|
|||||||
$this->enforceSendingAndExecutionLimits($timer);
|
$this->enforceSendingAndExecutionLimits($timer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public function getBatchSize(): int {
|
public function getBatchSize(): int {
|
||||||
return $this->throttlingHandler->getBatchSize();
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user