Skip to content

Commit

Permalink
Merge pull request #102 from oddvalue/main
Browse files Browse the repository at this point in the history
Allow tasks to be monitored but not synced with oh dear
  • Loading branch information
freekmurze authored Jan 26, 2024
2 parents 469a698 + 1182c73 commit 823455c
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 4 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
19 changes: 16 additions & 3 deletions src/Commands/SyncCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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])
);
});

Expand Down Expand Up @@ -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) {
Expand All @@ -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 = [];
Expand Down
8 changes: 8 additions & 0 deletions src/ScheduleMonitorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class ScheduleMonitorServiceProvider extends PackageServiceProvider

private bool $doNotMonitor;

private bool $doNotMonitorAtOhDear;

private bool $storeOutputInDb;

public function configurePackage(Package $package): void
Expand Down Expand Up @@ -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 */
Expand Down
6 changes: 6 additions & 0 deletions src/Support/ScheduledTasks/ScheduledTasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
13 changes: 13 additions & 0 deletions src/Support/ScheduledTasks/Tasks/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
43 changes: 42 additions & 1 deletion tests/Commands/SyncCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
$schedule->command('dummy-2')->hourly();
$schedule->command('dummy-3')->daily();
});

$this->artisan(SyncCommand::class, ['--keep-old' => true]);

$monitoredScheduledTasks = MonitoredScheduledTask::get();
Expand Down Expand Up @@ -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([]);
});

0 comments on commit 823455c

Please sign in to comment.