فهرست منبع

Merge branch 'hotfix/V8-7088-cron---critical-error'

Olivier Massot 10 ماه پیش
والد
کامیت
d6f8e6ddeb
1فایلهای تغییر یافته به همراه21 افزوده شده و 48 حذف شده
  1. 21 48
      src/Service/Cron/Job/CleanTempFiles.php

+ 21 - 48
src/Service/Cron/Job/CleanTempFiles.php

@@ -62,8 +62,6 @@ class CleanTempFiles extends BaseCronJob
             }
             $this->ui->print('  * '.$file->getPath());
         }
-
-        $this->purgeDb($maxDate, false);
     }
 
     /**
@@ -77,8 +75,9 @@ class CleanTempFiles extends BaseCronJob
         $maxDate->sub(new \DateInterval('P'.self::DELETE_OLDER_THAN.'D'));
 
         $files = $this->listFilesToDelete($maxDate);
+        $this->logger->info(count($files).' temporary files to be removed');
+
         $this->deleteFiles($files);
-        $this->purgeDb($maxDate);
     }
 
     /**
@@ -99,46 +98,6 @@ class CleanTempFiles extends BaseCronJob
         return $queryBuilder->getQuery()->getResult();
     }
 
-    /**
-     * Purge the DB from temporary file records older than N days.
-     *
-     * @throws ConnectionException
-     */
-    protected function purgeDb(\DateTime $maxDate, bool $commit = true): void
-    {
-        $this->connection->beginTransaction();
-        $this->connection->setAutoCommit(false);
-
-        $purged = 0;
-
-        try {
-            $purged += $this->purgeFiles($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 (\Exception $exception) {
-            $this->connection->rollback();
-            throw $exception;
-        }
-    }
-
-    /**
-     * Purge File table and returns the number of deleted records.
-     */
-    protected function purgeFiles(\DateTime $maxDate): int
-    {
-        $queryBuilder = $this->fileRepository->createQueryBuilder('f');
-        $queryBuilder->delete();
-        $this->getQueryConditions($queryBuilder, $maxDate);
-
-        return $queryBuilder->getQuery()->execute();
-    }
-
     /**
      * Delete the files.
      *
@@ -147,9 +106,13 @@ class CleanTempFiles extends BaseCronJob
     protected function deleteFiles(array $files): void
     {
         $total = count($files);
-        $this->ui->print($total.' temporary files to be removed');
+        $this->logger->info($total.' temporary files to be removed');
 
-        $this->ui->print('Deleting files...');
+        $this->connection->beginTransaction();
+        $this->connection->setAutoCommit(false);
+        $queryBuilder = $this->fileRepository->createQueryBuilder('f');
+
+        $this->logger->info('Deleting files...');
         $i = 0;
         $deleted = 0;
         $this->ui->progress(0, $total);
@@ -157,14 +120,24 @@ class CleanTempFiles extends BaseCronJob
             try {
                 ++$i;
                 $this->ui->progress($i, $total);
+
+                // Delete from disk
                 $this->storage->hardDelete($file);
+
+                // Remove from DB
+                $queryBuilder->delete()->where('f.id = :id')->setParameter('id', $file->getId());
+
                 ++$deleted;
-            } catch (\RuntimeException $e) {
-                $this->ui->print('ERROR : '.$e->getMessage());
+            } catch (\RuntimeException|\InvalidArgumentException $e) {
+                $this->logger->error('ERROR : '.$e->getMessage());
+            } catch (\Exception $exception) {
+                $this->connection->rollback();
+                throw $exception;
             }
         }
 
-        $this->ui->print($deleted.' files deleted');
+        $this->connection->commit();
+        $this->logger->info($deleted.' files deleted');
     }
 
     protected function getQueryConditions(QueryBuilder $queryBuilder, \DateTime $maxDate): void