Add handling and cleanup for expired export generations

Implemented a new cron job to identify and process expired export generations, dispatching messages for their removal. Added corresponding message handler, tests, and configuration updates to handle and orchestrate the deletion workflow.
This commit is contained in:
Julien Fastré 2025-05-26 17:46:46 +02:00
parent 3a016aa12a
commit c40e790425
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
8 changed files with 334 additions and 0 deletions

View File

@ -63,6 +63,7 @@ framework:
'Chill\MainBundle\Workflow\Messenger\PostPublicViewMessage': async
'Chill\MainBundle\Service\Workflow\CancelStaleWorkflowMessage': async
'Chill\MainBundle\Export\Messenger\ExportRequestGenerationMessage': priority
'Chill\MainBundle\Export\Messenger\RemoveExportGenerationMessage': async
# end of routes added by chill-bundles recipes
# Route your messages to the transports
# 'App\Message\YourMessage': async

View File

@ -0,0 +1,52 @@
<?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\Export\Cronjob;
use Chill\MainBundle\Cron\CronJobInterface;
use Chill\MainBundle\Entity\CronJobExecution;
use Chill\MainBundle\Export\Messenger\RemoveExportGenerationMessage;
use Chill\MainBundle\Repository\ExportGenerationRepository;
use Symfony\Component\Clock\ClockInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;
final readonly class RemoveExpiredExportGenerationCronJob implements CronJobInterface
{
public const KEY = 'remove-expired-export-generation';
public function __construct(private ClockInterface $clock, private ExportGenerationRepository $exportGenerationRepository, private MessageBusInterface $messageBus) {}
public function canRun(?CronJobExecution $cronJobExecution): bool
{
if (null === $cronJobExecution) {
return true;
}
return $cronJobExecution->getLastStart()->getTimestamp() < $this->clock->now()->sub(new \DateInterval('PT24H'))->getTimestamp();
}
public function getKey(): string
{
return self::KEY;
}
public function run(array $lastExecutionData): ?array
{
$now = $this->clock->now();
foreach ($this->exportGenerationRepository->findExpiredExportGeneration($now) as $exportGeneration) {
$this->messageBus->dispatch(new Envelope(new RemoveExportGenerationMessage($exportGeneration)));
}
return ['last-deletion' => $now->getTimestamp()];
}
}

View File

@ -0,0 +1,24 @@
<?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\Export\Messenger;
use Chill\MainBundle\Entity\ExportGeneration;
final readonly class RemoveExportGenerationMessage
{
public string $exportGenerationId;
public function __construct(ExportGeneration $exportGeneration)
{
$this->exportGenerationId = $exportGeneration->getId()->toString();
}
}

View File

@ -0,0 +1,49 @@
<?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\Export\Messenger;
use Chill\MainBundle\Repository\ExportGenerationRepository;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Clock\ClockInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
#[AsMessageHandler]
class RemoveExportGenerationMessageHandler implements MessageHandlerInterface
{
private const LOG_PREFIX = '[RemoveExportGenerationMessageHandler] ';
public function __construct(
private ExportGenerationRepository $exportGenerationRepository,
private EntityManagerInterface $entityManager,
private LoggerInterface $logger,
private ClockInterface $clock,
) {}
public function __invoke(RemoveExportGenerationMessage $message): void
{
$exportGeneration = $this->exportGenerationRepository->find($message->exportGenerationId);
if (null === $exportGeneration) {
$this->logger->error(self::LOG_PREFIX.'ExportGeneration not found');
throw new UnrecoverableMessageHandlingException(self::LOG_PREFIX.'ExportGeneration not found');
}
$storedObject = $exportGeneration->getStoredObject();
$storedObject->setDeleteAt($this->clock->now());
$this->entityManager->remove($exportGeneration);
$this->entityManager->flush();
}
}

View File

@ -73,4 +73,13 @@ class ExportGenerationRepository extends ServiceEntityRepository implements Asso
->getQuery()
->getResult();
}
public function findExpiredExportGeneration(\DateTimeImmutable $atDate): iterable
{
return $this->createQueryBuilder('e')
->where('e.deleteAt < :atDate')
->setParameter('atDate', $atDate)
->getQuery()
->toIterable();
}
}

View File

