Automations
1 year ago
KeyCheck
1 year ago
SendingQueue
3 weeks ago
StatsNotifications
2 months ago
AuthorizedSendingEmailsCheck.php
3 years ago
BackfillEngagementData.php
1 year ago
Bounce.php
3 weeks ago
BounceTaskSubscribersCleanup.php
1 month ago
BulkConfirmationEmailResend.php
3 months ago
ExportFilesCleanup.php
2 months ago
InactiveSubscribersMaintenance.php
1 month ago
LogCleanup.php
11 months ago
Mixpanel.php
10 months ago
NewsletterTemplateThumbnails.php
1 year ago
ReEngagementEmailsScheduler.php
1 year ago
Scheduler.php
2 months ago
SendingQueueBodyCleanup.php
3 months ago
SendingTaskSubscribersCleanup.php
3 months ago
SimpleWorker.php
1 year ago
StatisticsExport.php
3 months ago
SubscriberLimitNotificationWorker.php
2 months ago
SubscriberLinkTokens.php
1 year ago
SubscribersCountCacheRecalculation.php
1 month ago
SubscribersEngagementScore.php
1 week ago
SubscribersLastEngagement.php
3 months ago
SubscribersSegmentsCountSync.php
1 month ago
SubscribersStatsReport.php
1 year ago
Tracks.php
10 months ago
UnconfirmedSubscribersCleanup.php
3 months ago
UnsubscribeTokens.php
1 month ago
WooCommercePastOrders.php
1 year ago
WooCommerceSync.php
3 years ago
WorkersFactory.php
1 month ago
index.php
3 years ago
SubscribersEngagementScore.php
90 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\Segments\SegmentsRepository; |
| 11 | use MailPoet\Statistics\StatisticsOpensRepository; |
| 12 | use MailPoet\Subscribers\SubscribersRepository; |
| 13 | use MailPoetVendor\Carbon\Carbon; |
| 14 | |
| 15 | class SubscribersEngagementScore extends SimpleWorker { |
| 16 | const AUTOMATIC_SCHEDULING = true; |
| 17 | const SUPPORT_MULTIPLE_INSTANCES = false; |
| 18 | const BATCH_SIZE = 1000; |
| 19 | const SEGMENTS_BATCH_SIZE = 100; |
| 20 | const TASK_TYPE = 'subscribers_engagement_score'; |
| 21 | |
| 22 | /** @var SegmentsRepository */ |
| 23 | private $segmentsRepository; |
| 24 | |
| 25 | /** @var StatisticsOpensRepository */ |
| 26 | private $statisticsOpensRepository; |
| 27 | |
| 28 | /** @var SubscribersRepository */ |
| 29 | private $subscribersRepository; |
| 30 | |
| 31 | public function __construct( |
| 32 | SegmentsRepository $segmentsRepository, |
| 33 | StatisticsOpensRepository $statisticsOpensRepository, |
| 34 | SubscribersRepository $subscribersRepository |
| 35 | ) { |
| 36 | parent::__construct(); |
| 37 | $this->segmentsRepository = $segmentsRepository; |
| 38 | $this->statisticsOpensRepository = $statisticsOpensRepository; |
| 39 | $this->subscribersRepository = $subscribersRepository; |
| 40 | } |
| 41 | |
| 42 | public function processTaskStrategy(ScheduledTaskEntity $task, $timer) { |
| 43 | // The recalculator relies on UPDATE ... LEFT JOIN, which the SQLite integration in |
| 44 | // WordPress Playground does not support. Make the task a no-op there: it never writes |
| 45 | // engagementScoreUpdatedAt, so the loop below would keep re-reading the same batch |
| 46 | // until the execution limit throws. Segment averages are skipped too because they |
| 47 | // average subscriber scores that stay unset. |
| 48 | if (Connection::isSQLite()) { |
| 49 | return true; |
| 50 | } |
| 51 | |
| 52 | while ($this->recalculateSubscribers() > 0) { |
| 53 | $this->cronHelper->enforceExecutionLimit($timer); // Throws exception and interrupts process if over execution limit |
| 54 | } |
| 55 | while ($this->recalculateSegments($timer) > 0) { |
| 56 | $this->cronHelper->enforceExecutionLimit($timer); |
| 57 | } |
| 58 | $this->schedule(); |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | private function recalculateSubscribers(): int { |
| 63 | $subscriberIds = $this->subscribersRepository->findIdsByUpdatedScoreNotInLastMonth(self::BATCH_SIZE); |
| 64 | $this->statisticsOpensRepository->recalculateSubscribersScore($subscriberIds); |
| 65 | return count($subscriberIds); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @param float $timer |
| 70 | */ |
| 71 | private function recalculateSegments($timer): int { |
| 72 | $segments = $this->segmentsRepository->findByUpdatedScoreNotInLastDay(self::SEGMENTS_BATCH_SIZE); |
| 73 | foreach ($segments as $segment) { |
| 74 | $this->statisticsOpensRepository->recalculateSegmentScore($segment); |
| 75 | // A single segment recalculation can be slow on large lists, so check the limit per segment, |
| 76 | // not only per batch. Each processed segment is flushed immediately, so progress survives. |
| 77 | $this->cronHelper->enforceExecutionLimit($timer); |
| 78 | } |
| 79 | return count($segments); |
| 80 | } |
| 81 | |
| 82 | public function getNextRunDate() { |
| 83 | // random day of the next week |
| 84 | $date = Carbon::now()->millisecond(0); |
| 85 | $date->addDay(); |
| 86 | $date->setTime(mt_rand(0, 23), mt_rand(0, 59)); |
| 87 | return $date; |
| 88 | } |
| 89 | } |
| 90 |