소스 검색

setup mailing reports for crons

Olivier Massot 1 년 전
부모
커밋
5cf21090e2

+ 32 - 7
config/packages/docker/monolog.yaml

@@ -7,7 +7,7 @@ monolog:
             type: stream
             path: php://stderr
             level: error
-            channels: ["!event", "!doctrine"]
+            channels: ["!event", "!doctrine", "!cron"]
         console:
             type: console
             process_psr_3_messages: false
@@ -46,36 +46,61 @@ monolog:
             max_files: 3
             channels: security
 
-        # logs dédiés à l'exécution des cron-jobs
+
+
+        ### --- Cron-Jobs ---
         cron:
             type: group
             members: [cron_file]
             channels: cron
+        # Log fichier (niveau debug)
         cron_file:
             type: rotating_file
             path: "%kernel.logs_dir%/%kernel.environment%.cron.log"
             level: debug
             max_files: 7
             formatter: monolog.formatter.message
+
+        # Rapport par mail
+        cron_info:
+            type:           fingers_crossed
+            action_level:   info
+            handler:        cron_info_deduplicated
+            channels: ['cron']
+        cron_info_deduplicated:
+            type: deduplication
+            # the time in seconds during which duplicate entries are discarded (default: 60)
+            time: 10
+            handler: cron_info_mailer
+        cron_info_mailer:
+            type:           symfony_mailer
+            from_email:     "cron@opentalent.fr"
+            to_email:       "exploitation@opentalent.fr"
+            subject:        "Cron - Execution Report"
+            level:          info
+            content_type:   text/html
+
+        # Log par mail en cas d'erreur critique
         cron_critical:
             type:           fingers_crossed
             action_level:   critical
-            handler:        cron_deduplicated
-        cron_deduplicated:
+            handler:        cron_critical_deduplicated
+        cron_critical_deduplicated:
             type: deduplication
             # the time in seconds during which duplicate entries are discarded (default: 60)
             time: 10
-            handler: cron_mailer
-        cron_mailer:
+            handler: cron_critical_mailer
+        cron_critical_mailer:
             type:           symfony_mailer
             from_email:     "cron@opentalent.fr"
             to_email:       "exploitation@opentalent.fr"
             subject:        "Cron - Critical Error"
-            level:          error
+            level:          critical
             formatter:      monolog.formatter.html
             content_type:   text/html
 
 
+
         # uncomment to get logging in your browser
         # you may have to allow bigger header sizes in your Web server configuration
         #firephp:

+ 2 - 2
doc/cron.md

@@ -2,7 +2,7 @@
 
 ## La commande `ot:cron`
 
-Les taches planifiées (crons) sont éxecutées au moyen de la commande suivante, où jobs est le nom du job 
+Les taches planifiées (crons) sont ecutées au moyen de la commande suivante, où jobs est le nom du job 
 à effectuer (ou une liste de noms de séparés par des virgules): 
 
     bin/console ot:cron run <jobs>
@@ -15,7 +15,7 @@ Pour plus d'informations :
 
     bin/console ot:cron --help
 
-> A noter: tous les jobs implémentent une méthode preview qui peut être exécutée en passant l'option -p, --preview 
+> A noter : tous les jobs implémentent une méthode preview qui peut être exécutée en passant l'option -p, --preview 
 > à la commande.
 
 ## Créer un nouveau job

+ 6 - 5
src/Commands/CronCommand.php

@@ -16,6 +16,7 @@ use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Output\OutputInterface;
 use Symfony\Contracts\Service\Attribute\Required;
+use Throwable;
 
 /**
  * CLI Command to run the cron-jobs
@@ -26,7 +27,7 @@ use Symfony\Contracts\Service\Attribute\Required;
  *     bin/console ot:cron run clean-db --preview
  *     bin/console ot:cron run clean-db
  *
- * @see ~/src/Service/Cron/Readme.md
+ * @see doc/cron.md
  */
 #[AsCommand(
     name: 'ot:cron',
@@ -135,9 +136,9 @@ class CronCommand extends Command
         }
 
         $this->logger->info(
-            'CronCommand will ' .
-            ($preview ? 'preview' : 'execute') . ' ' .
-            implode(', ', array_map(static function($job) { return $job->name(); }, $jobs))
+            'CronCommand will execute ' .
+            implode(', ', array_map(static function($job) { return $job->name(); }, $jobs)) .
+            ($preview ? ' [PREVIEW MODE]' : '')
         );
 
         $results = [];
@@ -201,7 +202,7 @@ class CronCommand extends Command
             } else {
                 $job->execute();
             }
-        } catch (RuntimeException $e) {
+        } catch (Throwable $e) {
             $this->logger->critical($e);
             $this->output->write("An error happened while running the process : " . $e);
             return Command::FAILURE;

+ 4 - 2
src/Service/Cron/BaseCronJob.php

@@ -14,6 +14,8 @@ use Symfony\Contracts\Service\Attribute\Required;
  *
  * This class shouldn't implement directly the CronjobInterface because it shall not be injected itself into the
  * CronjobIterator, but all its subclasses should.
+ *
+ * @see doc/cron.md
  */
 abstract class BaseCronJob implements CronjobInterface
 {
@@ -37,7 +39,7 @@ abstract class BaseCronJob implements CronjobInterface
         );
     }
 
-    public function setUI(CronUIInterface $ui): void {
-        $this->ui = $ui;
+    public function setUI(CronUIInterface $io): void {
+        $this->ui = $io;
     }
 }

+ 1 - 1
src/Service/Cron/CronjobInterface.php

@@ -8,7 +8,7 @@ use Psr\Log\LoggerInterface;
 /**
  * A cron-job
  *
- * @see ~/src/Service/Cron/Readme.md
+ * @see doc/cron.md
  */
 interface CronjobInterface
 {

+ 2 - 2
src/Service/Dolibarr/DolibarrSyncService.php

@@ -633,8 +633,8 @@ class DolibarrSyncService
     /**
      * Returns the number of accesses possessing at least one of the missions
      *
-     * @param array<mixed> $missions A list of missions
-     * @param array<mixed> $members An organization members as returned by getActiveMembersIndex: [$accessID => [$missions...]]
+     * @param array<string> $missions A list of missions
+     * @param array<string, array<string>> $members An organization members as returned by getActiveMembersIndex: [$accessID => [$missions...]]
      * @return int
      */
     protected function countWithMission(array $missions, array $members): int