vendor/zenstruck/schedule-bundle/src/EventListener/ScheduleLoggerSubscriber.php line 105

Open in your IDE?
  1. <?php
  2. namespace Zenstruck\ScheduleBundle\EventListener;
  3. use Psr\Log\LoggerInterface;
  4. use Psr\Log\LogLevel;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Zenstruck\ScheduleBundle\Event\AfterScheduleEvent;
  7. use Zenstruck\ScheduleBundle\Event\AfterTaskEvent;
  8. use Zenstruck\ScheduleBundle\Event\BeforeScheduleEvent;
  9. use Zenstruck\ScheduleBundle\Event\BeforeTaskEvent;
  10. /**
  11.  * @author Kevin Bond <kevinbond@gmail.com>
  12.  */
  13. final class ScheduleLoggerSubscriber implements EventSubscriberInterface
  14. {
  15.     /** @var LoggerInterface */
  16.     private $logger;
  17.     public function __construct(LoggerInterface $logger)
  18.     {
  19.         $this->logger $logger;
  20.     }
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             BeforeScheduleEvent::class => 'beforeSchedule',
  25.             AfterScheduleEvent::class => 'afterSchedule',
  26.             BeforeTaskEvent::class => 'beforeTask',
  27.             AfterTaskEvent::class => 'afterTask',
  28.         ];
  29.     }
  30.     public function beforeSchedule(BeforeScheduleEvent $event): void
  31.     {
  32.         $context $event->runContext();
  33.         $allTaskCount \count($context->getSchedule()->all());
  34.         $dueTaskCount \count($context->dueTasks());
  35.         if (=== $dueTaskCount) {
  36.             $this->logger->debug('No tasks due to run.', ['total' => $allTaskCount]);
  37.             return;
  38.         }
  39.         $message \sprintf('%s %d %stask%s.',
  40.             $context->isForceRun() ? 'Force running' 'Running',
  41.             $dueTaskCount,
  42.             $context->isForceRun() ? '' 'due ',
  43.             $dueTaskCount 's' ''
  44.         );
  45.         $this->logger->info($message, [
  46.             'total' => $allTaskCount,
  47.             'due' => $dueTaskCount,
  48.         ]);
  49.     }
  50.     public function afterSchedule(AfterScheduleEvent $event): void
  51.     {
  52.         $context $event->runContext();
  53.         if ($context->isSkipped()) {
  54.             $this->logger->info($context->getSkipReason());
  55.             return;
  56.         }
  57.         $total \count($context->getResults());
  58.         $successful \count($context->getSuccessful());
  59.         $failures \count($context->getFailures());
  60.         $skipped \count($context->getSkipped());
  61.         $run \count($context->getRun());
  62.         $level $context->isSuccessful() ? LogLevel::INFO LogLevel::ERROR;
  63.         if (=== $total) {
  64.             return;
  65.         }
  66.         $this->logger->log($level"{$run}/{$total} tasks ran", [
  67.             'total' => $total,
  68.             'successful' => $successful,
  69.             'skipped' => $skipped,
  70.             'failures' => $failures,
  71.             'duration' => $context->getFormattedDuration(),
  72.             'memory' => $context->getFormattedMemory(),
  73.             'forced' => $context->isForceRun(),
  74.         ]);
  75.     }
  76.     public function beforeTask(BeforeTaskEvent $event): void
  77.     {
  78.         $context $event->runContext();
  79.         $task $context->getTask();
  80.         $this->logger->info(\sprintf('%s "%s"',
  81.             $context->getScheduleRunContext()->isForceRun() ? 'Force running' 'Running',
  82.             $task
  83.         ), ['id' => $task->getId()]);
  84.     }
  85.     public function afterTask(AfterTaskEvent $event): void
  86.     {
  87.         $context $event->runContext();
  88.         $result $context->getResult();
  89.         $task $result->getTask();
  90.         $logContext = ['id' => $task->getId()];
  91.         if ($result->isSkipped()) {
  92.             $this->logger->info("Skipped \"{$task}\" ({$result->getDescription()})"$logContext);
  93.             return;
  94.         }
  95.         $logContext['result'] = $result->getDescription();
  96.         $logContext['duration'] = $context->getFormattedDuration();
  97.         $logContext['memory'] = $context->getFormattedMemory();
  98.         $logContext['forced'] = $context->getScheduleRunContext()->isForceRun();
  99.         if ($result->isSuccessful()) {
  100.             $this->logger->info("Successfully ran \"{$task}\""$logContext);
  101.             return;
  102.         }
  103.         if ($result->getOutput()) {
  104.             $logContext['output'] = $result->getOutput();
  105.         }
  106.         if (!$result->isException()) {
  107.             $this->logger->error("Failure when running \"{$task}\""$logContext);
  108.             return;
  109.         }
  110.         $logContext['exception'] = $result->getException();
  111.         $this->logger->critical("Exception thrown when running \"{$task}\""$logContext);
  112.     }
  113. }