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

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