Automations
1 year ago
KeyCheck
1 year ago
SendingQueue
4 days ago
StatsNotifications
2 months ago
AuthorizedSendingEmailsCheck.php
3 years ago
BackfillEngagementData.php
1 year ago
Bounce.php
4 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
SendingQueueBodyCleanup.php
98 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\SendingQueuesRepository; |
| 10 | use MailPoet\Settings\SettingsController; |
| 11 | use MailPoetVendor\Carbon\Carbon; |
| 12 | |
| 13 | /** |
| 14 | * Runs 4x daily in staggered 6-hour slots. Each run NULLs newsletter_rendered_body |
| 15 | * for completed sending queues older than the configured retention window (default 30 days). |
| 16 | * Processes in batches of 250 rows with a 30s time limit per run. |
| 17 | * Setting the retention to '' (Never) disables cleanup entirely. |
| 18 | */ |
| 19 | class SendingQueueBodyCleanup extends SimpleWorker { |
| 20 | const TASK_TYPE = 'sending_queue_body_cleanup'; |
| 21 | const BATCH_SIZE = 250; |
| 22 | const MAX_EXECUTION_TIME = 30; |
| 23 | const SUPPORT_MULTIPLE_INSTANCES = false; |
| 24 | |
| 25 | /** @var SendingQueuesRepository */ |
| 26 | private $sendingQueuesRepository; |
| 27 | |
| 28 | /** @var SettingsController */ |
| 29 | private $settings; |
| 30 | |
| 31 | public function __construct( |
| 32 | SendingQueuesRepository $sendingQueuesRepository, |
| 33 | SettingsController $settings |
| 34 | ) { |
| 35 | $this->sendingQueuesRepository = $sendingQueuesRepository; |
| 36 | $this->settings = $settings; |
| 37 | parent::__construct(); |
| 38 | } |
| 39 | |
| 40 | public function processTaskStrategy(ScheduledTaskEntity $task, $timer) { |
| 41 | $retentionDays = (int)$this->settings->get('sending_queue_body_retention_days'); |
| 42 | if ($retentionDays <= 0) { |
| 43 | return true; |
| 44 | } |
| 45 | |
| 46 | $startTime = microtime(true); |
| 47 | do { |
| 48 | $this->cronHelper->enforceExecutionLimit($timer); |
| 49 | |
| 50 | $updated = $this->sendingQueuesRepository->nullRenderedBodyForOldCompletedQueues( |
| 51 | $retentionDays, |
| 52 | self::BATCH_SIZE |
| 53 | ); |
| 54 | |
| 55 | if ($updated < self::BATCH_SIZE || (microtime(true) - $startTime) > self::MAX_EXECUTION_TIME) { |
| 56 | break; |
| 57 | } |
| 58 | } while (true); |
| 59 | |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | public function schedule() { |
| 64 | $baseDate = Carbon::now()->millisecond(0)->startOfDay(); |
| 65 | |
| 66 | for ($slot = 0; $slot < 4; $slot++) { |
| 67 | $hour = $slot * 6 + mt_rand(0, 5); |
| 68 | $minute = mt_rand(0, 59); |
| 69 | $second = mt_rand(0, 59); |
| 70 | |
| 71 | $scheduleDate = clone $baseDate; |
| 72 | $scheduleDate->setTime($hour, $minute, $second); |
| 73 | |
| 74 | if ($scheduleDate->isPast()) { |
| 75 | $scheduleDate->addDay(); |
| 76 | } |
| 77 | |
| 78 | $this->cronWorkerScheduler->scheduleMultiple(static::TASK_TYPE, $scheduleDate); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | public function getNextRunDate() { |
| 83 | $date = Carbon::now()->millisecond(0); |
| 84 | |
| 85 | $timeSlot = mt_rand(0, 3); |
| 86 | $hour = $timeSlot * 6 + mt_rand(0, 5); |
| 87 | $minute = mt_rand(0, 59); |
| 88 | |
| 89 | $date->setTime($hour, $minute, mt_rand(0, 59)); |
| 90 | |
| 91 | if ($date->isPast()) { |
| 92 | $date->addDay(); |
| 93 | } |
| 94 | |
| 95 | return $date; |
| 96 | } |
| 97 | } |
| 98 |