Deactivate automations runs when deactivating automation
[MAILPOET-5982]
This commit is contained in:
@@ -2,11 +2,13 @@
|
|||||||
|
|
||||||
namespace MailPoet\Automation\Engine\Builder;
|
namespace MailPoet\Automation\Engine\Builder;
|
||||||
|
|
||||||
|
use MailPoet\Automation\Engine\Control\ActionScheduler;
|
||||||
use MailPoet\Automation\Engine\Data\Automation;
|
use MailPoet\Automation\Engine\Data\Automation;
|
||||||
use MailPoet\Automation\Engine\Data\Step;
|
use MailPoet\Automation\Engine\Data\Step;
|
||||||
use MailPoet\Automation\Engine\Exceptions;
|
use MailPoet\Automation\Engine\Exceptions;
|
||||||
use MailPoet\Automation\Engine\Exceptions\UnexpectedValueException;
|
use MailPoet\Automation\Engine\Exceptions\UnexpectedValueException;
|
||||||
use MailPoet\Automation\Engine\Hooks;
|
use MailPoet\Automation\Engine\Hooks;
|
||||||
|
use MailPoet\Automation\Engine\Storage\AutomationRunStorage;
|
||||||
use MailPoet\Automation\Engine\Storage\AutomationStatisticsStorage;
|
use MailPoet\Automation\Engine\Storage\AutomationStatisticsStorage;
|
||||||
use MailPoet\Automation\Engine\Storage\AutomationStorage;
|
use MailPoet\Automation\Engine\Storage\AutomationStorage;
|
||||||
use MailPoet\Automation\Engine\Validation\AutomationValidator;
|
use MailPoet\Automation\Engine\Validation\AutomationValidator;
|
||||||
@@ -27,11 +29,17 @@ class UpdateAutomationController {
|
|||||||
/** @var UpdateStepsController */
|
/** @var UpdateStepsController */
|
||||||
private $updateStepsController;
|
private $updateStepsController;
|
||||||
|
|
||||||
|
private AutomationRunStorage $automationRunStorage;
|
||||||
|
|
||||||
|
private ActionScheduler $actionScheduler;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
Hooks $hooks,
|
Hooks $hooks,
|
||||||
AutomationStorage $storage,
|
AutomationStorage $storage,
|
||||||
AutomationStatisticsStorage $statisticsStorage,
|
AutomationStatisticsStorage $statisticsStorage,
|
||||||
AutomationValidator $automationValidator,
|
AutomationValidator $automationValidator,
|
||||||
|
AutomationRunStorage $automationRunStorage,
|
||||||
|
ActionScheduler $actionScheduler,
|
||||||
UpdateStepsController $updateStepsController
|
UpdateStepsController $updateStepsController
|
||||||
) {
|
) {
|
||||||
$this->hooks = $hooks;
|
$this->hooks = $hooks;
|
||||||
@@ -39,6 +47,8 @@ class UpdateAutomationController {
|
|||||||
$this->statisticsStorage = $statisticsStorage;
|
$this->statisticsStorage = $statisticsStorage;
|
||||||
$this->automationValidator = $automationValidator;
|
$this->automationValidator = $automationValidator;
|
||||||
$this->updateStepsController = $updateStepsController;
|
$this->updateStepsController = $updateStepsController;
|
||||||
|
$this->automationRunStorage = $automationRunStorage;
|
||||||
|
$this->actionScheduler = $actionScheduler;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function updateAutomation(int $id, array $data): Automation {
|
public function updateAutomation(int $id, array $data): Automation {
|
||||||
@@ -66,6 +76,10 @@ class UpdateAutomationController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($automation->getStatus() === Automation::STATUS_DRAFT) {
|
||||||
|
$this->unscheduleAutomationRuns($automation);
|
||||||
|
}
|
||||||
|
|
||||||
if (array_key_exists('meta', $data)) {
|
if (array_key_exists('meta', $data)) {
|
||||||
$automation->deleteAllMetas();
|
$automation->deleteAllMetas();
|
||||||
foreach ($data['meta'] as $key => $value) {
|
foreach ($data['meta'] as $key => $value) {
|
||||||
@@ -141,4 +155,22 @@ class UpdateAutomationController {
|
|||||||
unset($bData['args']);
|
unset($bData['args']);
|
||||||
return $aData === $bData;
|
return $aData === $bData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function unscheduleAutomationRuns(Automation $automation): void {
|
||||||
|
$runIds = [];
|
||||||
|
$runs = $this->automationRunStorage->getAutomationRunsForAutomation($automation);
|
||||||
|
foreach ($runs as $run) {
|
||||||
|
$runIds[$run->getId()] = $run;
|
||||||
|
}
|
||||||
|
|
||||||
|
$actions = $this->actionScheduler->getScheduledActions(['hook' => Hooks::AUTOMATION_STEP, 'status' => 'pending']);
|
||||||
|
|
||||||
|
foreach ($actions as $action) {
|
||||||
|
$args = $action->get_args();
|
||||||
|
$automationArgs = reset($args);
|
||||||
|
if (isset($automationArgs['automation_run_id']) && isset($runIds[$automationArgs['automation_run_id']])) {
|
||||||
|
$this->actionScheduler->unscheduleAction(Hooks::AUTOMATION_STEP, $args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user