mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-29 19:13:49 +00:00
Suffix message class with 'Message' and add check on workflow to assert no transitions were applied since message placed in queue
This commit is contained in:
@@ -54,8 +54,8 @@ class CancelStaleWorkflowCronJob implements CronJobInterface
|
||||
|
||||
foreach ($staleWorkflowIds as $wId) {
|
||||
try {
|
||||
$this->messageBus->dispatch(new CancelStaleWorkflow($wId));
|
||||
$lastCanceled = $wId;
|
||||
$this->messageBus->dispatch(new CancelStaleWorkflowMessage($wId));
|
||||
$lastCanceled = max($wId, $lastCanceled);
|
||||
++$processedCount;
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->error("Failed to dispatch CancelStaleWorkflow for ID {$wId}", ['exception' => $e]);
|
||||
|
@@ -14,6 +14,7 @@ namespace Chill\MainBundle\Service\Workflow;
|
||||
use Chill\MainBundle\Repository\Workflow\EntityWorkflowRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\Clock\ClockInterface;
|
||||
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
|
||||
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
|
||||
use Symfony\Component\Workflow\Registry;
|
||||
@@ -21,13 +22,29 @@ use Symfony\Component\Workflow\Registry;
|
||||
#[AsMessageHandler]
|
||||
class CancelStaleWorkflowHandler
|
||||
{
|
||||
public function __construct(private readonly EntityWorkflowRepository $workflowRepository, private readonly Registry $registry, private EntityManagerInterface $em, private LoggerInterface $logger) {}
|
||||
public const string KEEP_INTERVAL = 'P90D';
|
||||
|
||||
public function __invoke(CancelStaleWorkflow $message): void
|
||||
public function __construct(private readonly EntityWorkflowRepository $workflowRepository, private readonly Registry $registry, private readonly EntityManagerInterface $em, private readonly LoggerInterface $logger, private readonly ClockInterface $clock) {}
|
||||
|
||||
public function __invoke(CancelStaleWorkflowMessage $message): void
|
||||
{
|
||||
$workflowId = $message->getWorkflowId();
|
||||
|
||||
$olderThanDate = $this->clock->now()->sub(new \DateInterval(self::KEEP_INTERVAL));
|
||||
$staleWorkflows = $this->workflowRepository->findWorkflowsWithoutFinalStepAndOlderThan($olderThanDate);
|
||||
|
||||
$workflow = $this->workflowRepository->find($workflowId);
|
||||
|
||||
if (in_array($workflow, $staleWorkflows, true)) {
|
||||
$this->logger->alert('Workflow has transitioned in the meantime.', [$workflowId]);
|
||||
return;
|
||||
}
|
||||
|
||||
if (null === $workflow) {
|
||||
$this->logger->alert('Workflow was not found!', [$workflowId]);
|
||||
return;
|
||||
}
|
||||
|
||||
$workflowComponent = $this->registry->get($workflow, $workflow->getWorkflowName());
|
||||
$metadataStore = $workflowComponent->getMetadataStore();
|
||||
$transitions = $workflowComponent->getEnabledTransitions($workflow);
|
||||
@@ -44,15 +61,15 @@ class CancelStaleWorkflowHandler
|
||||
$isFinalPositive = $metadataStore->getMetadata('isFinalPositive', $transition);
|
||||
|
||||
if ($isFinal && !$isFinalPositive) {
|
||||
$workflowComponent->apply($workflow, 'annule');
|
||||
$this->logger->info(sprintf('EntityWorkflow %d has been transitioned.', $workflowId));
|
||||
$workflowComponent->apply($workflow, $transition->getName());
|
||||
$this->logger->info('EntityWorkflow has been cancelled automatically.', [$workflowId]);
|
||||
$transitionApplied = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$transitionApplied) {
|
||||
$this->logger->error(sprintf('No valid transition found for EntityWorkflow %d.', $workflowId));
|
||||
$this->logger->error('No valid transition found for EntityWorkflow %d.', [$workflowId]);
|
||||
throw new UnrecoverableMessageHandlingException(sprintf('No valid transition found for EntityWorkflow %d.', $workflowId));
|
||||
}
|
||||
}
|
||||
|
@@ -11,7 +11,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Chill\MainBundle\Service\Workflow;
|
||||
|
||||
class CancelStaleWorkflow
|
||||
class CancelStaleWorkflowMessage
|
||||
{
|
||||
public function __construct(public int $workflowId) {}
|
||||
|
Reference in New Issue
Block a user