From 00bd5e2e7d438a8d84d5a0563abf2e3b9fba2585 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Lys=C3=BD?= Date: Wed, 1 Sep 2021 13:26:28 +0200 Subject: [PATCH] Schedule recalculation only when another is not running or scheduled [MAILPOET-3776] --- .../Workers/SubscribersCountCacheRecalculation.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/Cron/Workers/SubscribersCountCacheRecalculation.php b/lib/Cron/Workers/SubscribersCountCacheRecalculation.php index 95031b41fa..d3e95ecc30 100644 --- a/lib/Cron/Workers/SubscribersCountCacheRecalculation.php +++ b/lib/Cron/Workers/SubscribersCountCacheRecalculation.php @@ -5,6 +5,7 @@ namespace MailPoet\Cron\Workers; use MailPoet\Cache\TransientCache; use MailPoet\Entities\SegmentEntity; use MailPoet\Models\ScheduledTask; +use MailPoet\Newsletter\Sending\ScheduledTasksRepository; use MailPoet\Segments\SegmentsRepository; use MailPoet\Subscribers\SubscribersCountsController; use MailPoet\WP\Functions as WPFunctions; @@ -25,16 +26,21 @@ class SubscribersCountCacheRecalculation extends SimpleWorker { /** @var SubscribersCountsController */ private $subscribersCountsController; + /** @var ScheduledTasksRepository */ + private $scheduledTasksRepository; + public function __construct( TransientCache $transientCache, SegmentsRepository $segmentsRepository, SubscribersCountsController $subscribersCountsController, + ScheduledTasksRepository $scheduledTasksRepository, WPFunctions $wp ) { parent::__construct($wp); $this->transientCache = $transientCache; $this->segmentsRepository = $segmentsRepository; $this->subscribersCountsController = $subscribersCountsController; + $this->scheduledTasksRepository = $scheduledTasksRepository; } public function processTaskStrategy(ScheduledTask $task, $timer) { @@ -73,6 +79,10 @@ class SubscribersCountCacheRecalculation extends SimpleWorker { } public function shouldBeScheduled(): bool { + $scheduledOrRunningTask = $this->scheduledTasksRepository->findScheduledOrRunningTask(self::TASK_TYPE); + if ($scheduledOrRunningTask) { + return false; + } $now = Carbon::now(); $oldestCreatedAt = $this->transientCache->getOldestCreatedAt(TransientCache::SUBSCRIBERS_STATISTICS_COUNT_KEY); return $oldestCreatedAt === null || $now->diffInMinutes($oldestCreatedAt) > self::EXPIRATION_IN_MINUTES;