diff --git a/src/ScheduleMonitorServiceProvider.php b/src/ScheduleMonitorServiceProvider.php index 8da0cd4..f341bb7 100644 --- a/src/ScheduleMonitorServiceProvider.php +++ b/src/ScheduleMonitorServiceProvider.php @@ -111,32 +111,40 @@ protected function registerEventHandlers(): self protected function registerSchedulerEventMacros(): self { - SchedulerEvent::macro('monitorName', function (string $monitorName) { - $this->monitorName = $monitorName; + $this->app->singleton( + Support\ScheduledTasks\ScheduleMonitoringConfigurationsRepository::class, + static fn () => new Support\ScheduledTasks\ScheduleMonitoringConfigurationsRepository(), + ); + + /** @var Support\ScheduledTasks\ScheduleMonitoringConfigurationsRepository $scheduleMonitoringConfigurationsRepository */ + $scheduleMonitoringConfigurationsRepository = $this->app->make(Support\ScheduledTasks\ScheduleMonitoringConfigurationsRepository::class); + + SchedulerEvent::macro('monitorName', function (string $monitorName) use ($scheduleMonitoringConfigurationsRepository) { + $scheduleMonitoringConfigurationsRepository->setMonitorName($this, $monitorName); return $this; }); - SchedulerEvent::macro('graceTimeInMinutes', function (int $graceTimeInMinutes) { - $this->graceTimeInMinutes = $graceTimeInMinutes; + SchedulerEvent::macro('graceTimeInMinutes', function (int $graceTimeInMinutes) use ($scheduleMonitoringConfigurationsRepository) { + $scheduleMonitoringConfigurationsRepository->setGraceTimeInMinutes($this, $graceTimeInMinutes); return $this; }); - SchedulerEvent::macro('doNotMonitor', function (bool $bool = true) { - $this->doNotMonitor = $bool; + SchedulerEvent::macro('doNotMonitor', function (bool $bool = true) use ($scheduleMonitoringConfigurationsRepository) { + $scheduleMonitoringConfigurationsRepository->setDoNotMonitor($this, $bool); return $this; }); - SchedulerEvent::macro('doNotMonitorAtOhDear', function (bool $bool = true) { - $this->doNotMonitorAtOhDear = $bool; + SchedulerEvent::macro('doNotMonitorAtOhDear', function (bool $bool = true) use ($scheduleMonitoringConfigurationsRepository) { + $scheduleMonitoringConfigurationsRepository->setDoNotMonitorAtOhDear($this, $bool); return $this; }); - SchedulerEvent::macro('storeOutputInDb', function () { - $this->storeOutputInDb = true; + SchedulerEvent::macro('storeOutputInDb', function () use ($scheduleMonitoringConfigurationsRepository) { + $scheduleMonitoringConfigurationsRepository->setStoreOutputInDb($this, true); /** @psalm-suppress UndefinedMethod */ $this->ensureOutputIsBeingCaptured(); diff --git a/src/Support/Concerns/UsesScheduleMonitoringConfigurationsRepository.php b/src/Support/Concerns/UsesScheduleMonitoringConfigurationsRepository.php new file mode 100644 index 0000000..0b7aa6b --- /dev/null +++ b/src/Support/Concerns/UsesScheduleMonitoringConfigurationsRepository.php @@ -0,0 +1,13 @@ +setProperty($target, 'monitorName', $monitorName); + } + + public function getMonitorName(object $target): ?string + { + return $this->getProperty($target, 'monitorName'); + } + + public function setGraceTimeInMinutes(object $target, int $graceTimeInMinutes): void + { + $this->setProperty($target, 'graceTimeInMinutes', $graceTimeInMinutes); + } + + public function getGraceTimeInMinutes(object $target): ?int + { + return $this->getProperty($target, 'graceTimeInMinutes'); + } + + public function setDoNotMonitor(object $target, bool $doNotMonitor = true): void + { + $this->setProperty($target, 'doNotMonitor', $doNotMonitor); + } + + public function getDoNotMonitor(object $target): ?bool + { + return $this->getProperty($target, 'doNotMonitor'); + } + + public function setDoNotMonitorAtOhDear(object $target, bool $doNotMonitorAtOhDear = true): void + { + $this->setProperty($target, 'doNotMonitorAtOhDear', $doNotMonitorAtOhDear); + } + + public function getDoNotMonitorAtOhDear(object $target): ?bool + { + return $this->getProperty($target, 'doNotMonitorAtOhDear'); + } + + public function setStoreOutputInDb(object $target, bool $storeOutputInDb = true): void + { + $this->setProperty($target, 'storeOutputInDb', $storeOutputInDb); + } + + public function getStoreOutputInDb(object $target): ?bool + { + return $this->getProperty($target, 'storeOutputInDb'); + } + + + private function setProperty(object $target, string $key, mixed $value): void + { + data_set($this->store, $this->makeKey($target, $key), $value); + } + + private function getProperty(object $target, string $key): mixed + { + dump(__METHOD__, $this->store); + + return data_get($this->store, $this->makeKey($target, $key)); + } + + private function makeKey(object $target, string $key): array + { + return [ + $target::class, + spl_object_hash($target), + $key, + ]; + } +} diff --git a/src/Support/ScheduledTasks/Tasks/Task.php b/src/Support/ScheduledTasks/Tasks/Task.php index 567cfbd..93e3ae1 100644 --- a/src/Support/ScheduledTasks/Tasks/Task.php +++ b/src/Support/ScheduledTasks/Tasks/Task.php @@ -10,11 +10,13 @@ use Lorisleiva\CronTranslator\CronParsingException; use Lorisleiva\CronTranslator\CronTranslator; use Spatie\ScheduleMonitor\Models\MonitoredScheduledTask; +use Spatie\ScheduleMonitor\Support\Concerns\UsesScheduleMonitoringConfigurationsRepository; use Spatie\ScheduleMonitor\Support\Concerns\UsesScheduleMonitoringModels; abstract class Task { use UsesScheduleMonitoringModels; + use UsesScheduleMonitoringConfigurationsRepository; protected Event $event; @@ -46,16 +48,19 @@ public function uniqueId(): string public function name(): ?string { - return $this->event->monitorName ?? $this->defaultName(); + return $this->getScheduleMonitoringConfigurationsRepository()->getMonitorName($this->event) + ?? $this->defaultName(); } public function shouldMonitor(): bool { - if (! isset($this->event->doNotMonitor)) { + $doNotMonitor = $this->getScheduleMonitoringConfigurationsRepository() + ->getDoNotMonitor($this->event); + if (! isset($doNotMonitor)) { return true; } - return ! $this->event->doNotMonitor; + return ! $doNotMonitor; } public function isBeingMonitored(): bool @@ -65,11 +70,13 @@ public function isBeingMonitored(): bool public function shouldMonitorAtOhDear(): bool { - if (! isset($this->event->doNotMonitorAtOhDear)) { + $doNotMonitorAtOhDear = $this->getScheduleMonitoringConfigurationsRepository() + ->getDoNotMonitorAtOhDear($this->event); + if (! isset($doNotMonitorAtOhDear)) { return true; } - return ! $this->event->doNotMonitorAtOhDear; + return ! $doNotMonitorAtOhDear; } public function isBeingMonitoredAtOhDear(): bool @@ -163,7 +170,8 @@ public function lastRunFailed(): bool public function graceTimeInMinutes() { - return $this->event->graceTimeInMinutes ?? config('schedule-monitor.oh_dear.grace_time_in_minutes', 5); + return $this->getScheduleMonitoringConfigurationsRepository()->getGraceTimeInMinutes($this->event) + ?? config('schedule-monitor.oh_dear.grace_time_in_minutes', 5); } public function cronExpression(): string diff --git a/tests/ScheduledTaskSubscriber/ScheduledTaskSubscriberTest.php b/tests/ScheduledTaskSubscriber/ScheduledTaskSubscriberTest.php index 7bb49e2..c0cf8a4 100644 --- a/tests/ScheduledTaskSubscriber/ScheduledTaskSubscriberTest.php +++ b/tests/ScheduledTaskSubscriber/ScheduledTaskSubscriberTest.php @@ -90,6 +90,7 @@ ->monitorName('failing-task'); }); + $this->artisan(SyncCommand::class)->assertExitCode(0); $this->artisan('schedule:run')->assertExitCode(0); diff --git a/tests/Traits/UsesScheduleMonitoringConfigurationsRepositoryTest.php b/tests/Traits/UsesScheduleMonitoringConfigurationsRepositoryTest.php new file mode 100644 index 0000000..c58f67f --- /dev/null +++ b/tests/Traits/UsesScheduleMonitoringConfigurationsRepositoryTest.php @@ -0,0 +1,14 @@ +getScheduleMonitoringConfigurationsRepository(); + + expect($repository)->toBeInstanceOf(ScheduleMonitoringConfigurationsRepository::class); +});