Set a deactivating Workflow to inactive once all runs are completed

[MAILPOET-4731]
This commit is contained in:
David Remer
2022-10-24 13:23:40 +03:00
committed by Jan Jakeš
parent 57548b579f
commit b74890137a

View File

@@ -101,11 +101,13 @@ class StepHandler {
} catch (Throwable $e) {
$status = $e instanceof InvalidStateException && $e->getErrorCode() === 'mailpoet_automation_workflow_not_active' ? WorkflowRun::STATUS_CANCELLED : WorkflowRun::STATUS_FAILED;
$this->workflowRunStorage->updateStatus($status, (int)$args['workflow_run_id']);
$this->postProcessWorkflowRun((int)$args['workflow_run_id']);
if (!$e instanceof Exception) {
throw new Exception($e->getMessage(), intval($e->getCode()), $e);
}
throw $e;
}
$this->postProcessWorkflowRun((int)$args['workflow_run_id']);
}
private function handleStep(array $args): void {
@@ -211,4 +213,33 @@ class StepHandler {
}
return $subjectEntries;
}
private function postProcessWorkflowRun(int $workflowRunId): void {
$workflowRun = $this->workflowRunStorage->getWorkflowRun($workflowRunId);
if (!$workflowRun) {
return;
}
$workflow = $this->workflowStorage->getWorkflow($workflowRun->getWorkflowId());
if (!$workflow) {
return;
}
$this->postProcessWorkflow($workflow);
}
private function postProcessWorkflow(Workflow $workflow): void {
if ($workflow->getStatus() === Workflow::STATUS_DEACTIVATING) {
$activeRuns = array_filter(
$this->workflowRunStorage->getWorkflowRunsForWorkflow($workflow),
function(WorkflowRun $run): bool {
return $run->getStatus() === WorkflowRun::STATUS_RUNNING;
}
);
// Set a deactivating Workflow to inactive once all workflow runs are finished.
if (!$activeRuns) {
$workflow->setStatus(Workflow::STATUS_INACTIVE);
$this->workflowStorage->updateWorkflow($workflow);
}
}
}
}