Optimize newsletter reactivation

When reactivating a newsletter with many queues (e.g., a welcome email with a long history)
NewsletterEntity::getQueues may fail because it attempts to fetch all queues and instantiate
many entity objects.

In this commit, I optimize this by looking only into unfinished queues.
getUnfinishedQueues look for where countToProcess is higher than 0.
We set the count for welcome emails when we create a new queue
see f1ffc77bf3/mailpoet/lib/Newsletter/Scheduler/WelcomeScheduler.php (L140)
[MAILPOET-6310]
This commit is contained in:
Rostislav Wolny
2024-11-07 14:59:15 +01:00
committed by Aschepikov
parent 1c3ea9cd0a
commit 3fbff8c83f
3 changed files with 4 additions and 3 deletions

View File

@@ -215,7 +215,7 @@ class Newsletters extends APIEndpoint {
// if there are paused tasks unpause them // if there are paused tasks unpause them
if ($newsletter->getStatus() === NewsletterEntity::STATUS_ACTIVE) { if ($newsletter->getStatus() === NewsletterEntity::STATUS_ACTIVE) {
$queues = $newsletter->getQueues(); $queues = $newsletter->getUnfinishedQueues();
foreach ($queues as $queue) { foreach ($queues as $queue) {
$task = $queue->getTask(); $task = $queue->getTask();
if ($task && $task->getStatus() === ScheduledTaskEntity::STATUS_PAUSED) { if ($task && $task->getStatus() === ScheduledTaskEntity::STATUS_PAUSED) {

View File

@@ -537,7 +537,7 @@ class NewsletterEntity {
/** /**
* @return Collection<int, SendingQueueEntity> * @return Collection<int, SendingQueueEntity>
*/ */
private function getUnfinishedQueues(): Collection { public function getUnfinishedQueues(): Collection {
$criteria = new Criteria(); $criteria = new Criteria();
$expr = Criteria::expr(); $expr = Criteria::expr();
$criteria->where($expr->neq('countToProcess', 0)); $criteria->where($expr->neq('countToProcess', 0));

View File

@@ -290,7 +290,8 @@ class NewslettersTest extends \MailPoetTest {
SendingQueueWorker::TASK_TYPE, SendingQueueWorker::TASK_TYPE,
ScheduledTaskEntity::STATUS_PAUSED, ScheduledTaskEntity::STATUS_PAUSED,
); );
(new SendingQueueFactory())->create($scheduledTask1, $this->postNotification); $queue = (new SendingQueueFactory())->create($scheduledTask1, $this->postNotification);
$queue->setCountToProcess(1);
$segment = $this->segmentRepository->createOrUpdate('Segment 1'); $segment = $this->segmentRepository->createOrUpdate('Segment 1');
$this->createNewsletterSegment($this->postNotification, $segment); $this->createNewsletterSegment($this->postNotification, $segment);