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
AutomationController.php
58 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Engine\Control; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use ActionScheduler_CanceledAction; |
| 9 | use MailPoet\Automation\Engine\Data\AutomationRunLog; |
| 10 | use MailPoet\Automation\Engine\Exceptions; |
| 11 | use MailPoet\Automation\Engine\Hooks; |
| 12 | use MailPoet\Automation\Engine\Storage\AutomationRunLogStorage; |
| 13 | |
| 14 | class AutomationController { |
| 15 | private ActionScheduler $actionScheduler; |
| 16 | private AutomationRunLogStorage $automationRunLogStorage; |
| 17 | |
| 18 | public function __construct( |
| 19 | ActionScheduler $actionScheduler, |
| 20 | AutomationRunLogStorage $automationRunLogStorage |
| 21 | ) { |
| 22 | $this->actionScheduler = $actionScheduler; |
| 23 | $this->automationRunLogStorage = $automationRunLogStorage; |
| 24 | } |
| 25 | |
| 26 | public function enqueueProgress(int $runId, string $stepId): void { |
| 27 | $log = $this->automationRunLogStorage->getAutomationRunLogByRunAndStepId($runId, $stepId); |
| 28 | if (!$log) { |
| 29 | throw Exceptions::stepNotStarted($stepId, $runId); |
| 30 | } |
| 31 | |
| 32 | if ($log->getStatus() !== AutomationRunLog::STATUS_RUNNING) { |
| 33 | throw Exceptions::stepNotRunning($stepId, $log->getStatus(), $runId); |
| 34 | } |
| 35 | |
| 36 | $runNumber = $log->getRunNumber() + 1; |
| 37 | $args = [ |
| 38 | 'automation_run_id' => $runId, |
| 39 | 'step_id' => $stepId, |
| 40 | 'run_number' => $runNumber, |
| 41 | ]; |
| 42 | |
| 43 | // if a pending action exists, unschedule it |
| 44 | $this->actionScheduler->unscheduleAction(Hooks::AUTOMATION_STEP, [$args]); |
| 45 | |
| 46 | // if an action still exists (pending, in-progress, complete, failed), it's an error |
| 47 | $actions = $this->actionScheduler->getScheduledActions(['hook' => Hooks::AUTOMATION_STEP, 'args' => [$args]]); |
| 48 | $processedActions = array_filter($actions, function ($action) { |
| 49 | return !$action instanceof ActionScheduler_CanceledAction; |
| 50 | }); |
| 51 | if (count($processedActions) > 0) { |
| 52 | throw Exceptions::stepActionProcessed($stepId, $runId, $runNumber); |
| 53 | } |
| 54 | |
| 55 | $this->actionScheduler->enqueue(Hooks::AUTOMATION_STEP, [$args]); |
| 56 | } |
| 57 | } |
| 58 |