瀏覽代碼

improve logging and prevent silent exception

Olivier Massot 1 年之前
父節點
當前提交
aeef3d3bac
共有 1 個文件被更改,包括 15 次插入4 次删除
  1. 15 4
      src/Service/Cron/Job/CleanDb.php

+ 15 - 4
src/Service/Cron/Job/CleanDb.php

@@ -10,6 +10,7 @@ use Doctrine\DBAL\Connection;
 use Doctrine\DBAL\DBALException;
 use Exception;
 use JetBrains\PhpStorm\Pure;
+use Throwable;
 
 /**
  * Cronjob that delete records older than N days in DB tables like Audit_ or Message
@@ -58,6 +59,7 @@ class CleanDb extends BaseCronJob implements CronjobInterface
      * @param bool $commit
      * @throws Exception
      * @throws \Doctrine\DBAL\Driver\Exception
+     * @throws Throwable
      */
     protected function purgeDb(bool $commit = true): void
     {
@@ -67,7 +69,7 @@ class CleanDb extends BaseCronJob implements CronjobInterface
         $maxDateAudit = DatesUtils::new();
         $maxDateAudit->sub(new \DateInterval('P' . self::PURGE_AUDIT_RECORDS_OLDER_THAN . 'D'));
 
-        $this->ui->print('Purge DB from temporary records modified before ' . $maxDate->format('c'));
+        $this->ui->print('Purge DB from temporary records modified before '.$maxDate->format('c'));
         $this->connection->beginTransaction();
         $this->connection->setAutoCommit(false);
 
@@ -88,6 +90,7 @@ class CleanDb extends BaseCronJob implements CronjobInterface
             }
         }catch (\Throwable $exception){
             $this->connection->rollback();
+            throw $exception;
         }
     }
 
@@ -102,7 +105,7 @@ class CleanDb extends BaseCronJob implements CronjobInterface
      */
     protected function purgeAuditTables(DateTime $maxDate): int
     {
-        $this->ui->print('Purge Audit tables');
+        $this->ui->print('Purge Audit_* tables from the records created before the '.$maxDate->format('c'));
 
         $tableNames = $this->connection->getSchemaManager()->listTableNames();
 
@@ -147,7 +150,11 @@ class CleanDb extends BaseCronJob implements CronjobInterface
             ->andWhere( $q->expr()->eq('isSystem', true))
         ;
 
-        return $q->execute();
+        $purged = $q->execute();
+
+        $this->ui->print('* Message : '.$purged.' lines to delete');
+
+        return $purged;
     }
 
     /**
@@ -168,6 +175,10 @@ class CleanDb extends BaseCronJob implements CronjobInterface
             ->andWhere( $q->expr()->lt('createDate', $maxDate->format('Y-m-d')))
         ;
 
-        return $q->execute();
+        $purged = $q->execute();
+
+        $this->ui->print('* Information : '.$purged.' lines to delete');
+
+        return $purged;
     }
 }