diff --git a/README.md b/README.md index 6775454..eb87a13 100644 --- a/README.md +++ b/README.md @@ -287,6 +287,19 @@ protected function schedule(Schedule $schedule) } ``` +### Disabling Oh Dear for individual tasks + +If you want to have a task monitored by the schedule monitor, but not by Oh Dear, you can tack on `doMonitorAtOhDear` to your scheduled tasks. + +```php +// in app/Console/Kernel.php + +protected function schedule(Schedule $schedule) +{ + $schedule->command('your-command')->daily()->doNotMonitorAtOhDear(); +} +``` + ## Unsupported methods Currently, this package does not work for tasks that use these methods: diff --git a/src/Commands/SyncCommand.php b/src/Commands/SyncCommand.php index 6511f4b..e5bb9f0 100644 --- a/src/Commands/SyncCommand.php +++ b/src/Commands/SyncCommand.php @@ -48,12 +48,12 @@ protected function storeScheduledTasksInDatabase(): self ->map(function (Task $task) { return $this->getMonitoredScheduleTaskModel()->updateOrCreate( ['name' => $task->name()], - [ + array_merge([ 'type' => $task->type(), 'cron_expression' => $task->cronExpression(), 'timezone' => $task->timezone(), 'grace_time_in_minutes' => $task->graceTimeInMinutes(), - ] + ], $task->shouldMonitorAtOhDear() ? [] : ['ping_url' => null]) ); }); @@ -121,7 +121,14 @@ function (CronCheck $cronCheck) { protected function syncMonitoredScheduledTaskWithOhDear(int $siteId): array { - $monitoredScheduledTasks = $this->getMonitoredScheduleTaskModel()->get(); + $monitoredScheduledTasks = $this->getMonitoredScheduleTaskModel() + ->whereIn( + 'name', + ScheduledTasks::createForSchedule() + ->monitoredAtOhDear() + ->map->name() + ) + ->get(); $cronChecks = $monitoredScheduledTasks ->map(function (MonitoredScheduledTask $monitoredScheduledTask) { @@ -145,6 +152,12 @@ protected function pushMonitoredScheduledTaskToOhDear(int $siteId): array { $tasksToRegister = $this->getMonitoredScheduleTaskModel() ->whereNull('registered_on_oh_dear_at') + ->whereIn( + 'name', + ScheduledTasks::createForSchedule() + ->monitoredAtOhDear() + ->map->name() + ) ->get(); $cronChecks = []; diff --git a/src/ScheduleMonitorServiceProvider.php b/src/ScheduleMonitorServiceProvider.php index 4b3da61..8da0cd4 100644 --- a/src/ScheduleMonitorServiceProvider.php +++ b/src/ScheduleMonitorServiceProvider.php @@ -26,6 +26,8 @@ class ScheduleMonitorServiceProvider extends PackageServiceProvider private bool $doNotMonitor; + private bool $doNotMonitorAtOhDear; + private bool $storeOutputInDb; public function configurePackage(Package $package): void @@ -127,6 +129,12 @@ protected function registerSchedulerEventMacros(): self return $this; }); + SchedulerEvent::macro('doNotMonitorAtOhDear', function (bool $bool = true) { + $this->doNotMonitorAtOhDear = $bool; + + return $this; + }); + SchedulerEvent::macro('storeOutputInDb', function () { $this->storeOutputInDb = true; /** @psalm-suppress UndefinedMethod */ diff --git a/src/Support/ScheduledTasks/ScheduledTasks.php b/src/Support/ScheduledTasks/ScheduledTasks.php index 24bd497..b696fa7 100644 --- a/src/Support/ScheduledTasks/ScheduledTasks.php +++ b/src/Support/ScheduledTasks/ScheduledTasks.php @@ -42,6 +42,12 @@ public function uniqueTasks(): Collection ->values(); } + public function monitoredAtOhDear() + { + return $this->uniqueTasks() + ->filter(fn (Task $task) => $task->shouldMonitorAtOhDear()); + } + public function duplicateTasks(): Collection { $uniqueTasksIds = $this->uniqueTasks() diff --git a/src/Support/ScheduledTasks/Tasks/Task.php b/src/Support/ScheduledTasks/Tasks/Task.php index 7e7eecf..818c089 100644 --- a/src/Support/ScheduledTasks/Tasks/Task.php +++ b/src/Support/ScheduledTasks/Tasks/Task.php @@ -63,12 +63,25 @@ public function isBeingMonitored(): bool return ! is_null($this->monitoredScheduledTask); } + public function shouldMonitorAtOhDear(): bool + { + if (! isset($this->event->doNotMonitorAtOhDear)) { + return true; + } + + return ! $this->event->doNotMonitorAtOhDear; + } + public function isBeingMonitoredAtOhDear(): bool { if (! $this->isBeingMonitored()) { return false; } + if (! $this->shouldMonitorAtOhDear()) { + return false; + } + return ! empty($this->monitoredScheduledTask->ping_url); } diff --git a/tests/Commands/SyncCommandTest.php b/tests/Commands/SyncCommandTest.php index 6af906f..faf7b6f 100644 --- a/tests/Commands/SyncCommandTest.php +++ b/tests/Commands/SyncCommandTest.php @@ -98,7 +98,7 @@ $schedule->command('dummy-2')->hourly(); $schedule->command('dummy-3')->daily(); }); - + $this->artisan(SyncCommand::class, ['--keep-old' => true]); $monitoredScheduledTasks = MonitoredScheduledTask::get(); @@ -239,3 +239,44 @@ expect(MonitoredScheduledTask::get())->toHaveCount(1); expect($this->ohDear->getSyncedCronCheckAttributes())->toEqual([]); }); + +it('will not sync tasks with oh dear that should not be', function () { + TestKernel::registerScheduledTasks(function (Schedule $schedule) { + $schedule->command('dummy')->everyMinute()->doNotMonitorAtOhDear(); + }); + + $this->artisan(SyncCommand::class); + + expect(MonitoredScheduledTask::get())->toHaveCount(1); + + expect($this->ohDear->getSyncedCronCheckAttributes())->toEqual([]); + + expect(MonitoredScheduledTask::first()->ping_url)->toBeNull(); +}); + +it('will not keep old ping_urls for tasks not being sent to oh dear', function () { + MonitoredScheduledTask::create([ + 'name' => 'dummy', + 'type' => 'command', + 'cron_expression' => '* * * * *', + 'ping_url' => 'https://ping.ohdear.app/test-ping-url-dummy', + 'registered_on_oh_dear_at' => now()->format('Y-m-d H:i:s'), + 'grace_time_in_minutes' => 5, + 'last_pinged_at' => null, + 'last_started_at' => null, + 'last_finished_at' => null, + 'timezone' => 'UTC', + ]); + + TestKernel::registerScheduledTasks(function (Schedule $schedule) { + $schedule->command('dummy')->everyMinute()->doNotMonitorAtOhDear(); + }); + + $this->artisan(SyncCommand::class); + + expect(MonitoredScheduledTask::get())->toHaveCount(1); + + expect(MonitoredScheduledTask::first()->ping_url)->toBeNull(); + + expect($this->ohDear->getSyncedCronCheckAttributes())->toEqual([]); +});