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
SimpleWorker.php
93 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Cron\Workers; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Cron\CronHelper; |
| 9 | use MailPoet\Cron\CronWorkerInterface; |
| 10 | use MailPoet\Cron\CronWorkerRunner; |
| 11 | use MailPoet\Cron\CronWorkerScheduler; |
| 12 | use MailPoet\DI\ContainerWrapper; |
| 13 | use MailPoet\Entities\ScheduledTaskEntity; |
| 14 | use MailPoet\Newsletter\Sending\ScheduledTasksRepository; |
| 15 | use MailPoetVendor\Carbon\Carbon; |
| 16 | |
| 17 | abstract class SimpleWorker implements CronWorkerInterface { |
| 18 | const TASK_TYPE = null; |
| 19 | const AUTOMATIC_SCHEDULING = true; |
| 20 | const SUPPORT_MULTIPLE_INSTANCES = true; |
| 21 | |
| 22 | public $timer; |
| 23 | |
| 24 | /** @var CronHelper */ |
| 25 | protected $cronHelper; |
| 26 | |
| 27 | /** @var CronWorkerScheduler */ |
| 28 | protected $cronWorkerScheduler; |
| 29 | |
| 30 | /** @var ScheduledTasksRepository */ |
| 31 | protected $scheduledTasksRepository; |
| 32 | |
| 33 | public function __construct() { |
| 34 | if (static::TASK_TYPE === null) { |
| 35 | throw new \Exception('Constant TASK_TYPE is not defined on subclass ' . get_class($this)); |
| 36 | } |
| 37 | $this->cronHelper = ContainerWrapper::getInstance()->get(CronHelper::class); |
| 38 | $this->cronWorkerScheduler = ContainerWrapper::getInstance()->get(CronWorkerScheduler::class); |
| 39 | $this->scheduledTasksRepository = ContainerWrapper::getInstance()->get(ScheduledTasksRepository::class); |
| 40 | } |
| 41 | |
| 42 | public function getTaskType() { |
| 43 | return static::TASK_TYPE; |
| 44 | } |
| 45 | |
| 46 | public function supportsMultipleInstances() { |
| 47 | return static::SUPPORT_MULTIPLE_INSTANCES; |
| 48 | } |
| 49 | |
| 50 | public function schedule() { |
| 51 | $this->cronWorkerScheduler->schedule(static::TASK_TYPE, $this->getNextRunDate()); |
| 52 | } |
| 53 | |
| 54 | protected function scheduleImmediately(): void { |
| 55 | $this->cronWorkerScheduler->schedule(static::TASK_TYPE, $this->getNextRunDateImmediately()); |
| 56 | } |
| 57 | |
| 58 | public function checkProcessingRequirements() { |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | public function init() { |
| 63 | } |
| 64 | |
| 65 | public function prepareTaskStrategy(ScheduledTaskEntity $task, $timer) { |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | public function processTaskStrategy(ScheduledTaskEntity $task, $timer) { |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | public function getNextRunDate() { |
| 74 | // random day of the next week |
| 75 | $date = Carbon::now()->millisecond(0); |
| 76 | $date->setISODate((int)$date->format('o'), ((int)$date->format('W')) + 1, mt_rand(1, 7)); |
| 77 | $date->startOfDay(); |
| 78 | return $date; |
| 79 | } |
| 80 | |
| 81 | protected function getNextRunDateImmediately(): Carbon { |
| 82 | return Carbon::now()->millisecond(0); |
| 83 | } |
| 84 | |
| 85 | public function scheduleAutomatically() { |
| 86 | return static::AUTOMATIC_SCHEDULING; |
| 87 | } |
| 88 | |
| 89 | protected function getCompletedTasks() { |
| 90 | return $this->scheduledTasksRepository->findCompletedByType(static::TASK_TYPE, CronWorkerRunner::TASK_BATCH_SIZE); |
| 91 | } |
| 92 | } |
| 93 |