vendor/dukecity/command-scheduler-bundle/EventSubscriber/SchedulerCommandSubscriber.php line 53

Open in your IDE?
  1. <?php
  2. namespace Dukecity\CommandSchedulerBundle\EventSubscriber;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use JetBrains\PhpStorm\ArrayShape;
  5. use Dukecity\CommandSchedulerBundle\Event\SchedulerCommandCreatedEvent;
  6. use Dukecity\CommandSchedulerBundle\Event\SchedulerCommandPostExecutionEvent;
  7. use Dukecity\CommandSchedulerBundle\Event\SchedulerCommandFailedEvent;
  8. use Dukecity\CommandSchedulerBundle\Event\SchedulerCommandPreExecutionEvent;
  9. use Dukecity\CommandSchedulerBundle\Notification\CronMonitorNotification;
  10. use Psr\Log\LoggerInterface;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\Notifier\NotifierInterface;
  13. use Symfony\Component\Notifier\Recipient\Recipient;
  14. class SchedulerCommandSubscriber implements EventSubscriberInterface
  15. {
  16.     protected LoggerInterface $logger;
  17.     protected EntityManagerInterface $em;
  18.     protected NotifierInterface|null $notifier;
  19.     /**
  20.      * TODO check if parameters needed
  21.      */
  22.     public function __construct(LoggerInterface $loggerEntityManagerInterface $emNotifierInterface|null $notifier null, private array $monitor_mail = [], private string $monitor_mail_subject 'CronMonitor:')
  23.     {
  24.         $this->logger $logger;
  25.         $this->em $em;
  26.         $this->notifier $notifier;
  27.     }
  28.     /**
  29.      * {@inheritdoc}
  30.      */
  31.     #[ArrayShape([
  32.         SchedulerCommandCreatedEvent::class => 'array',
  33.         SchedulerCommandFailedEvent::class => 'array',
  34.         SchedulerCommandPreExecutionEvent::class => 'array',
  35.         SchedulerCommandPostExecutionEvent::class => 'array',
  36.     ])]
  37.     public static function getSubscribedEvents(): array
  38.     {
  39.         return [
  40.             SchedulerCommandCreatedEvent::class => ['onScheduledCommandCreated',    -10],
  41.             SchedulerCommandFailedEvent::class => ['onScheduledCommandFailed',     20],
  42.             SchedulerCommandPreExecutionEvent::class => ['onScheduledCommandPreExecution',   10],
  43.             SchedulerCommandPostExecutionEvent::class => ['onScheduledCommandPostExecution',   30],
  44.         ];
  45.     }
  46.     // TODO check if useful (could be handled by doctrine lifecycle events)
  47.     public function onScheduledCommandCreated(SchedulerCommandCreatedEvent $event)
  48.     {
  49.         $this->logger->info('ScheduledCommandCreated', ['name' => $event->getCommand()->getName()]);
  50.     }
  51.     public function onScheduledCommandFailed(SchedulerCommandFailedEvent $event)
  52.     {
  53.         # notifier is optional
  54.         if($this->notifier)
  55.         {
  56.             //...$this->notifier->getAdminRecipients()
  57.             $recipients = [];
  58.             foreach ($this->monitor_mail as $mailadress) {
  59.                 $recipients[] = new Recipient($mailadress);
  60.             }
  61.             $this->notifier->send(new CronMonitorNotification($event->getFailedCommands(), $this->monitor_mail_subject), ...$recipients);
  62.         }
  63.         //$this->logger->warning('SchedulerCommandFailedEvent', ['details' => $event->getMessage()]);
  64.     }
  65.     public function onScheduledCommandPreExecution(SchedulerCommandPreExecutionEvent $event)
  66.     {
  67.         #var_dump('ScheduledCommandPreExecution');
  68.         $this->logger->info('ScheduledCommandPreExecution', ['name' => $event->getCommand()->getName()]);
  69.     }
  70.     public function onScheduledCommandPostExecution(SchedulerCommandPostExecutionEvent $event)
  71.     {
  72.         #var_dump('ScheduledCommandPostExecution');
  73.         $this->logger->info('ScheduledCommandPostExecution', [
  74.             'name' => $event->getCommand()->getName(),
  75.             "result" => $event->getResult(),
  76.             #"log" => $event->getLog(),
  77.             "runtime" => $event->getRuntime()->format('%S seconds'),
  78.             #"exception" => $event->getException()?->getMessage() ?? null
  79.         ]);
  80.     }
  81. }