Add logger messages for possible debugging purposes

This commit is contained in:
Julie Lenaerts 2024-08-12 12:05:03 +02:00 committed by Julien Fastré
parent 29fec50515
commit 6001bb6447
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB

View File

@ -6,22 +6,22 @@ use Chill\MainBundle\Cron\CronJobInterface;
use Chill\MainBundle\Entity\CronJobExecution; use Chill\MainBundle\Entity\CronJobExecution;
use Chill\MainBundle\Repository\Workflow\EntityWorkflowRepository; use Chill\MainBundle\Repository\Workflow\EntityWorkflowRepository;
use DateInterval; use DateInterval;
use Doctrine\DBAL\Connection;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Symfony\Component\Clock\ClockInterface; use Symfony\Component\Clock\ClockInterface;
use Symfony\Component\Messenger\MessageBusInterface; use Symfony\Component\Messenger\MessageBusInterface;
class CancelStaleWorkflowCronJob implements CronJobInterface class CancelStaleWorkflowCronJob implements CronJobInterface
{ {
public const KEY = 'remove-stale-workflow'; public const string KEY = 'remove-stale-workflow';
public const KEEP_INTERVAL = 'P90D'; public const string KEEP_INTERVAL = 'P90D';
private const LAST_CANCELED_WORKFLOW = 'last-canceled-workflow-id'; private const string LAST_CANCELED_WORKFLOW = 'last-canceled-workflow-id';
public function __construct(private readonly EntityWorkflowRepository $workflowRepository, public function __construct(private readonly EntityWorkflowRepository $workflowRepository,
private readonly ClockInterface $clock, private readonly ClockInterface $clock,
private readonly MessageBusInterface $messageBus, private readonly MessageBusInterface $messageBus,
private readonly LoggerInterface $logger,
) )
{ {
} }
@ -38,18 +38,26 @@ class CancelStaleWorkflowCronJob implements CronJobInterface
public function run(array $lastExecutionData): ?array public function run(array $lastExecutionData): ?array
{ {
$this->logger->info('Cronjob started: Canceling stale workflows.');
$olderThanDate = $this->clock->now()->sub(new DateInterval(self::KEEP_INTERVAL)); $olderThanDate = $this->clock->now()->sub(new DateInterval(self::KEEP_INTERVAL));
$staleWorkflowIds = $this->workflowRepository->findWorkflowsWithoutFinalStepAndOlderThan($olderThanDate); $staleWorkflowIds = $this->workflowRepository->findWorkflowsWithoutFinalStepAndOlderThan($olderThanDate);
$lastCanceled = self::LAST_CANCELED_WORKFLOW; $lastCanceled = self::LAST_CANCELED_WORKFLOW;
$processedCount = 0;
foreach ($staleWorkflowIds as $wId) { foreach ($staleWorkflowIds as $wId) {
$this->messageBus->dispatch(new CancelStaleWorkflow($wId)); try {
$lastCanceled = $wId; $this->messageBus->dispatch(new CancelStaleWorkflow($wId));
$lastCanceled = $wId;
$processedCount++;
} catch (\Exception $e) {
$this->logger->error("Failed to dispatch CancelStaleWorkflow for ID {$wId}", ['exception' => $e]);
continue;
}
} }
$this->logger->info("Cronjob completed: {$processedCount} workflows processed.");
return [self::LAST_CANCELED_WORKFLOW => $lastCanceled]; return [self::LAST_CANCELED_WORKFLOW => $lastCanceled];
} }
} }