Automations
1 year ago
KeyCheck
1 year ago
SendingQueue
5 days ago
StatsNotifications
2 months ago
AuthorizedSendingEmailsCheck.php
3 years ago
BackfillEngagementData.php
1 year ago
Bounce.php
5 days ago
BounceTaskSubscribersCleanup.php
1 month ago
BulkConfirmationEmailResend.php
2 months ago
ExportFilesCleanup.php
2 months ago
InactiveSubscribersMaintenance.php
2 weeks ago
LogCleanup.php
10 months ago
Mixpanel.php
9 months ago
NewsletterTemplateThumbnails.php
1 year ago
ReEngagementEmailsScheduler.php
1 year ago
Scheduler.php
2 months ago
SendingQueueBodyCleanup.php
2 months ago
SendingTaskSubscribersCleanup.php
2 months ago
SimpleWorker.php
1 year ago
StatisticsExport.php
2 months ago
SubscriberLimitNotificationWorker.php
2 months ago
SubscriberLinkTokens.php
1 year ago
SubscribersCountCacheRecalculation.php
2 weeks ago
SubscribersEngagementScore.php
3 days ago
SubscribersLastEngagement.php
2 months ago
SubscribersSegmentsCountSync.php
2 weeks ago
SubscribersStatsReport.php
1 year ago
Tracks.php
9 months ago
UnconfirmedSubscribersCleanup.php
2 months ago
UnsubscribeTokens.php
1 month ago
WooCommercePastOrders.php
1 year ago
WooCommerceSync.php
3 years ago
WorkersFactory.php
2 weeks ago
index.php
3 years ago
SubscribersCountCacheRecalculation.php
107 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Cron\Workers; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Cache\TransientCache; |
| 9 | use MailPoet\Entities\ScheduledTaskEntity; |
| 10 | use MailPoet\Entities\SegmentEntity; |
| 11 | use MailPoet\Segments\SegmentsRepository; |
| 12 | use MailPoet\Subscribers\SubscribersCountsController; |
| 13 | use MailPoetVendor\Carbon\Carbon; |
| 14 | |
| 15 | class SubscribersCountCacheRecalculation extends SimpleWorker { |
| 16 | private const EXPIRATION_IN_MINUTES = 30; |
| 17 | const TASK_TYPE = 'subscribers_count_cache_recalculation'; |
| 18 | const AUTOMATIC_SCHEDULING = false; |
| 19 | const SUPPORT_MULTIPLE_INSTANCES = false; |
| 20 | |
| 21 | /** @var TransientCache */ |
| 22 | private $transientCache; |
| 23 | |
| 24 | /** @var SegmentsRepository */ |
| 25 | private $segmentsRepository; |
| 26 | |
| 27 | /** @var SubscribersCountsController */ |
| 28 | private $subscribersCountsController; |
| 29 | |
| 30 | public function __construct( |
| 31 | TransientCache $transientCache, |
| 32 | SegmentsRepository $segmentsRepository, |
| 33 | SubscribersCountsController $subscribersCountsController |
| 34 | ) { |
| 35 | parent::__construct(); |
| 36 | $this->transientCache = $transientCache; |
| 37 | $this->segmentsRepository = $segmentsRepository; |
| 38 | $this->subscribersCountsController = $subscribersCountsController; |
| 39 | } |
| 40 | |
| 41 | public function processTaskStrategy(ScheduledTaskEntity $task, $timer) { |
| 42 | $segments = $this->segmentsRepository->findAll(); |
| 43 | foreach ($segments as $segment) { |
| 44 | $this->recalculateSegmentCache($timer, (int)$segment->getId(), $segment); |
| 45 | } |
| 46 | |
| 47 | // update cache for subscribers without segment |
| 48 | $this->recalculateSegmentCache($timer, 0); |
| 49 | |
| 50 | $this->recalculateGlobalCache($timer); |
| 51 | |
| 52 | $this->recalculateHomepageCache($timer); |
| 53 | |
| 54 | // remove redundancies from cache |
| 55 | $this->cronHelper->enforceExecutionLimit($timer); |
| 56 | $this->subscribersCountsController->removeRedundancyFromStatisticsCache(); |
| 57 | |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | private function recalculateSegmentCache($timer, int $segmentId, ?SegmentEntity $segment = null): void { |
| 62 | $this->cronHelper->enforceExecutionLimit($timer); |
| 63 | $now = Carbon::now(); |
| 64 | $item = $this->transientCache->getItem(TransientCache::SUBSCRIBERS_STATISTICS_COUNT_KEY, $segmentId); |
| 65 | if ($item === null || !isset($item['created_at']) || $now->diffInMinutes($item['created_at']) > self::EXPIRATION_IN_MINUTES) { |
| 66 | if ($segment) { |
| 67 | $this->subscribersCountsController->recalculateSegmentStatisticsCache($segment); |
| 68 | } else { |
| 69 | $this->subscribersCountsController->recalculateSubscribersWithoutSegmentStatisticsCache(); |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | private function recalculateGlobalCache($timer): void { |
| 75 | $this->cronHelper->enforceExecutionLimit($timer); |
| 76 | $now = Carbon::now(); |
| 77 | $item = $this->transientCache->getItem(TransientCache::SUBSCRIBERS_GLOBAL_STATISTICS_COUNT_KEY, 0); |
| 78 | if ($item === null || !isset($item['created_at']) || $now->diffInMinutes($item['created_at']) > self::EXPIRATION_IN_MINUTES) { |
| 79 | $this->subscribersCountsController->recalculateGlobalStatusStatisticsCache(); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | private function recalculateHomepageCache($timer): void { |
| 84 | $this->cronHelper->enforceExecutionLimit($timer); |
| 85 | $now = Carbon::now(); |
| 86 | $item = $this->transientCache->getItem(TransientCache::SUBSCRIBERS_HOMEPAGE_STATISTICS_COUNT_KEY, 0); |
| 87 | if ($item === null || !isset($item['created_at']) || $now->diffInMinutes($item['created_at']) > self::EXPIRATION_IN_MINUTES) { |
| 88 | $this->cronHelper->enforceExecutionLimit($timer); |
| 89 | $this->subscribersCountsController->recalculateHomepageStatisticsCache(); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | public function getNextRunDate() { |
| 94 | return Carbon::now()->millisecond(0); |
| 95 | } |
| 96 | |
| 97 | public function shouldBeScheduled(): bool { |
| 98 | $scheduledOrRunningTask = $this->scheduledTasksRepository->findScheduledOrRunningTask(self::TASK_TYPE); |
| 99 | if ($scheduledOrRunningTask) { |
| 100 | return false; |
| 101 | } |
| 102 | $now = Carbon::now(); |
| 103 | $oldestCreatedAt = $this->transientCache->getOldestCreatedAt(TransientCache::SUBSCRIBERS_STATISTICS_COUNT_KEY); |
| 104 | return $oldestCreatedAt === null || $now->diffInMinutes($oldestCreatedAt) > self::EXPIRATION_IN_MINUTES; |
| 105 | } |
| 106 | } |
| 107 |