chill-bundles/src/Bundle/ChillMainBundle/Service/Workflow/CancelStaleWorkflowCronJob.php

71 lines
2.3 KiB
PHP

<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Service\Workflow;
use Chill\MainBundle\Cron\CronJobInterface;
use Chill\MainBundle\Entity\CronJobExecution;
use Chill\MainBundle\Repository\Workflow\EntityWorkflowRepository;
use Psr\Log\LoggerInterface;
use Symfony\Component\Clock\ClockInterface;
use Symfony\Component\Messenger\MessageBusInterface;
class CancelStaleWorkflowCronJob implements CronJobInterface
{
public const string KEY = 'remove-stale-workflow';
public const string KEEP_INTERVAL = 'P90D';
private const string LAST_CANCELED_WORKFLOW = 'last-canceled-workflow-id';
public function __construct(
private readonly EntityWorkflowRepository $workflowRepository,
private readonly ClockInterface $clock,
private readonly MessageBusInterface $messageBus,
private readonly LoggerInterface $logger,
) {}
public function canRun(?CronJobExecution $cronJobExecution): bool
{
return $this->clock->now() >= $cronJobExecution->getLastEnd()->add(new \DateInterval('P1D'));
}
public function getKey(): string
{
return self::KEY;
}
public function run(array $lastExecutionData): ?array
{
$this->logger->info('Cronjob started: Canceling stale workflows.');
$olderThanDate = $this->clock->now()->sub(new \DateInterval(self::KEEP_INTERVAL));
$staleWorkflowIds = $this->workflowRepository->findWorkflowsWithoutFinalStepAndOlderThan($olderThanDate);
$lastCanceled = self::LAST_CANCELED_WORKFLOW;
$processedCount = 0;
foreach ($staleWorkflowIds as $wId) {
try {
$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]);
continue;
}
}
$this->logger->info("Cronjob completed: {$processedCount} workflows processed.");
return [self::LAST_CANCELED_WORKFLOW => $lastCanceled];
}
}