Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ping oh dear when scheduled task has started #85

Merged
merged 4 commits into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config/schedule-monitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,10 @@
* longer than the time configured here.
*/
'retry_job_for_minutes' => 10,

/*
* Sends a starting ping
*/
'should_send_starting_ping' => env('OH_DEAR_SEND_STARTING_PING', false),
],
];
5 changes: 5 additions & 0 deletions src/Models/MonitoredScheduledTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ public function markAsStarting(ScheduledTaskStarting $event): self
'last_started_at' => now(),
]);

if (config('schedule-monitor.oh_dear.should_send_starting_ping') === true) {
$this->pingOhDear($logItem);
}

return $this;
}

Expand Down Expand Up @@ -176,6 +180,7 @@ public function pingOhDear(MonitoredScheduledTaskLogItem $logItem): self
}

if (! in_array($logItem->type, [
$this->getMonitoredScheduleTaskLogItemModel()::TYPE_STARTING,
$this->getMonitoredScheduleTaskLogItemModel()::TYPE_FAILED,
$this->getMonitoredScheduleTaskLogItemModel()::TYPE_FINISHED,
], true)) {
Expand Down
2 changes: 2 additions & 0 deletions src/Support/OhDearPayload/OhDearPayloadFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
use Spatie\ScheduleMonitor\Support\OhDearPayload\Payloads\FailedPayload;
use Spatie\ScheduleMonitor\Support\OhDearPayload\Payloads\FinishedPayload;
use Spatie\ScheduleMonitor\Support\OhDearPayload\Payloads\Payload;
use Spatie\ScheduleMonitor\Support\OhDearPayload\Payloads\StartingPayload;

class OhDearPayloadFactory
{
public static function createForLogItem(MonitoredScheduledTaskLogItem $logItem): ?Payload
{
$payloadClasses = [
StartingPayload::class,
FailedPayload::class,
FinishedPayload::class,
];
Expand Down
2 changes: 1 addition & 1 deletion src/Support/OhDearPayload/Payloads/FinishedPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function url()
public function data(): array
{
return Arr::only($this->logItem->meta ?? [], [
'runtime',
...(config('schedule-monitor.oh_dear.should_send_starting_ping') ? [] : ['runtime']),
'exit_code',
'memory',
]);
Expand Down
23 changes: 23 additions & 0 deletions src/Support/OhDearPayload/Payloads/StartingPayload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Spatie\ScheduleMonitor\Support\OhDearPayload\Payloads;

use Spatie\ScheduleMonitor\Models\MonitoredScheduledTaskLogItem;

class StartingPayload extends Payload
{
public static function canHandle(MonitoredScheduledTaskLogItem $logItem): bool
{
return $logItem->type === MonitoredScheduledTaskLogItem::TYPE_STARTING;
}

public function url()
{
return "{$this->baseUrl()}/starting";
}

public function data(): array
{
return [];
}
}
20 changes: 20 additions & 0 deletions tests/ScheduledTaskSubscriber/PingFinishedToOhDearTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,23 @@
return true;
});
});

it('will not transmit runtime to ohdear when sending starting ping is enabled', function () {
config()->set('schedule-monitor.oh_dear.should_send_starting_ping', true);

dispatch(new PingOhDearJob($this->monitoredScheduledTaskLogItem));

Http::assertSent(function (Request $request) {
$this->assertEquals(
$request->url(),
$this->monitoredScheduledTaskLogItem->monitoredScheduledTask->ping_url . '/finished'
);

expect($request->data())->toEqual([
'exit_code' => 123,
'memory' => 12345,
]);

return true;
});
});
40 changes: 40 additions & 0 deletions tests/ScheduledTaskSubscriber/PingStartingToOhDearTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
use Spatie\ScheduleMonitor\Jobs\PingOhDearJob;
use Spatie\ScheduleMonitor\Models\MonitoredScheduledTaskLogItem;
use Spatie\TestTime\TestTime;

