mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-29 11:03:50 +00:00
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:
@@ -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.");
|
||||
|
Reference in New Issue
Block a user