ActionScheduler.php
3 weeks ago
AutomationController.php
2 years ago
FilterHandler.php
1 month ago
RootStep.php
2 years ago
StepHandler.php
2 months ago
StepRunController.php
1 year ago
StepRunControllerFactory.php
1 year ago
StepRunLogger.php
2 months ago
StepRunLoggerFactory.php
2 years ago
StepScheduler.php
1 year ago
SubjectLoader.php
3 years ago
SubjectTransformerHandler.php
1 year ago
TriggerHandler.php
2 years ago
index.php
3 years ago
StepScheduler.php
123 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Engine\Control; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Automation\Engine\Data\AutomationRun; |
| 9 | use MailPoet\Automation\Engine\Data\StepRunArgs; |
| 10 | use MailPoet\Automation\Engine\Exceptions; |
| 11 | use MailPoet\Automation\Engine\Hooks; |
| 12 | use MailPoet\Automation\Engine\Storage\AutomationRunStorage; |
| 13 | |
| 14 | class StepScheduler { |
| 15 | /** @var ActionScheduler */ |
| 16 | private $actionScheduler; |
| 17 | |
| 18 | /** @var AutomationRunStorage */ |
| 19 | private $automationRunStorage; |
| 20 | |
| 21 | public function __construct( |
| 22 | ActionScheduler $actionScheduler, |
| 23 | AutomationRunStorage $automationRunStorage |
| 24 | ) { |
| 25 | $this->actionScheduler = $actionScheduler; |
| 26 | $this->automationRunStorage = $automationRunStorage; |
| 27 | } |
| 28 | |
| 29 | public function scheduleProgress(StepRunArgs $args, ?int $timestamp = null): int { |
| 30 | $runId = $args->getAutomationRun()->getId(); |
| 31 | $data = $this->getActionData($runId, $args->getStep()->getId(), $args->getRunNumber() + 1); |
| 32 | return $this->scheduleStepAction($data, $timestamp); |
| 33 | } |
| 34 | |
| 35 | public function scheduleNextStep(StepRunArgs $args, ?int $timestamp = null): int { |
| 36 | $step = $args->getStep(); |
| 37 | $nextSteps = $step->getNextSteps(); |
| 38 | |
| 39 | // complete the automation run if there are no more steps |
| 40 | if (count($nextSteps) === 0) { |
| 41 | $this->completeAutomationRun($args); |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | if (count($nextSteps) > 1) { |
| 46 | throw Exceptions::nextStepNotScheduled($step->getId()); |
| 47 | } |
| 48 | |
| 49 | return $this->scheduleNextStepByIndex($args, 0, $timestamp); |
| 50 | } |
| 51 | |
| 52 | public function scheduleNextStepByIndex(StepRunArgs $args, int $nextStepIndex, ?int $timestamp = null): int { |
| 53 | $step = $args->getStep(); |
| 54 | $nextStep = $step->getNextSteps()[$nextStepIndex] ?? null; |
| 55 | if (!$nextStep) { |
| 56 | throw Exceptions::nextStepNotFound($step->getId(), $nextStepIndex); |
| 57 | } |
| 58 | |
| 59 | $runId = $args->getAutomationRun()->getId(); |
| 60 | $nextStepId = $nextStep->getId(); |
| 61 | if (!$nextStepId) { |
| 62 | $this->completeAutomationRun($args); |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | $data = $this->getActionData($runId, $nextStepId); |
| 67 | $id = $this->scheduleStepAction($data, $timestamp); |
| 68 | $this->automationRunStorage->updateNextStep($runId, $nextStepId); |
| 69 | return $id; |
| 70 | } |
| 71 | |
| 72 | public function hasScheduledNextStep(StepRunArgs $args): bool { |
| 73 | $runId = $args->getAutomationRun()->getId(); |
| 74 | foreach ($args->getStep()->getNextStepIds() as $nextStepId) { |
| 75 | $data = $this->getActionData($runId, $nextStepId); |
| 76 | $hasStep = $this->actionScheduler->hasScheduledAction(Hooks::AUTOMATION_STEP, $data); |
| 77 | if ($hasStep) { |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | // BC for old steps without run number |
| 82 | unset($data[0]['run_number']); |
| 83 | $hasStep = $this->actionScheduler->hasScheduledAction(Hooks::AUTOMATION_STEP, $data); |
| 84 | if ($hasStep) { |
| 85 | return true; |
| 86 | } |
| 87 | } |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | public function hasScheduledProgress(StepRunArgs $args): bool { |
| 92 | $runId = $args->getAutomationRun()->getId(); |
| 93 | $data = $this->getActionData($runId, $args->getStep()->getId(), $args->getRunNumber() + 1); |
| 94 | return $this->actionScheduler->hasScheduledAction(Hooks::AUTOMATION_STEP, $data); |
| 95 | } |
| 96 | |
| 97 | public function hasScheduledStep(StepRunArgs $args): bool { |
| 98 | return $this->hasScheduledNextStep($args) || $this->hasScheduledProgress($args); |
| 99 | } |
| 100 | |
| 101 | private function scheduleStepAction(array $data, ?int $timestamp = null): int { |
| 102 | return $timestamp === null |
| 103 | ? $this->actionScheduler->enqueue(Hooks::AUTOMATION_STEP, $data) |
| 104 | : $this->actionScheduler->schedule($timestamp, Hooks::AUTOMATION_STEP, $data); |
| 105 | } |
| 106 | |
| 107 | private function completeAutomationRun(StepRunArgs $args): void { |
| 108 | $runId = $args->getAutomationRun()->getId(); |
| 109 | $this->automationRunStorage->updateNextStep($runId, null); |
| 110 | $this->automationRunStorage->updateStatus($runId, AutomationRun::STATUS_COMPLETE); |
| 111 | } |
| 112 | |
| 113 | private function getActionData(int $runId, string $stepId, int $runNumber = 1): array { |
| 114 | return [ |
| 115 | [ |
| 116 | 'automation_run_id' => $runId, |
| 117 | 'step_id' => $stepId, |
| 118 | 'run_number' => $runNumber, |
| 119 | ], |
| 120 | ]; |
| 121 | } |
| 122 | } |
| 123 |