Browse Source

setup logging for cron jobs, and minor fixes

Olivier Massot 3 years ago
parent
commit
27a3c9be77

+ 13 - 14
config/packages/docker/monolog.yaml

@@ -1,5 +1,6 @@
 # Voir doc/logging.md
 monolog:
+    channels: ['cron']
     handlers:
         # sorties standards (stdout, stderr, console)
         stderr:
@@ -45,33 +46,31 @@ monolog:
             max_files: 3
             channels: security
 
-        # logs spécifiques à certains process
-        # * synchro dolibarr
-        dolibarrsync:
+        # logs dédiés à l'exécution des cron-jobs
+        cron:
             type: group
-            members: [dolibarrsync_file]
-            channels: dolibarrsync
-        dolibarrsync_file:
+            members: [cron_file]
+            channels: cron
+        cron_file:
             type: rotating_file
-            path: "%kernel.logs_dir%/%kernel.environment%.dolibarrsync.log"
+            path: "%kernel.logs_dir%/%kernel.environment%.cron.log"
             level: debug
             max_files: 7
             formatter: monolog.formatter.message
-
-#        dolibarrsync_critical:
+#        cron_critical:
 #            type:           fingers_crossed
 #            action_level:   critical
-#            handler:        dolibarrsync_deduplicated
-#        dolibarrsync_deduplicated:
+#            handler:        cron_deduplicated
+#        cron_deduplicated:
 #            type: deduplication
 #            # the time in seconds during which duplicate entries are discarded (default: 60)
 #            time: 10
-#            handler: dolibarrsync_mailer
-#        dolibarrsync_mailer:
+#            handler: cron_mailer
+#        cron_mailer:
 #            type:           symfony_mailer
 #            from_email:     "process@opentalent.fr"
 #            to_email:       "exploitation@opentalent.fr"
-#            subject:        "Dolibarr Sync - Critical Error"
+#            subject:        "Cron - Critical Error"
 #            level:          error
 #            formatter:      monolog.formatter.html
 #            content_type:   text/html

+ 24 - 25
config/packages/prod/monolog.yaml

@@ -1,5 +1,6 @@
 # Voir doc/logging.md
 monolog:
+    channels: ['cron']
     handlers:
         # sorties standards (stdout, stderr, console)
         stderr:
@@ -45,36 +46,34 @@ monolog:
             max_files: 3
             channels: security
 
-        # logs spécifiques à certains process
-        # * synchro dolibarr
-        dolibarrsync:
+        # logs dédiés à l'exécution des cron-jobs
+        cron:
             type: group
-            members: [dolibarrsync_file]
-            channels: dolibarrsync
-        dolibarrsync_file:
+            members: [cron_file]
+            channels: cron
+        cron_file:
             type: rotating_file
-            path: "%kernel.logs_dir%/%kernel.environment%.dolibarrsync.log"
+            path: "%kernel.logs_dir%/%kernel.environment%.cron.log"
             level: debug
             max_files: 7
             formatter: monolog.formatter.message
-
-        #        dolibarrsync_critical:
-        #            type:           fingers_crossed
-        #            action_level:   critical
-        #            handler:        dolibarrsync_deduplicated
-        #        dolibarrsync_deduplicated:
-        #            type: deduplication
-        #            # the time in seconds during which duplicate entries are discarded (default: 60)
-        #            time: 10
-        #            handler: dolibarrsync_mailer
-        #        dolibarrsync_mailer:
-        #            type:           symfony_mailer
-        #            from_email:     "process@opentalent.fr"
-        #            to_email:       "exploitation@opentalent.fr"
-        #            subject:        "Dolibarr Sync - Critical Error"
-        #            level:          error
-        #            formatter:      monolog.formatter.html
-        #            content_type:   text/html
+#        cron_critical:
+#            type:           fingers_crossed
+#            action_level:   critical
+#            handler:        cron_deduplicated
+#        cron_deduplicated:
+#            type: deduplication
+#            # the time in seconds during which duplicate entries are discarded (default: 60)
+#            time: 10
+#            handler: cron_mailer
+#        cron_mailer:
+#            type:           symfony_mailer
+#            from_email:     "process@opentalent.fr"
+#            to_email:       "exploitation@opentalent.fr"
+#            subject:        "Cron - Critical Error"
+#            level:          error
+#            formatter:      monolog.formatter.html
+#            content_type:   text/html
 
 
         # uncomment to get logging in your browser

