Add functionality to delete old versions of documents

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.
This commit is contained in:
2024-07-15 15:54:26 +02:00
parent 67d24cb951
commit c38f7c1179
10 changed files with 476 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
<?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];
}
}

View File

@@ -0,0 +1,19 @@
<?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;
final readonly class RemoveOldVersionMessage
{
public function __construct(
public int $storedObjectVersionId
) {}
}

View File

@@ -0,0 +1,54 @@
<?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\Exception\StoredObjectManagerException;
use Chill\DocStoreBundle\Repository\StoredObjectVersionRepository;
use Chill\DocStoreBundle\Service\StoredObjectManagerInterface;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
final readonly class RemoveOldVersionMessageHandler implements MessageHandlerInterface
{
private const LOG_PREFIX = '[RemoveOldVersionMessageHandler] ';
public function __construct(
private StoredObjectVersionRepository $storedObjectVersionRepository,
private LoggerInterface $logger,
private EntityManagerInterface $entityManager,
private StoredObjectManagerInterface $storedObjectManager,
) {}
/**
* @throws StoredObjectManagerException
*/
public function __invoke(RemoveOldVersionMessage $message): void
{
$this->logger->info(self::LOG_PREFIX.'Received one message', ['storedObjectVersionId' => $message->storedObjectVersionId]);
$storedObjectVersion = $this->storedObjectVersionRepository->find($message->storedObjectVersionId);
if (null === $storedObjectVersion) {
$this->logger->error(self::LOG_PREFIX.'StoredObjectVersion not found in database', ['storedObjectVersionId' => $message->storedObjectVersionId]);
throw new \RuntimeException('StoredObjectVersion not found with id '.$message->storedObjectVersionId);
}
$this->storedObjectManager->delete($storedObjectVersion);
$this->entityManager->remove($storedObjectVersion);
$this->entityManager->flush();
// clear the entity manager for future usage
$this->entityManager->clear();
}
}

View File

@@ -217,6 +217,23 @@ final class StoredObjectManager implements StoredObjectManagerInterface
return $version;
}
/**
* @throws StoredObjectManagerException
*/
public function delete(StoredObjectVersion $storedObjectVersion): void
{
$signedUrl = $this->tempUrlGenerator->generate('DELETE', $storedObjectVersion->getFilename());
try {
$response = $this->client->request('DELETE', $signedUrl->url);
if (Response::HTTP_NO_CONTENT !== $response->getStatusCode()) {
throw StoredObjectManagerException::invalidStatusCode($response->getStatusCode());
}
} catch (TransportExceptionInterface $exception) {
throw StoredObjectManagerException::errorDuringHttpRequest($exception);
}
}
public function clearCache(): void
{
$this->inMemory = [];

View File

@@ -51,6 +51,11 @@ interface StoredObjectManagerInterface
*/
public function write(StoredObject $document, string $clearContent, ?string $contentType = null): StoredObjectVersion;
/**
* @throws StoredObjectManagerException
*/
public function delete(StoredObjectVersion $storedObjectVersion): void;
/**
* return or compute the etag for the document.
*