Automations
1 year ago
KeyCheck
1 year ago
SendingQueue
6 days ago
StatsNotifications
2 months ago
AuthorizedSendingEmailsCheck.php
3 years ago
BackfillEngagementData.php
1 year ago
Bounce.php
6 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
4 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
SubscribersSegmentsCountSync.php
91 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Cron\Workers; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Doctrine\WPDB\Connection; |
| 9 | use MailPoet\Entities\ScheduledTaskEntity; |
| 10 | use MailPoet\Entities\SubscriberEntity; |
| 11 | use MailPoet\Segments\SegmentSubscribersRepository; |
| 12 | use MailPoet\Subscribers\SegmentsCountRecalculator; |
| 13 | use MailPoetVendor\Doctrine\ORM\EntityManager; |
| 14 | |
| 15 | /** |
| 16 | * Populates and reconciles SubscriberEntity::$segmentsCount. |
| 17 | * |
| 18 | * The first run is the backfill: it sweeps the whole subscribers table by id |
| 19 | * range, recomputes segments_count for every row, and then calls |
| 20 | * SegmentSubscribersRepository::markSegmentsCountColumnReady() so reads start |
| 21 | * trusting the column. |
| 22 | * |
| 23 | * Every subsequent (weekly) run is the reconcile backstop: it re-sweeps the |
| 24 | * table to repair any drift left by a write path that forgot to update the |
| 25 | * column. Both phases use the same idempotent recompute, so the value always |
| 26 | * converges. Work is chunked and bounded by enforceExecutionLimit() so the |
| 27 | * sweep never runs as one long query and never blocks a request. |
| 28 | */ |
| 29 | class SubscribersSegmentsCountSync extends SimpleWorker { |
| 30 | const TASK_TYPE = 'subscribers_segments_count_sync'; |
| 31 | const SUPPORT_MULTIPLE_INSTANCES = false; |
| 32 | |
| 33 | /** @var EntityManager */ |
| 34 | private $entityManager; |
| 35 | |
| 36 | /** @var SegmentsCountRecalculator */ |
| 37 | private $segmentsCountRecalculator; |
| 38 | |
| 39 | /** @var SegmentSubscribersRepository */ |
| 40 | private $segmentSubscribersRepository; |
| 41 | |
| 42 | public function __construct( |
| 43 | EntityManager $entityManager, |
| 44 | SegmentsCountRecalculator $segmentsCountRecalculator, |
| 45 | SegmentSubscribersRepository $segmentSubscribersRepository |
| 46 | ) { |
| 47 | parent::__construct(); |
| 48 | $this->entityManager = $entityManager; |
| 49 | $this->segmentsCountRecalculator = $segmentsCountRecalculator; |
| 50 | $this->segmentSubscribersRepository = $segmentSubscribersRepository; |
| 51 | } |
| 52 | |
| 53 | public function processTaskStrategy(ScheduledTaskEntity $task, $timer): bool { |
| 54 | // The recalculator relies on UPDATE ... LEFT JOIN, which the SQLite |
| 55 | // integration in WordPress Playground does not support. Make the task a |
| 56 | // no-op there and never flip the backfill flag, so reads stay on the |
| 57 | // anti-join fallback instead of trusting an unpopulated column. |
| 58 | if (Connection::isSQLite()) { |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | $meta = $task->getMeta(); |
| 63 | $lastId = isset($meta['last_subscriber_id']) ? (int)$meta['last_subscriber_id'] : 0; |
| 64 | $highestId = $this->getHighestSubscriberId(); |
| 65 | |
| 66 | while ($lastId < $highestId) { |
| 67 | $this->segmentsCountRecalculator->recalculateForIdRange($lastId + 1, $lastId + SegmentsCountRecalculator::BATCH_SIZE); |
| 68 | $lastId += SegmentsCountRecalculator::BATCH_SIZE; |
| 69 | $task->setMeta(['last_subscriber_id' => $lastId]); |
| 70 | $this->scheduledTasksRepository->persist($task); |
| 71 | $this->scheduledTasksRepository->flush(); |
| 72 | $this->cronHelper->enforceExecutionLimit($timer); // throws and reschedules when over the limit |
| 73 | } |
| 74 | |
| 75 | // The whole table has been recomputed: reads can trust segments_count now. |
| 76 | // The cursor is intentionally left in place: each weekly reconcile run is a |
| 77 | // fresh task with empty meta, so it sweeps from id 0 again on its own. And if |
| 78 | // markSegmentsCountColumnReady() throws, the retry just re-runs the flag flip |
| 79 | // (the while loop is already exhausted) instead of re-sweeping the table. |
| 80 | $this->segmentSubscribersRepository->markSegmentsCountColumnReady(); |
| 81 | |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | private function getHighestSubscriberId(): int { |
| 86 | $subscribersTable = $this->entityManager->getClassMetadata(SubscriberEntity::class)->getTableName(); |
| 87 | $result = $this->entityManager->getConnection()->executeQuery("SELECT MAX(id) FROM $subscribersTable LIMIT 1;")->fetchNumeric(); |
| 88 | return is_array($result) && isset($result[0]) && is_numeric($result[0]) ? (int)$result[0] : 0; |
| 89 | } |
| 90 | } |
| 91 |