+ 0 - 4
config/services.yaml

@@ -76,10 +76,6 @@ services:
     App\Service\ServiceIterator\CronjobIterator:
         - !tagged_iterator app.cronjob
 
-    App\Service\Dolibarr\DolibarrSyncService:
-        tags:
-            - { name: monolog.logger, channel: dolibarrsync }
-
     #########################################
     ##  SERIALIZER Decorates ##
     App\Serializer\DefaultNormalizer:

+ 11 - 8
src/Commands/CronCommand.php

@@ -46,7 +46,8 @@ class CronCommand extends Command
     private CronjobIterator $cronjobIterator;
 
     #[Required]
-    public function setLoggerInterface(LoggerInterface $logger): void { $this->logger = $logger; }
+    /** @see https://symfony.com/doc/current/logging/channels_handlers.html#how-to-autowire-logger-channels */
+    public function setLoggerInterface(LoggerInterface $cronLogger): void { $this->logger = $cronLogger; }
     #[Required]
     public function setCronjobIterator(CronjobIterator $cronjobIterator): void { $this->cronjobIterator = $cronjobIterator; }
 
@@ -124,7 +125,11 @@ class CronCommand extends Command
             }
         }
 
-        $this->logger->info('CronCommand will ' . ($preview ? 'preview' : 'execute') . ' ' . implode(', ', $jobs));
+        $this->logger->info(
+            'CronCommand will ' .
+            ($preview ? 'preview' : 'execute') . ' ' .
+            implode(', ', array_map(static function($job) { return $job->name(); }, $jobs))
+        );
 
         foreach ($jobs as $job) {
             $this->runJob($job, $preview);
@@ -171,12 +176,10 @@ class CronCommand extends Command
         $t0 = microtime(true);
 
         $this->output->writeln(
-            $formatter->formatSection($job->name(),"Start")
+            $formatter->formatSection($job->name(),"Start" . ($preview ? ' [PREVIEW MODE]' : ''))
         );
-        if ($preview) {
-            $this->output->writeln('PREVIEW MODE');
-        }
 
+        // Establish communication between job and the console
         $ui = new ConsoleUI($this->output);
         $job->setUI($ui);
 
@@ -194,9 +197,9 @@ class CronCommand extends Command
 
         $t1 = microtime(true);
 
-        $msg = "Job has been successfully executed [" . ($t1 - $t0) . " sec.]";
+        $msg = "Job has been successfully executed (" . round($t1 - $t0, 2) . " sec.)" . ($preview ? ' [PREVIEW MODE]' : '');
         $this->output->writeln($formatter->formatSection($job->name(), $msg));
-        $this->logger->info($msg);
+        $this->logger->info($job->name() . ' - ' . $msg);
 
         return Command::SUCCESS;
     }

+ 5 - 0
src/Service/Cron/BaseCronJob.php

@@ -18,6 +18,7 @@ use Symfony\Contracts\Service\Attribute\Required;
 abstract class BaseCronJob
 {
     protected CronUIInterface $ui;
+    protected LoggerInterface $logger;
 
     #[Pure]
     public function __construct()
@@ -25,6 +26,10 @@ abstract class BaseCronJob
         $this->ui = new SilentUI();
     }
 
