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
BounceTaskSubscribersCleanup.php
80 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Cron\Workers; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Entities\ScheduledTaskEntity; |
| 9 | use MailPoet\Newsletter\Sending\ScheduledTaskSubscribersRepository; |
| 10 | use MailPoetVendor\Carbon\Carbon; |
| 11 | |
| 12 | /** |
| 13 | * Purges leftover rows from scheduled_task_subscribers for completed bounce tasks. |
| 14 | * |
| 15 | * Bounce tasks are recurring and no longer need their subscriber rows once |
| 16 | * completed, so unlike SendingTaskSubscribersCleanup there is no retention |
| 17 | * period — every completed, non-deleted bounce task that still has rows is |
| 18 | * eligible. |
| 19 | * |
| 20 | * Runs once an hour at a random minute past the hour (to spread DB load |
| 21 | * across sites). Each run loops in batches: first selects up to |
| 22 | * TASK_BATCH_SIZE completed bounce tasks that still have subscriber rows, then |
| 23 | * deletes up to ROW_BATCH_SIZE rows per iteration. The loop continues until |
| 24 | * fewer rows are deleted than the limit or MAX_EXECUTION_TIME is exceeded. A |
| 25 | * 100ms pause between iterations throttles I/O on shared hosting. |
| 26 | * |
| 27 | * This is kept separate from the bounce task itself, which is expected to be |
| 28 | * replaced by a more efficient one. Once that lands, the cleanup can stop as |
| 29 | * soon as no bounce rows are found — which the batch loop already does. |
| 30 | */ |
| 31 | class BounceTaskSubscribersCleanup extends SimpleWorker { |
| 32 | const TASK_TYPE = 'bounce_task_subscribers_cleanup'; |
| 33 | const TASK_BATCH_SIZE = 200; |
| 34 | const ROW_BATCH_SIZE = 10000; |
| 35 | const MAX_EXECUTION_TIME = 10; |
| 36 | const SUPPORT_MULTIPLE_INSTANCES = false; |
| 37 | |
| 38 | /** @var ScheduledTaskSubscribersRepository */ |
| 39 | private $scheduledTaskSubscribersRepository; |
| 40 | |
| 41 | public function __construct( |
| 42 | ScheduledTaskSubscribersRepository $scheduledTaskSubscribersRepository |
| 43 | ) { |
| 44 | $this->scheduledTaskSubscribersRepository = $scheduledTaskSubscribersRepository; |
| 45 | parent::__construct(); |
| 46 | } |
| 47 | |
| 48 | public function processTaskStrategy(ScheduledTaskEntity $task, $timer) { |
| 49 | $startTime = microtime(true); |
| 50 | |
| 51 | do { |
| 52 | $this->cronHelper->enforceExecutionLimit($timer); |
| 53 | |
| 54 | $deleted = $this->scheduledTaskSubscribersRepository->purgeCompletedBounceTaskSubscribers( |
| 55 | self::TASK_BATCH_SIZE, |
| 56 | self::ROW_BATCH_SIZE |
| 57 | ); |
| 58 | |
| 59 | if ( |
| 60 | $deleted === 0 || |
| 61 | (microtime(true) - $startTime) > self::MAX_EXECUTION_TIME |
| 62 | ) { |
| 63 | break; |
| 64 | } |
| 65 | |
| 66 | usleep(100000); |
| 67 | } while (true); |
| 68 | |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | public function getNextRunDate() { |
| 73 | return Carbon::now()->millisecond(0) |
| 74 | ->startOfHour() |
| 75 | ->addHour() |
| 76 | ->addMinutes(mt_rand(0, 59)) |
| 77 | ->addSeconds(mt_rand(0, 59)); |
| 78 | } |
| 79 | } |
| 80 |