vendor/3slab/vdm-library-bundle/EventSubscriber/Monitoring/MonitoringWorkerStartedSubscriber.php line 65

Open in your IDE?
  1. <?php
  2. /**
  3.  * @package    3slab/VdmLibraryBundle
  4.  * @copyright  2020 Suez Smart Solutions 3S.lab
  5.  * @license    https://github.com/3slab/VdmLibraryBundle/blob/master/LICENSE
  6.  */
  7. namespace Vdm\Bundle\LibraryBundle\EventSubscriber\Monitoring;
  8. use Psr\Log\NullLogger;
  9. use Vdm\Bundle\LibraryBundle\Event\CollectWorkerStartedEvent;
  10. use Vdm\Bundle\LibraryBundle\Monitoring\Model\RunningStat;
  11. use Vdm\Bundle\LibraryBundle\Service\Monitoring\Monitoring;
  12. use Vdm\Bundle\LibraryBundle\Service\Monitoring\MonitoringService;
  13. use Vdm\Bundle\LibraryBundle\Service\Monitoring\Storage\StorageInterface;
  14. use Psr\Log\LoggerInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\Messenger\Event\WorkerStartedEvent;
  17. /**
  18.  * Class MonitoringWorkerStartedSubscriber
  19.  *
  20.  * @package Vdm\Bundle\LibraryBundle\EventSubscriber\Monitoring
  21.  */
  22. class MonitoringWorkerStartedSubscriber implements EventSubscriberInterface
  23. {
  24.     /**
  25.      * @var MonitoringService
  26.      */
  27.     protected $monitoring;
  28.     /**
  29.      * @var LoggerInterface
  30.      */
  31.     protected $logger;
  32.     /**
  33.      * MonitoringWorkerHandledMessageListener constructor.
  34.      *
  35.      * @param MonitoringService $monitoring
  36.      * @param LoggerInterface|null $vdmLogger
  37.      */
  38.     public function __construct(MonitoringService $monitoringLoggerInterface $vdmLogger null)
  39.     {
  40.         $this->monitoring $monitoring;
  41.         $this->logger $vdmLogger ?? new NullLogger();
  42.     }
  43.     /**
  44.      * Method executed on WorkerStartedEvent event
  45.      *
  46.      * @param WorkerStartedEvent $event
  47.      */
  48.     public function onWorkerStartedEvent(WorkerStartedEvent $event)
  49.     {
  50.         $this->handleMonitoring();
  51.     }
  52.     /**
  53.      * Method executed on CollectWorkerStartedEvent event
  54.      *
  55.      * @param CollectWorkerStartedEvent $event
  56.      */
  57.     public function onCollectWorkerStartedEvent(CollectWorkerStartedEvent $event)
  58.     {
  59.         $this->handleMonitoring();
  60.     }
  61.     /**
  62.      * Set running stat
  63.      */
  64.     protected function handleMonitoring(): void
  65.     {
  66.         $this->monitoring->update(Monitoring::RUNNING_STAT1);
  67.         $this->logger->debug('worker started metric sent');
  68.     }
  69.     /**
  70.      * {@inheritDoc}
  71.      * @codeCoverageIgnore
  72.      */
  73.     public static function getSubscribedEvents(): array
  74.     {
  75.         return [
  76.             CollectWorkerStartedEvent::class => 'onCollectWorkerStartedEvent',
  77.             WorkerStartedEvent::class => 'onWorkerStartedEvent',
  78.         ];
  79.     }
  80. }