beforeEach(function () {
TestTime::freeze('Y-m-d H:i:s', '2020-01-01 00:00:00');

Http::fake([
'ping.ohdear.app/*' => Http::response('ok', 200),
]);

$this->monitoredScheduledTaskLogItem = MonitoredScheduledTaskLogItem::factory()->create([
'type' => MonitoredScheduledTaskLogItem::TYPE_STARTING,
'meta' => [],
]);
});

it('can ping oh dear when a scheduled task has started', function () {
dispatch(new PingOhDearJob($this->monitoredScheduledTaskLogItem));

$this->assertEquals(
$this->monitoredScheduledTaskLogItem->monitoredScheduledTask->refresh()->last_pinged_at->format('Y-m-d H:i:s'),
now()->format('Y-m-d H:i:s'),
);

Http::assertSent(function (Request $request) {
$this->assertEquals(
$request->url(),
$this->monitoredScheduledTaskLogItem->monitoredScheduledTask->ping_url . '/starting'
);

expect($request->data())->toEqual([]);

return true;
});
});
30 changes: 28 additions & 2 deletions tests/ScheduledTaskSubscriber/ScheduledTaskSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,21 @@
});

it('will fire a job and create a log item when a monitored scheduled task finished', function () {
config()->set('schedule-monitor.oh_dear.should_send_starting_ping', true);

$this->artisan(SyncCommand::class)->assertExitCode(0);
$this->artisan('schedule:run')->assertExitCode(0);

Bus::assertDispatched(function (PingOhDearJob $job) {
$monitoredScheduledTask = $job->logItem->monitoredScheduledTask;

return $monitoredScheduledTask->name === 'dummy-task';
return $monitoredScheduledTask->name === 'dummy-task' && $job->logItem->type === MonitoredScheduledTaskLogItem::TYPE_STARTING;
});

Bus::assertDispatched(function (PingOhDearJob $job) {
$monitoredScheduledTask = $job->logItem->monitoredScheduledTask;

return $monitoredScheduledTask->name === 'dummy-task' && $job->logItem->type === MonitoredScheduledTaskLogItem::TYPE_FINISHED;
});

$logTypes = MonitoredScheduledTask::findByName('dummy-task')->logItems->pluck('type')->toArray();
Expand All @@ -43,6 +51,18 @@
], $logTypes);
});

it('will not not ping oh dear starting endpoint by default', function () {

$this->artisan(SyncCommand::class)->assertExitCode(0);
$this->artisan('schedule:run')->assertExitCode(0);

Bus::assertNotDispatched(function (PingOhDearJob $job) {
$monitoredScheduledTask = $job->logItem->monitoredScheduledTask;

return $monitoredScheduledTask->name === 'dummy-task' && $job->logItem->type === MonitoredScheduledTaskLogItem::TYPE_STARTING;
});
});

it('will log skipped scheduled tasks', function () {
TestKernel::replaceScheduledTasks(function (Schedule $schedule) {
$schedule
Expand All @@ -61,7 +81,7 @@
], $logTypes);
});

it('will log failures of scheduled tasks', function () {
it('will fire a job and log failures of scheduled tasks', function () {
TestKernel::replaceScheduledTasks(function (Schedule $schedule) {
$schedule
->call(function () {
Expand All @@ -74,6 +94,12 @@
$this->artisan(SyncCommand::class)->assertExitCode(0);
$this->artisan('schedule:run')->assertExitCode(0);

Bus::assertDispatched(function (PingOhDearJob $job) {
$monitoredScheduledTask = $job->logItem->monitoredScheduledTask;

return $monitoredScheduledTask->name === 'failing-task' && $job->logItem->type === MonitoredScheduledTaskLogItem::TYPE_FAILED;
});

$logTypes = MonitoredScheduledTask::findByName('failing-task')
->logItems
->pluck('type')
Expand Down