|
@@ -2,15 +2,16 @@
|
|
|
|
|
|
|
|
namespace App\Service\Cron\Job;
|
|
namespace App\Service\Cron\Job;
|
|
|
|
|
|
|
|
-use App\Repository\Access\AccessRepository;
|
|
|
|
|
|
|
+use App\Entity\Core\File;
|
|
|
|
|
+use App\Enum\Core\FileHostEnum;
|
|
|
|
|
+use App\Enum\Core\FileStatusEnum;
|
|
|
use App\Repository\Core\FileRepository;
|
|
use App\Repository\Core\FileRepository;
|
|
|
use App\Service\Cron\BaseCronJob;
|
|
use App\Service\Cron\BaseCronJob;
|
|
|
use App\Service\Cron\CronjobInterface;
|
|
use App\Service\Cron\CronjobInterface;
|
|
|
use App\Service\File\Storage\LocalStorage;
|
|
use App\Service\File\Storage\LocalStorage;
|
|
|
use App\Service\Utils\DatesUtils;
|
|
use App\Service\Utils\DatesUtils;
|
|
|
-use Doctrine\Common\Collections\Collection;
|
|
|
|
|
-use Doctrine\Common\Collections\Criteria;
|
|
|
|
|
-use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
|
|
|
+use Doctrine\DBAL\Connection;
|
|
|
|
|
+use Doctrine\ORM\QueryBuilder;
|
|
|
use Exception;
|
|
use Exception;
|
|
|
use JetBrains\PhpStorm\Pure;
|
|
use JetBrains\PhpStorm\Pure;
|
|
|
|
|
|
|
@@ -24,29 +25,17 @@ class CleanTempFiles extends BaseCronJob implements CronjobInterface
|
|
|
/**
|
|
/**
|
|
|
* Delay before removing a temporary file (in days)
|
|
* Delay before removing a temporary file (in days)
|
|
|
*/
|
|
*/
|
|
|
- private const DELETE_OLDER_THAN = 7;
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * ID of the Access who author the file deletion
|
|
|
|
|
- */
|
|
|
|
|
- private const AUTHOR = 10984;
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * Delay before deleting the record of the temporary file from the DB
|
|
|
|
|
- */
|
|
|
|
|
- private const PURGE_RECORDS_OLDER_THAN = 365;
|
|
|
|
|
|
|
+ private const DELETE_OLDER_THAN = 60;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
|
+ * @param Connection $connection
|
|
|
* @param FileRepository $fileRepository
|
|
* @param FileRepository $fileRepository
|
|
|
- * @param EntityManagerInterface $em
|
|
|
|
|
- * @param AccessRepository $accessRepository
|
|
|
|
|
* @param LocalStorage $storage
|
|
* @param LocalStorage $storage
|
|
|
*/
|
|
*/
|
|
|
#[Pure]
|
|
#[Pure]
|
|
|
public function __construct(
|
|
public function __construct(
|
|
|
|
|
+ private Connection $connection,
|
|
|
private FileRepository $fileRepository,
|
|
private FileRepository $fileRepository,
|
|
|
- private EntityManagerInterface $em,
|
|
|
|
|
- private AccessRepository $accessRepository,
|
|
|
|
|
private LocalStorage $storage
|
|
private LocalStorage $storage
|
|
|
) {
|
|
) {
|
|
|
parent::__construct();
|
|
parent::__construct();
|
|
@@ -58,7 +47,8 @@ class CleanTempFiles extends BaseCronJob implements CronjobInterface
|
|
|
*/
|
|
*/
|
|
|
public function preview(): void
|
|
public function preview(): void
|
|
|
{
|
|
{
|
|
|
- $files = $this->listFilesToDelete();
|
|
|
|
|
|
|
+ $maxDate = DatesUtils::new();
|
|
|
|
|
+ $files = $this->listFilesToDelete($maxDate);
|
|
|
$total = count($files);
|
|
$total = count($files);
|
|
|
$this->ui->print($total . " temporary files to be removed");
|
|
$this->ui->print($total . " temporary files to be removed");
|
|
|
$this->ui->print("> Printing the first and last 50 :");
|
|
$this->ui->print("> Printing the first and last 50 :");
|
|
@@ -73,7 +63,7 @@ class CleanTempFiles extends BaseCronJob implements CronjobInterface
|
|
|
$this->ui->print(' * ' . $file->getPath());
|
|
$this->ui->print(' * ' . $file->getPath());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- $this->purgeDb(false);
|
|
|
|
|
|
|
+ $this->purgeDb($maxDate, false);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -82,56 +72,75 @@ class CleanTempFiles extends BaseCronJob implements CronjobInterface
|
|
|
*/
|
|
*/
|
|
|
public function execute(): void
|
|
public function execute(): void
|
|
|
{
|
|
{
|
|
|
- $files = $this->listFilesToDelete();
|
|
|
|
|
-
|
|
|
|
|
|
|
+ $maxDate = DatesUtils::new();
|
|
|
|
|
+ $files = $this->listFilesToDelete($maxDate);
|
|
|
$this->deleteFiles($files);
|
|
$this->deleteFiles($files);
|
|
|
-
|
|
|
|
|
- $this->purgeDb();
|
|
|
|
|
|
|
+ $this->purgeDb($maxDate);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* List the files to delete in the DB
|
|
* List the files to delete in the DB
|
|
|
- *
|
|
|
|
|
- * @return Collection
|
|
|
|
|
|
|
+ * @param \DateTime $maxDate
|
|
|
|
|
+ * @return array<File>
|
|
|
* @throws Exception
|
|
* @throws Exception
|
|
|
*/
|
|
*/
|
|
|
- protected function listFilesToDelete(): Collection
|
|
|
|
|
|
|
+ protected function listFilesToDelete(\DateTime $maxDate): array
|
|
|
{
|
|
{
|
|
|
|
|
+ $this->ui->print('List temporary files created before ' . $maxDate->format('c'));
|
|
|
|
|
|
|
|
- $maxDate = DatesUtils::new();
|
|
|
|
|
- $maxDate->sub(new \DateInterval('P' . self::DELETE_OLDER_THAN . 'D'));
|
|
|
|
|
|
|
+ $queryBuilder = $this->fileRepository->createQueryBuilder('f');
|
|
|
|
|
+ $queryBuilder->select();
|
|
|
|
|
+ $this->getQueryConditions($queryBuilder, $maxDate);
|
|
|
|
|
+ return $queryBuilder->getQuery()->getResult();
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- $this->ui->print('List temporary files created before ' . $maxDate->format('c'));
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Purge File table and returns the number of deleted records
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param \DateTime $maxDate
|
|
|
|
|
+ * @param bool $commit
|
|
|
|
|
+ * @return int
|
|
|
|
|
+ */
|
|
|
|
|
+ protected function purgeDb(\DateTime $maxDate, bool $commit = true, ): void {
|
|
|
|
|
+ $this->ui->print('Purge DB from records of temporary files create before ' . $maxDate->format('c'));
|
|
|
|
|
|
|
|
- $criteria = new Criteria();
|
|
|
|
|
- $criteria->where(
|
|
|
|
|
- Criteria::expr()->andX(
|
|
|
|
|
- Criteria::expr()->eq('isTemporaryFile', true),
|
|
|
|
|
- Criteria::expr()->orX(
|
|
|
|
|
- Criteria::expr()->lt('createDate', $maxDate),
|
|
|
|
|
- Criteria::expr()->isNull('createDate')
|
|
|
|
|
- )
|
|
|
|
|
- )
|
|
|
|
|
- );
|
|
|
|
|
|
|
+ $this->connection->beginTransaction();
|
|
|
|
|
+ $this->connection->setAutoCommit(false);
|
|
|
|
|
|
|
|
- return $this->fileRepository->matching($criteria);
|
|
|
|
|
|
|
+ $purged = 0;
|
|
|
|
|
+
|
|
|
|
|
+ try{
|
|
|
|
|
+ $purged += $this->purgeFile($maxDate);
|
|
|
|
|
+
|
|
|
|
|
+ if ($commit) {
|
|
|
|
|
+ $this->connection->commit();
|
|
|
|
|
+ $this->ui->print('DB purged - ' . $purged . ' records permanently deleted');
|
|
|
|
|
+ }else{
|
|
|
|
|
+ $this->connection->rollback();
|
|
|
|
|
+ $this->ui->print('DB purged - ' . $purged . ' records would be permanently deleted');
|
|
|
|
|
+ }
|
|
|
|
|
+ }catch (\Throwable $exception){
|
|
|
|
|
+ $this->connection->rollback();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private function purgeFile($maxDate){
|
|
|
|
|
+ $queryBuilder = $this->fileRepository->createQueryBuilder('f');
|
|
|
|
|
+ $queryBuilder->delete();
|
|
|
|
|
+ $this->getQueryConditions($queryBuilder, $maxDate);
|
|
|
|
|
+ return $queryBuilder->getQuery()->execute();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Delete the files
|
|
* Delete the files
|
|
|
*
|
|
*
|
|
|
- * @param Collection $files
|
|
|
|
|
|
|
+ * @param array $files
|
|
|
|
|
+ * @return void
|
|
|
*/
|
|
*/
|
|
|
- protected function deleteFiles(Collection $files): void {
|
|
|
|
|
|
|
+ protected function deleteFiles(array $files): void {
|
|
|
$total = count($files);
|
|
$total = count($files);
|
|
|
$this->ui->print($total . " temporary files to be removed");
|
|
$this->ui->print($total . " temporary files to be removed");
|
|
|
|
|
|
|
|
- $author = $this->accessRepository->find(self::AUTHOR);
|
|
|
|
|
-
|
|
|
|
|
- if ($author === null) {
|
|
|
|
|
- throw new \RuntimeException('Access ' . self::AUTHOR . ' could not be found');
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
$this->ui->print('Deleting files...');
|
|
$this->ui->print('Deleting files...');
|
|
|
$i = 0;
|
|
$i = 0;
|
|
|
$deleted = 0;
|
|
$deleted = 0;
|
|
@@ -140,7 +149,7 @@ class CleanTempFiles extends BaseCronJob implements CronjobInterface
|
|
|
try {
|
|
try {
|
|
|
$i++;
|
|
$i++;
|
|
|
$this->ui->progress($i, $total);
|
|
$this->ui->progress($i, $total);
|
|
|
- $this->storage->delete($file, $author);
|
|
|
|
|
|
|
+ $this->storage->forceDelete($file);
|
|
|
$deleted++;
|
|
$deleted++;
|
|
|
} catch (\RuntimeException $e) {
|
|
} catch (\RuntimeException $e) {
|
|
|
$this->ui->print('ERROR : ' . $e->getMessage());
|
|
$this->ui->print('ERROR : ' . $e->getMessage());
|
|
@@ -151,38 +160,29 @@ class CleanTempFiles extends BaseCronJob implements CronjobInterface
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * Purge the DB from temporary file records older than N days
|
|
|
|
|
- *
|
|
|
|
|
- * @param bool $commit
|
|
|
|
|
- * @throws Exception
|
|
|
|
|
|
|
+ * @param QueryBuilder $queryBuilder
|
|
|
|
|
+ * @param \DateTime $maxDate
|
|
|
|
|
+ * @return mixed
|
|
|
*/
|
|
*/
|
|
|
- protected function purgeDb(bool $commit = true): void {
|
|
|
|
|
-
|
|
|
|
|
- $maxDate = DatesUtils::new();
|
|
|
|
|
- $maxDate->sub(new \DateInterval('P' . self::PURGE_RECORDS_OLDER_THAN . 'D'));
|
|
|
|
|
-
|
|
|
|
|
- $this->ui->print('Purge DB from records of files deleted before ' . $maxDate->format('c'));
|
|
|
|
|
-
|
|
|
|
|
- $this->em->beginTransaction();
|
|
|
|
|
-
|
|
|
|
|
- $queryBuilder = $this->em->createQueryBuilder();
|
|
|
|
|
- $q = $queryBuilder
|
|
|
|
|
- ->delete('File', 'f')
|
|
|
|
|
- ->where($queryBuilder->expr()->eq('f.isTemporaryFile', 1))
|
|
|
|
|
- ->andWhere($queryBuilder->expr()->lt('f.updateDate', ':maxDate'))
|
|
|
|
|
- ->setParameter('maxDate', $maxDate)
|
|
|
|
|
- ->getQuery();
|
|
|
|
|
-
|
|
|
|
|
- $purged = $q->getResult();
|
|
|
|
|
-
|
|
|
|
|
- if ($commit) {
|
|
|
|
|
- $this->em->commit();
|
|
|
|
|
- $this->ui->print('DB purged - ' . $purged . ' records permanently deleted');
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- $this->em->rollback();
|
|
|
|
|
- $this->ui->print('DB purged - ' . $purged . ' records would be permanently deleted');
|
|
|
|
|
|
|
+ private function getQueryConditions(QueryBuilder $queryBuilder, \DateTime $maxDate): void{
|
|
|
|
|
+ $queryBuilder
|
|
|
|
|
+ ->andWhere(
|
|
|
|
|
+ $queryBuilder->expr()->orX(
|
|
|
|
|
+ $queryBuilder->expr()->eq('f.isTemporaryFile', ':temporaryTrue'),
|
|
|
|
|
+ $queryBuilder->expr()->eq('f.status', ':status')
|
|
|
|
|
+ )
|
|
|
|
|
+ )
|
|
|
|
|
+ ->andWhere($queryBuilder->expr()->eq('f.host', ':host'))
|
|
|
|
|
+ ->andWhere(
|
|
|
|
|
+ $queryBuilder->expr()->orX(
|
|
|
|
|
+ $queryBuilder->expr()->lt('f.createDate', ':maxDate'),
|
|
|
|
|
+ $queryBuilder->expr()->isNull('f.createDate')
|
|
|
|
|
+ )
|
|
|
|
|
+ )
|
|
|
|
|
+ ->setParameter('temporaryTrue', true)
|
|
|
|
|
+ ->setParameter('host', FileHostEnum::AP2I()->getValue())
|
|
|
|
|
+ ->setParameter('status', FileStatusEnum::DELETED()->getValue())
|
|
|
|
|
+ ->setParameter('maxDate', $maxDate->format('Y-m-d'))
|
|
|
|
|
+ ;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
}
|
|
}
|