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
SendingTaskSubscribersCleanup.php
113 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 MailPoet\Settings\SettingsController; |
| 11 | use MailPoetVendor\Carbon\Carbon; |
| 12 | |
| 13 | /** |
| 14 | * Purges old rows from scheduled_task_subscribers for completed sending tasks. |
| 15 | * |
| 16 | * Runs 4 times per day in random 6-hour slots (0–5h, 6–11h, 12–17h, 18–23h). |
| 17 | * Each run loops in batches: first selects up to TASK_BATCH_SIZE completed tasks |
| 18 | * older than the configured retention period that still have subscriber rows, |
| 19 | * then deletes up to ROW_BATCH_SIZE rows per iteration. The loop continues until |
| 20 | * fewer rows are deleted than the limit or MAX_EXECUTION_TIME is exceeded. |
| 21 | * A 100ms pause between iterations throttles I/O on shared hosting. |
| 22 | * |
| 23 | * Retention is controlled by the 'sending_status_retention_days' setting. |
| 24 | * Empty string means "Never" — the worker skips all deletions. |
| 25 | */ |
| 26 | class SendingTaskSubscribersCleanup extends SimpleWorker { |
| 27 | const TASK_TYPE = 'sending_task_subscribers_cleanup'; |
| 28 | const TASK_BATCH_SIZE = 200; |
| 29 | const ROW_BATCH_SIZE = 10000; |
| 30 | const MAX_EXECUTION_TIME = 30; |
| 31 | const SUPPORT_MULTIPLE_INSTANCES = false; |
| 32 | |
| 33 | /** @var ScheduledTaskSubscribersRepository */ |
| 34 | private $scheduledTaskSubscribersRepository; |
| 35 | |
| 36 | /** @var SettingsController */ |
| 37 | private $settings; |
| 38 | |
| 39 | public function __construct( |
| 40 | ScheduledTaskSubscribersRepository $scheduledTaskSubscribersRepository, |
| 41 | SettingsController $settings |
| 42 | ) { |
| 43 | $this->scheduledTaskSubscribersRepository = $scheduledTaskSubscribersRepository; |
| 44 | $this->settings = $settings; |
| 45 | parent::__construct(); |
| 46 | } |
| 47 | |
| 48 | public function processTaskStrategy(ScheduledTaskEntity $task, $timer) { |
| 49 | $retentionDays = (int)$this->settings->get('sending_status_retention_days', ''); |
| 50 | |
| 51 | if ($retentionDays <= 0) { |
| 52 | return true; |
| 53 | } |
| 54 | |
| 55 | $startTime = microtime(true); |
| 56 | |
| 57 | do { |
| 58 | $this->cronHelper->enforceExecutionLimit($timer); |
| 59 | |
| 60 | $deleted = $this->scheduledTaskSubscribersRepository->purgeOldTaskSubscribers( |
| 61 | $retentionDays, |
| 62 | self::TASK_BATCH_SIZE, |
| 63 | self::ROW_BATCH_SIZE |
| 64 | ); |
| 65 | |
| 66 | if ( |
| 67 | $deleted === 0 || |
| 68 | (microtime(true) - $startTime) > self::MAX_EXECUTION_TIME |
| 69 | ) { |
| 70 | break; |
| 71 | } |
| 72 | |
| 73 | usleep(100000); |
| 74 | } while (true); |
| 75 | |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | public function schedule() { |
| 80 | $baseDate = Carbon::now()->millisecond(0)->startOfDay(); |
| 81 | |
| 82 | for ($slot = 0; $slot < 4; $slot++) { |
| 83 | $hour = $slot * 6 + mt_rand(0, 5); |
| 84 | $minute = mt_rand(0, 59); |
| 85 | $second = mt_rand(0, 59); |
| 86 | |
| 87 | $scheduleDate = clone $baseDate; |
| 88 | $scheduleDate->setTime($hour, $minute, $second); |
| 89 | |
| 90 | if ($scheduleDate->isPast()) { |
| 91 | $scheduleDate->addDay(); |
| 92 | } |
| 93 | |
| 94 | $this->cronWorkerScheduler->scheduleMultiple(static::TASK_TYPE, $scheduleDate); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | public function getNextRunDate() { |
| 99 | $date = Carbon::now()->millisecond(0); |
| 100 | $timeSlot = mt_rand(0, 3); |
| 101 | $hour = $timeSlot * 6 + mt_rand(0, 5); |
| 102 | $minute = mt_rand(0, 59); |
| 103 | |
| 104 | $date->setTime($hour, $minute, mt_rand(0, 59)); |
| 105 | |
| 106 | if ($date->isPast()) { |
| 107 | $date->addDay(); |
| 108 | } |
| 109 | |
| 110 | return $date; |
| 111 | } |
| 112 | } |
| 113 |