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

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