+    #[Required]
+    /** @see https://symfony.com/doc/current/logging/channels_handlers.html#how-to-autowire-logger-channels */
+    public function setLoggerInterface(LoggerInterface $cronLogger): void { $this->logger = $cronLogger; }
+
     final public function name(): string {
         return StringsUtils::camelToSnake(
             preg_replace('/(?:\w+\\\)*(\w+)$/', '$1', static::class),

+ 2 - 0
src/Service/Cron/CronjobInterface.php

@@ -3,6 +3,7 @@
 namespace App\Service\Cron;
 
 use App\Service\Cron\UI\CronUIInterface;
+use Psr\Log\LoggerInterface;
 
 /**
  * A cron-job
@@ -13,6 +14,7 @@ interface CronjobInterface
 {
     public function name(): string;
     public function setUI(CronUIInterface $io): void;
+    public function setLoggerInterface(LoggerInterface $cronLogger): void;
     public function preview(): void;
     public function execute(): void;
 }

+ 5 - 2
src/Service/Cron/Job/CleanTempFiles.php

@@ -10,6 +10,7 @@ use App\Service\Storage\LocalStorage;
 use Doctrine\Common\Collections\Collection;
 use Doctrine\Common\Collections\Criteria;
 use Doctrine\ORM\EntityManagerInterface;
+use Exception;
 use JetBrains\PhpStorm\Pure;
 
 /**
@@ -50,6 +51,7 @@ class CleanTempFiles extends BaseCronJob implements CronjobInterface
 
     /**
      * Preview the result of the execution, without actually deleting anything
+     * @throws Exception
      */
     public function preview(): void
     {
@@ -73,6 +75,7 @@ class CleanTempFiles extends BaseCronJob implements CronjobInterface
 
     /**
      * Proceed to the deletion of the files and the purge of the DB
+     * @throws Exception
      */
     public function execute(): void
     {
@@ -87,7 +90,7 @@ class CleanTempFiles extends BaseCronJob implements CronjobInterface
      * List the files to delete in the DB
      *
      * @return Collection
-     * @throws \Exception
+     * @throws Exception
      */
     private function listFilesToDelete(): Collection {
 
@@ -144,7 +147,7 @@ class CleanTempFiles extends BaseCronJob implements CronjobInterface
      * Purge the DB from temporary file records older than N days
      *
      * @param bool $commit
-     * @throws \Exception
+     * @throws Exception
      */
     private function purgeDb(bool $commit = true): void {
 

+ 7 - 1
src/Service/Dolibarr/DolibarrSyncService.php

@@ -36,6 +36,7 @@ use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
 use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
 use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
 use Symfony\Contracts\HttpClient\ResponseInterface;
+use Symfony\Contracts\Service\Attribute\Required;
 use Symfony\Contracts\Translation\TranslatorInterface;
 
 /**
@@ -46,6 +47,8 @@ use Symfony\Contracts\Translation\TranslatorInterface;
  */
 class DolibarrSyncService
 {
+    private LoggerInterface $logger;
+
     public function __construct(
         private OrganizationRepository $organizationRepository,
         private AccessRepository $accessRepository,
@@ -54,9 +57,12 @@ class DolibarrSyncService
         private AddressPostalUtils $addressPostalUtils,
         private ArrayUtils $arrayUtils,
         private TranslatorInterface $translator,
-        private LoggerInterface $logger
     ) {}
 
+    #[Required]
+    /** @see https://symfony.com/doc/current/logging/channels_handlers.html#how-to-autowire-logger-channels */
+    public function setLoggerInterface(LoggerInterface $cronLogger): void { $this->logger = $cronLogger; }
+
     /**
      * Performs a scan, comparing data from the Opentalent DB and the data returned
      * by the Dolibarr API

+ 2 - 0
src/Service/Storage/LocalStorage.php

@@ -261,6 +261,8 @@ class LocalStorage implements FileStorageInterface
              ->setSize(0)
              ->setUpdatedBy($author->getId());
 
+        $this->entityManager->flush();
+
         return $file;
     }