src/CronBundle/EventSubscriber/CronScheduleEventSubscriber.php line 67

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of SolidInvoice project.
  5.  *
  6.  * (c) Pierre du Plessis <open-source@solidworx.co>
  7.  *
  8.  * This source file is subject to the MIT license that is bundled
  9.  * with this source code in the file LICENSE.
  10.  */
  11. namespace SolidInvoice\CronBundle\EventSubscriber;
  12. use Sentry\CheckInStatus;
  13. use Sentry\MonitorConfig;
  14. use Sentry\MonitorSchedule;
  15. use Sentry\MonitorScheduleUnit;
  16. use Sentry\State\Scope;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Zenstruck\ScheduleBundle\Event\AfterScheduleEvent;
  19. use Zenstruck\ScheduleBundle\Event\BeforeScheduleEvent;
  20. use function date_default_timezone_get;
  21. use function Sentry\captureCheckIn;
  22. use function Sentry\configureScope;
  23. final class CronScheduleEventSubscriber implements EventSubscriberInterface
  24. {
  25.     private const MONITOR_SLUG 'cron-schedule';
  26.     private ?string $checkInId null;
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             BeforeScheduleEvent::class => 'beforeSchedule',
  31.             AfterScheduleEvent::class => 'afterSchedule',
  32.         ];
  33.     }
  34.     public function beforeSchedule(BeforeScheduleEvent $event): void
  35.     {
  36.         configureScope(function (Scope $scope): void {
  37.             $scope->setContext('monitor', [
  38.                 'slug' => self::MONITOR_SLUG,
  39.             ]);
  40.         });
  41.         $monitorSchedule MonitorSchedule::interval(1MonitorScheduleUnit::minute());
  42.         $monitorConfig = new MonitorConfig(
  43.             $monitorSchedule,
  44.             2// check-in margin in minutes
  45.             5// max runtime in minutes
  46.         );
  47.         $this->checkInId captureCheckIn(
  48.             self::MONITOR_SLUG,
  49.             CheckInStatus::inProgress(),
  50.             null,
  51.             $monitorConfig,
  52.             date_default_timezone_get()
  53.         );
  54.     }
  55.     public function afterSchedule(AfterScheduleEvent $event): void
  56.     {
  57.         if ($this->checkInId === null || $this->checkInId === '') {
  58.             return;
  59.         }
  60.         captureCheckIn(
  61.             self::MONITOR_SLUG,
  62.             CheckInStatus::ok(),
  63.             $event->runContext()->getDuration(),
  64.             null,
  65.             $this->checkInId
  66.         );
  67.         $this->checkInId null;
  68.     }
  69. }