Fix Canceling of stale workflow cronjob

Refactor workflow cancellation logic to encapsulate transition checks in a dedicated method, and update CronJob handling to use entity workflows instead of IDs. Enhance test coverage to ensure proper handling and instantiate mocks for EntityManagerInterface.
This commit is contained in:
2024-10-21 17:39:31 +02:00
parent 1d708a481d
commit 527cf23d4f
6 changed files with 73 additions and 29 deletions

View File

@@ -14,6 +14,7 @@ namespace Chill\MainBundle\Service\Workflow;
use Chill\MainBundle\Cron\CronJobInterface;
use Chill\MainBundle\Entity\CronJobExecution;
use Chill\MainBundle\Repository\Workflow\EntityWorkflowRepository;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Clock\ClockInterface;
use Symfony\Component\Messenger\MessageBusInterface;
@@ -31,6 +32,7 @@ class CancelStaleWorkflowCronJob implements CronJobInterface
private readonly ClockInterface $clock,
private readonly MessageBusInterface $messageBus,
private readonly LoggerInterface $logger,
private readonly EntityManagerInterface $entityManager,
) {}
public function canRun(?CronJobExecution $cronJobExecution): bool
@@ -48,19 +50,23 @@ class CancelStaleWorkflowCronJob implements CronJobInterface
$this->logger->info('Cronjob started: Canceling stale workflows.');
$olderThanDate = $this->clock->now()->sub(new \DateInterval(self::KEEP_INTERVAL));
$staleWorkflowIds = $this->workflowRepository->findWorkflowsWithoutFinalStepAndOlderThan($olderThanDate);
$staleEntityWorkflows = $this->workflowRepository->findWorkflowsWithoutFinalStepAndOlderThan($olderThanDate);
$lastCanceled = $lastExecutionData[self::LAST_CANCELED_WORKFLOW] ?? 0;
$processedCount = 0;
foreach ($staleWorkflowIds as $wId) {
foreach ($staleEntityWorkflows as $staleEntityWorkflow) {
try {
$this->messageBus->dispatch(new CancelStaleWorkflowMessage($wId));
$lastCanceled = max($wId, $lastCanceled);
$this->messageBus->dispatch(new CancelStaleWorkflowMessage($staleEntityWorkflow->getId()));
$lastCanceled = max($staleEntityWorkflow->getId(), $lastCanceled);
++$processedCount;
} catch (\Exception $e) {
$this->logger->error("Failed to dispatch CancelStaleWorkflow for ID {$wId}", ['exception' => $e]);
$this->logger->error('Failed to dispatch CancelStaleWorkflow', ['exception' => $e, 'entityWorkflowId' => $staleEntityWorkflow->getId()]);
continue;
}
if (0 === $processedCount % 10) {
$this->entityManager->clear();
}
}
$this->logger->info("Cronjob completed: {$processedCount} workflows processed.");

View File

@@ -18,7 +18,9 @@ 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\Metadata\MetadataStoreInterface;
use Symfony\Component\Workflow\Registry;
use Symfony\Component\Workflow\Transition;
#[AsMessageHandler]
final readonly class CancelStaleWorkflowHandler
@@ -57,10 +59,7 @@ final readonly class CancelStaleWorkflowHandler
$wasInInitialPosition = 'initial' === $workflow->getStep();
foreach ($transitions as $transition) {
$isFinal = $metadataStore->getMetadata('isFinal', $transition);
$isFinalPositive = $metadataStore->getMetadata('isFinalPositive', $transition);
if ($isFinal && !$isFinalPositive) {
if ($this->willTransitionLeadToFinalNegative($transition, $metadataStore)) {
$dto = new WorkflowTransitionContextDTO($workflow);
$workflowComponent->apply($workflow, $transition->getName(), [
'context' => $dto,
@@ -85,4 +84,16 @@ final readonly class CancelStaleWorkflowHandler
$this->em->flush();
}
private function willTransitionLeadToFinalNegative(Transition $transition, MetadataStoreInterface $metadataStore): bool
{
foreach ($transition->getTos() as $place) {
$metadata = $metadataStore->getPlaceMetadata($place);
if (($metadata['isFinal'] ?? true) && false === ($metadata['isFinalPositive'] ?? true)) {
return true;
}
}
return false;
}
}