From 3fbff8c83fe3dab0d55e7d78ed9871100aea2034 Mon Sep 17 00:00:00 2001 From: Rostislav Wolny Date: Thu, 7 Nov 2024 14:59:15 +0100 Subject: [PATCH] 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 https://github.com/mailpoet/mailpoet/blob/f1ffc77bf318af475e324273695e74da07f3c1e9/mailpoet/lib/Newsletter/Scheduler/WelcomeScheduler.php#L140 [MAILPOET-6310] --- mailpoet/lib/API/JSON/v1/Newsletters.php | 2 +- mailpoet/lib/Entities/NewsletterEntity.php | 2 +- mailpoet/tests/integration/API/JSON/v1/NewslettersTest.php | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/mailpoet/lib/API/JSON/v1/Newsletters.php b/mailpoet/lib/API/JSON/v1/Newsletters.php index caa123f46b..5fe7bd29e8 100644 --- a/mailpoet/lib/API/JSON/v1/Newsletters.php +++ b/mailpoet/lib/API/JSON/v1/Newsletters.php @@ -215,7 +215,7 @@ class Newsletters extends APIEndpoint { // if there are paused tasks unpause them if ($newsletter->getStatus() === NewsletterEntity::STATUS_ACTIVE) { - $queues = $newsletter->getQueues(); + $queues = $newsletter->getUnfinishedQueues(); foreach ($queues as $queue) { $task = $queue->getTask(); if ($task && $task->getStatus() === ScheduledTaskEntity::STATUS_PAUSED) { diff --git a/mailpoet/lib/Entities/NewsletterEntity.php b/mailpoet/lib/Entities/NewsletterEntity.php index 92bf8fffe0..c280cf6855 100644 --- a/mailpoet/lib/Entities/NewsletterEntity.php +++ b/mailpoet/lib/Entities/NewsletterEntity.php @@ -537,7 +537,7 @@ class NewsletterEntity { /** * @return Collection */ - private function getUnfinishedQueues(): Collection { + public function getUnfinishedQueues(): Collection { $criteria = new Criteria(); $expr = Criteria::expr(); $criteria->where($expr->neq('countToProcess', 0)); diff --git a/mailpoet/tests/integration/API/JSON/v1/NewslettersTest.php b/mailpoet/tests/integration/API/JSON/v1/NewslettersTest.php index 603f270e9e..098f8e9be5 100644 --- a/mailpoet/tests/integration/API/JSON/v1/NewslettersTest.php +++ b/mailpoet/tests/integration/API/JSON/v1/NewslettersTest.php @@ -290,7 +290,8 @@ class NewslettersTest extends \MailPoetTest { SendingQueueWorker::TASK_TYPE, 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'); $this->createNewsletterSegment($this->postNotification, $segment);