Create message and handler for canceling stale workflows

This commit is contained in:
Julie Lenaerts 2024-08-12 11:44:31 +02:00 committed by Julien Fastré
parent 860ae5cedf
commit 34edb02cd0
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
2 changed files with 38 additions and 11 deletions

View File

@ -4,12 +4,12 @@ namespace Chill\MainBundle\Service\Workflow;
class CancelStaleWorkflow class CancelStaleWorkflow
{ {
public function __construct(public array $workflowIds) public function __construct(public int $workflowId)
{ {
} }
public function getWorkflowIds(): array public function getWorkflowId(): int
{ {
return $this->workflowIds; return $this->workflowId;
} }
} }

View File

@ -5,26 +5,53 @@ namespace Chill\MainBundle\Service\Workflow;
use Chill\MainBundle\Repository\Workflow\EntityWorkflowRepository; use Chill\MainBundle\Repository\Workflow\EntityWorkflowRepository;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface; use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
use Symfony\Component\Workflow\Registry;
class CancelStaleWorkflowHandler implements MessageHandlerInterface #[AsMessageHandler]
class CancelStaleWorkflowHandler
{ {
public function __construct(private readonly EntityWorkflowRepository $workflowRepository, private EntityManagerInterface $em, private LoggerInterface $logger) public function __construct(private readonly EntityWorkflowRepository $workflowRepository, private readonly Registry $registry, private EntityManagerInterface $em, private LoggerInterface $logger)
{ {
} }
public function __invoke(CancelStaleWorkflow $message): void public function __invoke(CancelStaleWorkflow $message): void
{ {
$workflowIds = $message->getWorkflowIds(); $workflowId = $message->getWorkflowId();
foreach ($workflowIds as $workflowId) { $workflow = $this->workflowRepository->find($workflowId);
$workflow = $this->workflowRepository->find($workflowId); $workflowComponent = $this->registry->get($workflow, $workflow->getWorkflowName());
$metadataStore = $workflowComponent->getMetadataStore();
$transitions = $workflowComponent->getEnabledTransitions($workflow);
$steps = $workflow->getSteps();
$steps = $workflow->getSteps(); $transitionApplied = false;
if (count($steps) === 1) {
$this->em->remove($workflow->getCurrentStep());
$this->em->remove($workflow);
} else {
foreach ($transitions as $transition) {
$isFinal = $metadataStore->getMetadata('isFinal', $transition);
$isFinalPositive = $metadataStore->getMetadata('isFinalPositive', $transition);
if ($isFinal && !$isFinalPositive) {
$workflowComponent->apply($workflow, 'annule');
$this->logger->info(sprintf('EntityWorkflow %d has been transitioned.', $workflowId));
$transitionApplied = true;
break;
}
}
if (!$transitionApplied) {
$this->logger->error(sprintf('No valid transition found for EntityWorkflow %d.', $workflowId));
throw new UnrecoverableMessageHandlingException(
sprintf('No valid transition found for EntityWorkflow %d.', $workflowId)
);
}
} }
} }
} }