mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
Add cronjob and repository method to find and cancel stale workflows every other day
This commit is contained in:
parent
34edb02cd0
commit
29fec50515
@ -198,6 +198,19 @@ class EntityWorkflowRepository implements ObjectRepository
|
||||
return $this->repository->findOneBy($criteria);
|
||||
}
|
||||
|
||||
public function findWorkflowsWithoutFinalStepAndOlderThan(\DateTimeImmutable $olderThanDate)
|
||||
{
|
||||
$qb = $this->repository->createQueryBuilder('sw');
|
||||
|
||||
$qb->select('sw.id')
|
||||
->where('NOT EXISTS (SELECT 1 FROM chill_main_entity_workflow_step ews WHERE ews.isFinal = TRUE AND ews.entityWorkflow = sw.id)')
|
||||
->andWhere(':olderThanDate > ALL (SELECT ews.transitionAt FROM chill_main_entity_workflow_step ews WHERE ews.transitionAt IS NOT NULL AND ews.entityWorkflow = sw.id)')
|
||||
->andWhere('sw.createdAt < :olderThanDate')
|
||||
->setParameter('olderThanDate', $olderThanDate);
|
||||
|
||||
return $qb->getQuery()->getResult();
|
||||
}
|
||||
|
||||
public function getClassName(): string
|
||||
{
|
||||
return EntityWorkflow::class;
|
||||
|
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\MainBundle\Service\Workflow;
|
||||
|
||||
use Chill\MainBundle\Cron\CronJobInterface;
|
||||
use Chill\MainBundle\Entity\CronJobExecution;
|
||||
use Chill\MainBundle\Repository\Workflow\EntityWorkflowRepository;
|
||||
use DateInterval;
|
||||
use Doctrine\DBAL\Connection;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\Clock\ClockInterface;
|
||||
use Symfony\Component\Messenger\MessageBusInterface;
|
||||
|
||||
class CancelStaleWorkflowCronJob implements CronJobInterface
|
||||
{
|
||||
public const KEY = 'remove-stale-workflow';
|
||||
|
||||
public const KEEP_INTERVAL = 'P90D';
|
||||
|
||||
private const LAST_CANCELED_WORKFLOW = 'last-canceled-workflow-id';
|
||||
|
||||
public function __construct(private readonly EntityWorkflowRepository $workflowRepository,
|
||||
private readonly ClockInterface $clock,
|
||||
private readonly MessageBusInterface $messageBus,
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
$olderThanDate = $this->clock->now()->sub(new DateInterval(self::KEEP_INTERVAL));
|
||||
|
||||
$staleWorkflowIds = $this->workflowRepository->findWorkflowsWithoutFinalStepAndOlderThan($olderThanDate);
|
||||
|
||||
$lastCanceled = self::LAST_CANCELED_WORKFLOW;
|
||||
|
||||
foreach ($staleWorkflowIds as $wId) {
|
||||
$this->messageBus->dispatch(new CancelStaleWorkflow($wId));
|
||||
$lastCanceled = $wId;
|
||||
}
|
||||
|
||||
return [self::LAST_CANCELED_WORKFLOW => $lastCanceled];
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user