mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-07-24 17:47:43 +00:00
This commit introduces a feature that automatically deletes old versions of StoredObjects in the Chill application. A cron job, "RemoveOldVersionCronJob", has been implemented to delete versions older than 90 days. A message handler, "RemoveOldVersionMessageHandler", has been added to handle deletion requests. Furthermore, unit tests for the new functionality have been provided.
61 lines
1.8 KiB
PHP
61 lines
1.8 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\DocStoreBundle\Service\StoredObjectCleaner;
|
|
|
|
use Chill\DocStoreBundle\Repository\StoredObjectVersionRepository;
|
|
use Chill\MainBundle\Cron\CronJobInterface;
|
|
use Chill\MainBundle\Entity\CronJobExecution;
|
|
use Symfony\Component\Clock\ClockInterface;
|
|
use Symfony\Component\Messenger\MessageBusInterface;
|
|
|
|
final readonly class RemoveOldVersionCronJob implements CronJobInterface
|
|
{
|
|
public const KEY = 'remove-old-stored-object-version';
|
|
|
|
private const LAST_DELETED_KEY = 'last-deleted-stored-object-version-id';
|
|
|
|
public const KEEP_INTERVAL = 'P90D';
|
|
|
|
public function __construct(
|
|
private ClockInterface $clock,
|
|
private MessageBusInterface $messageBus,
|
|
private StoredObjectVersionRepository $storedObjectVersionRepository,
|
|
) {}
|
|
|
|
public function canRun(?CronJobExecution $cronJobExecution): bool
|
|
{
|
|
if (null === $cronJobExecution) {
|
|
return true;
|
|
}
|
|
|
|
return $this->clock->now() >= $cronJobExecution->getLastEnd()->add(new \DateInterval('P1D'));
|
|
}
|
|
|
|
public function getKey(): string
|
|
{
|
|
return self::KEY;
|
|
}
|
|
|
|
public function run(array $lastExecutionData): ?array
|
|
{
|
|
$deleteBeforeDate = $this->clock->now()->sub(new \DateInterval(self::KEEP_INTERVAL));
|
|
$maxDeleted = $lastExecutionData[self::LAST_DELETED_KEY] ?? 0;
|
|
|
|
foreach ($this->storedObjectVersionRepository->findIdsByVersionsOlderThanDateAndNotLastVersion($deleteBeforeDate) as $id) {
|
|
$this->messageBus->dispatch(new RemoveOldVersionMessage($id));
|
|
$maxDeleted = max($maxDeleted, $id);
|
|
}
|
|
|
|
return [self::LAST_DELETED_KEY => $maxDeleted];
|
|
}
|
|
}
|