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
SubscribersEngagementScore.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\Segments\SegmentsRepository; |
| 10 | use MailPoet\Statistics\StatisticsOpensRepository; |
| 11 | use MailPoet\Subscribers\SubscribersRepository; |
| 12 | use MailPoetVendor\Carbon\Carbon; |
| 13 | |
| 14 | class SubscribersEngagementScore extends SimpleWorker { |
| 15 | const AUTOMATIC_SCHEDULING = true; |
| 16 | const SUPPORT_MULTIPLE_INSTANCES = false; |
| 17 | const BATCH_SIZE = 1000; |
| 18 | const SEGMENTS_BATCH_SIZE = 100; |
| 19 | const TASK_TYPE = 'subscribers_engagement_score'; |
| 20 | |
| 21 | /** @var SegmentsRepository */ |
| 22 | private $segmentsRepository; |
| 23 | |
| 24 | /** @var StatisticsOpensRepository */ |
| 25 | private $statisticsOpensRepository; |
| 26 | |
| 27 | /** @var SubscribersRepository */ |
| 28 | private $subscribersRepository; |
| 29 | |
| 30 | public function __construct( |
| 31 | SegmentsRepository $segmentsRepository, |
| 32 | StatisticsOpensRepository $statisticsOpensRepository, |
| 33 | SubscribersRepository $subscribersRepository |
| 34 | ) { |
| 35 | parent::__construct(); |
| 36 | $this->segmentsRepository = $segmentsRepository; |
| 37 | $this->statisticsOpensRepository = $statisticsOpensRepository; |
| 38 | $this->subscribersRepository = $subscribersRepository; |
| 39 | } |
| 40 | |
| 41 | public function processTaskStrategy(ScheduledTaskEntity $task, $timer) { |
| 42 | while ($this->recalculateSubscribers() > 0) { |
| 43 | $this->cronHelper->enforceExecutionLimit($timer); // Throws exception and interrupts process if over execution limit |
| 44 | } |
| 45 | while ($this->recalculateSegments($timer) > 0) { |
| 46 | $this->cronHelper->enforceExecutionLimit($timer); |
| 47 | } |
| 48 | $this->schedule(); |
| 49 | return true; |
| 50 | } |
| 51 | |
| 52 | private function recalculateSubscribers(): int { |
| 53 | $subscriberIds = $this->subscribersRepository->findIdsByUpdatedScoreNotInLastMonth(self::BATCH_SIZE); |
| 54 | $this->statisticsOpensRepository->recalculateSubscribersScore($subscriberIds); |
| 55 | return count($subscriberIds); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @param float $timer |
| 60 | */ |
| 61 | private function recalculateSegments($timer): int { |
| 62 | $segments = $this->segmentsRepository->findByUpdatedScoreNotInLastDay(self::SEGMENTS_BATCH_SIZE); |
| 63 | foreach ($segments as $segment) { |
| 64 | $this->statisticsOpensRepository->recalculateSegmentScore($segment); |
| 65 | // A single segment recalculation can be slow on large lists, so check the limit per segment, |
| 66 | // not only per batch. Each processed segment is flushed immediately, so progress survives. |
| 67 | $this->cronHelper->enforceExecutionLimit($timer); |
| 68 | } |
| 69 | return count($segments); |
| 70 | } |
| 71 | |
| 72 | public function getNextRunDate() { |
| 73 | // random day of the next week |
| 74 | $date = Carbon::now()->millisecond(0); |
| 75 | $date->addDay(); |
| 76 | $date->setTime(mt_rand(0, 23), mt_rand(0, 59)); |
| 77 | return $date; |
| 78 | } |
| 79 | } |
| 80 |