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
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
LogCleanup.php
96 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\Logging\LogRepository; |
| 10 | use MailPoetVendor\Carbon\Carbon; |
| 11 | |
| 12 | class LogCleanup extends SimpleWorker { |
| 13 | const TASK_TYPE = 'log_cleanup'; |
| 14 | const DAYS_TO_KEEP_LOGS = 30; |
| 15 | const BATCH_SIZE = 5000; |
| 16 | const MAX_EXECUTION_TIME = 30; |
| 17 | const SUPPORT_MULTIPLE_INSTANCES = false; |
| 18 | |
| 19 | /** @var LogRepository */ |
| 20 | private $logRepository; |
| 21 | |
| 22 | public function __construct( |
| 23 | LogRepository $logRepository |
| 24 | ) { |
| 25 | $this->logRepository = $logRepository; |
| 26 | parent::__construct(); |
| 27 | } |
| 28 | |
| 29 | public function processTaskStrategy(ScheduledTaskEntity $task, $timer) { |
| 30 | $startTime = microtime(true); |
| 31 | |
| 32 | do { |
| 33 | $this->cronHelper->enforceExecutionLimit($timer); |
| 34 | |
| 35 | $deleted = $this->logRepository->purgeOldLogs( |
| 36 | self::DAYS_TO_KEEP_LOGS, |
| 37 | self::BATCH_SIZE |
| 38 | ); |
| 39 | |
| 40 | // Stop if we've deleted less than the batch size (meaning we're done) |
| 41 | // or if we've exceeded the maximum execution time |
| 42 | if ( |
| 43 | $deleted < self::BATCH_SIZE || |
| 44 | (microtime(true) - $startTime) > self::MAX_EXECUTION_TIME |
| 45 | ) { |
| 46 | break; |
| 47 | } |
| 48 | |
| 49 | } while (true); |
| 50 | |
| 51 | return true; |
| 52 | } |
| 53 | |
| 54 | public function schedule() { |
| 55 | // Schedule four tasks per day in different 6-hour time slots |
| 56 | $baseDate = Carbon::now()->millisecond(0)->startOfDay(); |
| 57 | |
| 58 | // Schedule tasks for each 6-hour time slot |
| 59 | for ($slot = 0; $slot < 4; $slot++) { |
| 60 | $hour = $slot * 6 + mt_rand(0, 5); // 0-5, 6-11, 12-17, 18-23 |
| 61 | $minute = mt_rand(0, 59); |
| 62 | $second = mt_rand(0, 59); |
| 63 | |
| 64 | $scheduleDate = clone $baseDate; |
| 65 | $scheduleDate->setTime($hour, $minute, $second); |
| 66 | |
| 67 | // If the time has already passed today, schedule for tomorrow |
| 68 | if ($scheduleDate->isPast()) { |
| 69 | $scheduleDate->addDay(); |
| 70 | } |
| 71 | |
| 72 | $this->cronWorkerScheduler->scheduleMultiple(static::TASK_TYPE, $scheduleDate); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | public function getNextRunDate() { |
| 77 | // Return a single next run date for backward compatibility |
| 78 | // The actual scheduling is handled by the overridden schedule() method |
| 79 | $date = Carbon::now()->millisecond(0); |
| 80 | |
| 81 | // Choose one of four 6-hour time slots |
| 82 | $timeSlot = mt_rand(0, 3); |
| 83 | $hour = $timeSlot * 6 + mt_rand(0, 5); // 0-5, 6-11, 12-17, 18-23 |
| 84 | $minute = mt_rand(0, 59); |
| 85 | |
| 86 | $date->setTime($hour, $minute, mt_rand(0, 59)); |
| 87 | |
| 88 | // If the chosen time has already passed today, schedule for tomorrow |
| 89 | if ($date->isPast()) { |
| 90 | $date->addDay(); |
| 91 | } |
| 92 | |
| 93 | return $date; |
| 94 | } |
| 95 | } |
| 96 |