ActionScheduler
2 months ago
CliCommands
1 month ago
Triggers
2 months ago
Workers
4 days ago
CronHelper.php
2 months ago
CronTrigger.php
2 years ago
CronWorkerInterface.php
3 years ago
CronWorkerRunner.php
2 months ago
CronWorkerScheduler.php
10 months ago
Daemon.php
4 days ago
DaemonActionSchedulerRunner.php
6 months ago
DaemonHttpRunner.php
2 months ago
Supervisor.php
3 years ago
index.php
3 years ago
CronWorkerScheduler.php
84 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Cron; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Entities\ScheduledTaskEntity; |
| 9 | use MailPoet\Newsletter\Sending\ScheduledTasksRepository; |
| 10 | use MailPoetVendor\Carbon\Carbon; |
| 11 | |
| 12 | class CronWorkerScheduler { |
| 13 | /** @var ScheduledTasksRepository */ |
| 14 | private $scheduledTaskRepository; |
| 15 | |
| 16 | public function __construct( |
| 17 | ScheduledTasksRepository $scheduledTaskRepository |
| 18 | ) { |
| 19 | $this->scheduledTaskRepository = $scheduledTaskRepository; |
| 20 | } |
| 21 | |
| 22 | public function scheduleImmediatelyIfNotRunning($taskType, $priority = ScheduledTaskEntity::PRIORITY_LOW): ScheduledTaskEntity { |
| 23 | $task = $this->scheduledTaskRepository->findScheduledOrRunningTask($taskType); |
| 24 | // Do nothing when task is running |
| 25 | if (($task instanceof ScheduledTaskEntity) && $task->getStatus() === null) { |
| 26 | return $task; |
| 27 | } |
| 28 | $now = Carbon::now()->millisecond(0); |
| 29 | // Reschedule existing scheduled task |
| 30 | if ($task instanceof ScheduledTaskEntity) { |
| 31 | $task->setScheduledAt($now); |
| 32 | $task->setPriority($priority); |
| 33 | $this->scheduledTaskRepository->flush(); |
| 34 | } |
| 35 | // Schedule new task |
| 36 | return $this->schedule($taskType, $now, $priority); |
| 37 | } |
| 38 | |
| 39 | public function schedule($taskType, $nextRunDate, $priority = ScheduledTaskEntity::PRIORITY_LOW): ScheduledTaskEntity { |
| 40 | $alreadyScheduled = $this->scheduledTaskRepository->findScheduledTask($taskType); |
| 41 | if ($alreadyScheduled) { |
| 42 | return $alreadyScheduled; |
| 43 | } |
| 44 | return $this->createAndPersistTask($taskType, $nextRunDate, $priority); |
| 45 | } |
| 46 | |
| 47 | public function scheduleMultiple($taskType, $nextRunDate, $priority = ScheduledTaskEntity::PRIORITY_LOW): ScheduledTaskEntity { |
| 48 | // Allow multiple tasks of the same type with different run dates |
| 49 | return $this->createAndPersistTask($taskType, $nextRunDate, $priority); |
| 50 | } |
| 51 | |
| 52 | private function createAndPersistTask($taskType, $nextRunDate, $priority): ScheduledTaskEntity { |
| 53 | $task = new ScheduledTaskEntity(); |
| 54 | $task->setType($taskType); |
| 55 | $task->setStatus(ScheduledTaskEntity::STATUS_SCHEDULED); |
| 56 | $task->setPriority($priority); |
| 57 | $task->setScheduledAt($nextRunDate); |
| 58 | $this->scheduledTaskRepository->persist($task); |
| 59 | $this->scheduledTaskRepository->flush(); |
| 60 | return $task; |
| 61 | } |
| 62 | |
| 63 | public function reschedule(ScheduledTaskEntity $task, $timeout) { |
| 64 | $scheduledAt = Carbon::now()->millisecond(0); |
| 65 | $task->setScheduledAt($scheduledAt->addMinutes($timeout)); |
| 66 | $task->setStatus(ScheduledTaskEntity::STATUS_SCHEDULED); |
| 67 | $this->scheduledTaskRepository->persist($task); |
| 68 | $this->scheduledTaskRepository->flush(); |
| 69 | } |
| 70 | |
| 71 | public function rescheduleProgressively(ScheduledTaskEntity $task): int { |
| 72 | $scheduledAt = Carbon::now()->millisecond(0); |
| 73 | $rescheduleCount = $task->getRescheduleCount(); |
| 74 | $timeout = (int)min(ScheduledTaskEntity::BASIC_RESCHEDULE_TIMEOUT * pow(2, $rescheduleCount), ScheduledTaskEntity::MAX_RESCHEDULE_TIMEOUT); |
| 75 | $task->setScheduledAt($scheduledAt->addMinutes($timeout)); |
| 76 | $task->setRescheduleCount($rescheduleCount + 1); |
| 77 | $task->setStatus(ScheduledTaskEntity::STATUS_SCHEDULED); |
| 78 | $this->scheduledTaskRepository->persist($task); |
| 79 | $this->scheduledTaskRepository->flush(); |
| 80 | |
| 81 | return $timeout; |
| 82 | } |
| 83 | } |
| 84 |