@ -0,0 +1,118 @@
<?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\Tests\Export\Cronjob;
use Chill\MainBundle\Entity\CronJobExecution;
use Chill\MainBundle\Entity\ExportGeneration;
use Chill\MainBundle\Export\Cronjob\RemoveExpiredExportGenerationCronJob;
use Chill\MainBundle\Export\Messenger\RemoveExportGenerationMessage;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Argument;
use Symfony\Component\Clock\MockClock;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;
use Chill\MainBundle\Repository\ExportGenerationRepository;
/**
* @internal
*
* @coversNothing
*/
class RemoveExpiredExportGenerationCronJobTest extends TestCase
{
use ProphecyTrait;
public function testCanRunReturnsTrueWhenLastExecutionIsNull()
{
$clock = new MockClock(new \DateTimeImmutable('2024-06-25 10:00:00'));
$repo = $this->prophesize(ExportGenerationRepository::class);
$bus = $this->prophesize(MessageBusInterface::class);
$cronJob = new RemoveExpiredExportGenerationCronJob(
$clock,
$repo->reveal(),
$bus->reveal()
);
$this->assertTrue($cronJob->canRun(null));
}
public function testCanRunReturnsTrueWhenLastStartIsOlderThan24Hours()
{
$clock = new MockClock(new \DateTimeImmutable('2024-06-25 10:00:00'));
$repo = $this->prophesize(ExportGenerationRepository::class);
$bus = $this->prophesize(MessageBusInterface::class);
$cronJob = new RemoveExpiredExportGenerationCronJob(
$clock,
$repo->reveal(),
$bus->reveal()
);
$execution = new CronJobExecution('remove-expired-export-generation');
$execution->setLastStart(new \DateTimeImmutable('2024-06-24 09:59:59'));
$this->assertTrue($cronJob->canRun($execution));
}
public function testCanRunReturnsFalseWhenLastStartIsWithin24Hours()
{
$clock = new MockClock(new \DateTimeImmutable('2024-06-25 10:00:00'));
$repo = $this->prophesize(ExportGenerationRepository::class);
$bus = $this->prophesize(MessageBusInterface::class);
$cronJob = new RemoveExpiredExportGenerationCronJob(
$clock,
$repo->reveal(),
$bus->reveal()
);
$execution = new CronJobExecution('remove-expired-export-generation');
$execution->setLastStart(new \DateTimeImmutable('2024-06-24 10:01:00'));
$this->assertFalse($cronJob->canRun($execution));
}
public function testRunDispatchesMessagesForExpiredExportsAndReturnsLastDeletion()
{
$clock = new MockClock(new \DateTimeImmutable('2024-06-25 11:21:00'));
$repo = $this->prophesize(ExportGenerationRepository::class);
$bus = $this->prophesize(MessageBusInterface::class);
$expiredExports = [
new ExportGeneration('dummy', []),
];
$repo->findExpiredExportGeneration(Argument::that(function ($dateTime) use ($clock) {
// Ensure the repository is called with the current clock time
return $dateTime instanceof \DateTimeImmutable
&& $dateTime->getTimestamp() === $clock->now()->getTimestamp();
}))->willReturn($expiredExports);
// Expect one RemoveExportGenerationMessage for each expired export
$bus->dispatch(Argument::that(fn (Envelope $envelope) => $envelope->getMessage() instanceof RemoveExportGenerationMessage))
->shouldBeCalledTimes(1)
->will(fn ($args) => $args[0]);
$cronJob = new RemoveExpiredExportGenerationCronJob(
$clock,
$repo->reveal(),
$bus->reveal()
);
$result = $cronJob->run([]);
$this->assertIsArray($result);
$this->assertEquals(['last-deletion' => $clock->now()->getTimestamp()], $result);
}
}

View File

@ -0,0 +1,76 @@
<?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\Tests\Export\Messenger;
use Chill\MainBundle\Entity\ExportGeneration;
use Chill\MainBundle\Export\Messenger\RemoveExportGenerationMessage;
use Chill\MainBundle\Export\Messenger\RemoveExportGenerationMessageHandler;
use Chill\MainBundle\Repository\ExportGenerationRepository;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Psr\Log\NullLogger;
use Symfony\Component\Clock\MockClock;
/**
* @internal
*
* @coversNothing
*/
class RemoveExportGenerationMessageHandlerTest extends TestCase
{
use ProphecyTrait;
public function testInvokeUpdatesDeleteAtAndRemovesAndFlushes()
{
// Arrange
// 1. Create a MockClock at a fixed point in time
$now = new \DateTimeImmutable('2024-06-01T12:00:00');
$clock = new MockClock($now);
// 2. Create an ExportGeneration entity with a stored object
$exportGeneration = new ExportGeneration('test-alias', ['foo' => 'bar']);
$storedObject = $exportGeneration->getStoredObject();
// 3. Mock ExportGenerationRepository to return the ExportGeneration
$exportGenerationRepository = $this->prophesize(ExportGenerationRepository::class);
$exportGenerationRepository
->find($exportGeneration->getId())
->willReturn($exportGeneration);
// 4. Mock EntityManagerInterface and set expectations
$entityManager = $this->prophesize(EntityManagerInterface::class);
$entityManager->remove($exportGeneration)->shouldBeCalled();
$entityManager->flush()->shouldBeCalled();
// 6. Create message
$message = new RemoveExportGenerationMessage($exportGeneration);
// 7. Handler instantiation
$handler = new RemoveExportGenerationMessageHandler(
$exportGenerationRepository->reveal(),
$entityManager->reveal(),
new NullLogger(),
$clock
);
// Pre-condition: deleteAt not set.
$this->assertNull($storedObject->getDeleteAt());
// Act
$handler->__invoke($message);
// Assert
$this->assertEquals($now, $storedObject->getDeleteAt(), 'deleteAt of stored object was updated');
}
}

View File

@ -6,8 +6,13 @@ services:
Chill\MainBundle\Export\Helper\:
resource: '../../Export/Helper'
Chill\MainBundle\Export\Cronjob\:
resource: '../../Export/Cronjob'
Chill\MainBundle\Export\Messenger\ExportRequestGenerationMessageHandler: ~
Chill\MainBundle\Export\Messenger\RemoveExportGenerationMessageHandler: ~
Chill\MainBundle\Export\Messenger\OnExportGenerationFails: ~
Chill\MainBundle\Export\ExportFormHelper: ~