diff --git a/src/Cron/CronJobFactoryInterface.php b/src/Cron/CronJobFactoryInterface.php deleted file mode 100644 index 1677e278..00000000 --- a/src/Cron/CronJobFactoryInterface.php +++ /dev/null @@ -1,15 +0,0 @@ - $mergedConfig */ protected function loadInternal(array $mergedConfig, ContainerBuilder $container): void @@ -157,22 +149,22 @@ private function registerConfiguration(ContainerBuilder $container, array $confi ->setPublic(false) ->setFactory([$clientBuilderDefinition, 'getClient']); - // Setup Cronjob. + // Setup Monitors. $environment = ''; - if (isset($options['environment'])) { + if (\is_array($options) && isset($options['environment'])) { $environment = $options['environment']; } $release = null; - if (isset($options['release'])) { + if (\is_array($options) && isset($options['release'])) { $release = $options['release']; } $container - ->register(CronJobFactoryInterface::class, CronJobFactory::class) + ->register(MonitorFactoryInterface::class, MonitorFactory::class) ->setPublic(true) - ->setArgument(0, $environment) - ->setArgument(1, $release); + ->setArguments([$environment, $release]) + ; } /** diff --git a/src/Cron/CronJob.php b/src/Monitor/Monitor.php similarity index 64% rename from src/Cron/CronJob.php rename to src/Monitor/Monitor.php index fb217cce..3e9ad970 100644 --- a/src/Cron/CronJob.php +++ b/src/Monitor/Monitor.php @@ -2,15 +2,25 @@ declare(strict_types=1); -namespace Sentry\SentryBundle\Cron; +namespace Sentry\SentryBundle\Monitor; use Sentry\CheckIn; use Sentry\CheckInStatus; use Sentry\Event; +use Sentry\MonitorConfig; use Sentry\State\HubInterface; -class CronJob implements CronJobInterface +class Monitor implements MonitorInterface { + /** + * @var HubInterface + */ + private $hub; + + /** + * @var MonitorConfig + */ + private $monitorConfig; /** * @var string */ @@ -19,24 +29,26 @@ class CronJob implements CronJobInterface * @var string */ private $slug; - /** - * @var HubInterface - */ - private $hub; /** * @var string|null */ private $release; - public function __construct(HubInterface $hub, string $slug, string $environment, ?string $release = null) - { + public function __construct( + HubInterface $hub, + MonitorConfig $monitorConfig, + string $slug, + string $environment, + string $release = null + ) { + $this->monitorConfig = $monitorConfig; $this->environment = $environment; $this->slug = $slug; $this->hub = $hub; $this->release = $release; } - public function inProgress(?CheckIn $previous = null): CheckIn + public function inProgress(CheckIn $previous = null): CheckIn { $event = Event::createCheckIn(); $checkIn = new CheckIn( @@ -44,7 +56,9 @@ public function inProgress(?CheckIn $previous = null): CheckIn CheckInStatus::inProgress(), $previous ? $previous->getId() : null, $this->release, - $this->environment + $this->environment, + null, + $this->monitorConfig ); $event->setCheckIn($checkIn); $this->hub->captureEvent($event); @@ -52,7 +66,7 @@ public function inProgress(?CheckIn $previous = null): CheckIn return $checkIn; } - public function error(?CheckIn $previous = null): CheckIn + public function error(CheckIn $previous = null): CheckIn { $event = Event::createCheckIn(); $checkIn = new CheckIn( @@ -60,7 +74,9 @@ public function error(?CheckIn $previous = null): CheckIn CheckInStatus::error(), $previous ? $previous->getId() : null, $this->release, - $this->environment + $this->environment, + null, + $this->monitorConfig ); $event->setCheckIn($checkIn); $this->hub->captureEvent($event); @@ -68,7 +84,7 @@ public function error(?CheckIn $previous = null): CheckIn return $checkIn; } - public function ok(?CheckIn $previous = null): CheckIn + public function ok(CheckIn $previous = null): CheckIn { $event = Event::createCheckIn(); $checkIn = new CheckIn( @@ -76,7 +92,9 @@ public function ok(?CheckIn $previous = null): CheckIn CheckInStatus::ok(), $previous ? $previous->getId() : null, $this->release, - $this->environment + $this->environment, + null, + $this->monitorConfig ); $event->setCheckIn($checkIn); $this->hub->captureEvent($event); diff --git a/src/Cron/CronJobFactory.php b/src/Monitor/MonitorFactory.php similarity index 51% rename from src/Cron/CronJobFactory.php rename to src/Monitor/MonitorFactory.php index 2541b557..1043ea7d 100644 --- a/src/Cron/CronJobFactory.php +++ b/src/Monitor/MonitorFactory.php @@ -2,11 +2,12 @@ declare(strict_types=1); -namespace Sentry\SentryBundle\Cron; +namespace Sentry\SentryBundle\Monitor; +use Sentry\MonitorConfig; use Sentry\SentrySdk; -class CronJobFactory implements CronJobFactoryInterface +class MonitorFactory implements MonitorFactoryInterface { /** * @var string @@ -20,18 +21,16 @@ class CronJobFactory implements CronJobFactoryInterface /** * @param string $environment the configured environment */ - public function __construct(string $environment, ?string $release = null) + public function __construct(string $environment, string $release = null) { $this->environment = $environment; $this->release = $release; } - /** - * {@inheritdoc} - */ - public function getCronJob(string $slug): CronJobInterface + public function getMonitor(string $slug, MonitorConfig $monitorConfig): MonitorInterface { $hub = SentrySdk::getCurrentHub(); - return new CronJob($hub, $slug, $this->environment, $this->release); + + return new Monitor($hub, $monitorConfig, $slug, $this->environment, $this->release); } } diff --git a/src/Monitor/MonitorFactoryInterface.php b/src/Monitor/MonitorFactoryInterface.php new file mode 100644 index 00000000..d9a5f29d --- /dev/null +++ b/src/Monitor/MonitorFactoryInterface.php @@ -0,0 +1,17 @@ +getCronjob('test-monitor'); - $this->assertInstanceOf(CronJobInterface::class, $cronjob); + $factory = new MonitorFactory('test', 'test-release'); + $monitorConfig = new MonitorConfig( + MonitorSchedule::crontab('*/5 * * * *'), + 5, + 30, + 'UTC' + ); + $cronJob = $factory->getMonitor('test-cronjob', $monitorConfig); + $this->assertInstanceOf(MonitorInterface::class, $cronJob); + + // Create a CheckIn + $checkIn = $cronJob->inProgress(); + $this->assertEquals('test', $checkIn->getEnvironment()); + $this->assertEquals('test-release', $checkIn->getRelease()); + $this->assertEquals('test-cronjob', $checkIn->getMonitorSlug()); + } + + public function testInterval(): void + { + // Setup test + $factory = new MonitorFactory('test', 'test-release'); + $monitorConfig = new MonitorConfig( + MonitorSchedule::interval( + 30, + MonitorScheduleUnit::minute() + ), + 5, + 30, + 'UTC' + ); + $interval = $factory->getMonitor('test-interval', $monitorConfig); + $this->assertInstanceOf(MonitorInterface::class, $interval); // Create a CheckIn - $checkIn = $cronjob->inProgress(); + $checkIn = $interval->inProgress(); $this->assertEquals('test', $checkIn->getEnvironment()); $this->assertEquals('test-release', $checkIn->getRelease()); - $this->assertEquals('test-monitor', $checkIn->getMonitorSlug()); + $this->assertEquals('test-interval', $checkIn->getMonitorSlug()); } }