mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-12-06 17:52:16 +00:00
Add heartbeat monitoring for the scheduler and the queue
This commit is contained in:
committed by
Constantin Graf
parent
84af013881
commit
9b742372f0
@@ -38,6 +38,11 @@ CACHE_DRIVER=file
|
||||
# Queue
|
||||
QUEUE_CONNECTION=sync
|
||||
|
||||
# Heartbeat (optional push/heartbeat monitor URLs to check that the scheduler and queue are running)
|
||||
# The queue URL may contain the placeholder {latency_ms}, which is replaced with the queue latency.
|
||||
# HEARTBEAT_SCHEDULER_URL=
|
||||
# HEARTBEAT_QUEUE_URL=
|
||||
|
||||
# Session
|
||||
SESSION_DRIVER=database
|
||||
SESSION_LIFETIME=120
|
||||
|
||||
72
app/Console/Commands/Heartbeat/HeartbeatSendCommand.php
Normal file
72
app/Console/Commands/Heartbeat/HeartbeatSendCommand.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Console\Commands\Heartbeat;
|
||||
|
||||
use App\Jobs\HeartbeatQueueJob;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Throwable;
|
||||
|
||||
class HeartbeatSendCommand extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'heartbeat:send';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Sends the scheduler heartbeat and dispatches the queue heartbeat job to the configured monitoring URLs.';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle(): int
|
||||
{
|
||||
$schedulerUrl = config('heartbeat.scheduler_url');
|
||||
$queueUrl = config('heartbeat.queue_url');
|
||||
|
||||
if (! is_string($schedulerUrl) || $schedulerUrl === '') {
|
||||
$schedulerUrl = null;
|
||||
}
|
||||
if (! is_string($queueUrl) || $queueUrl === '') {
|
||||
$queueUrl = null;
|
||||
}
|
||||
|
||||
if ($schedulerUrl === null && $queueUrl === null) {
|
||||
$this->info('No heartbeat URLs configured, nothing to do.');
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
if ($schedulerUrl !== null) {
|
||||
try {
|
||||
Http::timeout((int) config('heartbeat.timeout', 5))
|
||||
->get($schedulerUrl)
|
||||
->throw();
|
||||
$this->info('Scheduler heartbeat sent.');
|
||||
} catch (Throwable $exception) {
|
||||
Log::warning('Failed to send scheduler heartbeat', [
|
||||
'exception' => $exception->getMessage(),
|
||||
]);
|
||||
$this->warn('Failed to send scheduler heartbeat, check the logs for more information.');
|
||||
}
|
||||
}
|
||||
|
||||
if ($queueUrl !== null) {
|
||||
HeartbeatQueueJob::dispatch(Carbon::now()->getTimestampMs());
|
||||
$this->info('Queue heartbeat job dispatched.');
|
||||
}
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -47,6 +47,12 @@ class Kernel extends ConsoleKernel
|
||||
$schedule->command('self-host:database-consistency')
|
||||
->when(fn (): bool => config('scheduling.tasks.self_hosting_database_consistency'))
|
||||
->everySixHours();
|
||||
|
||||
$schedule->command('heartbeat:send')
|
||||
->when(fn (): bool => filled(config('heartbeat.scheduler_url')) || filled(config('heartbeat.queue_url')))
|
||||
->everyMinute()
|
||||
->onOneServer()
|
||||
->withoutOverlapping();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
57
app/Jobs/HeartbeatQueueJob.php
Normal file
57
app/Jobs/HeartbeatQueueJob.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Throwable;
|
||||
|
||||
class HeartbeatQueueJob implements ShouldQueue
|
||||
{
|
||||
use Dispatchable;
|
||||
use InteractsWithQueue;
|
||||
use Queueable;
|
||||
|
||||
/**
|
||||
* A heartbeat is not retried, since a retried heartbeat would be stale.
|
||||
*/
|
||||
public int $tries = 1;
|
||||
|
||||
/**
|
||||
* @param int $dispatchedAtMs Unix timestamp in milliseconds of when the job was dispatched
|
||||
*/
|
||||
public function __construct(
|
||||
public readonly int $dispatchedAtMs
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*/
|
||||
public function handle(): void
|
||||
{
|
||||
$queueUrl = config('heartbeat.queue_url');
|
||||
if (! is_string($queueUrl) || $queueUrl === '') {
|
||||
return;
|
||||
}
|
||||
|
||||
$latencyMs = max(0, Carbon::now()->getTimestampMs() - $this->dispatchedAtMs);
|
||||
$url = str_replace('{latency_ms}', (string) $latencyMs, $queueUrl);
|
||||
|
||||
try {
|
||||
Http::timeout((int) config('heartbeat.timeout', 5))
|
||||
->get($url)
|
||||
->throw();
|
||||
} catch (Throwable $exception) {
|
||||
Log::warning('Failed to send queue heartbeat', [
|
||||
'exception' => $exception->getMessage(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
29
config/heartbeat.php
Normal file
29
config/heartbeat.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
return [
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Heartbeat URLs
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| URLs of an external monitoring service (push/heartbeat monitors) that are
|
||||
| called regularly to signal that the scheduler and the queue are working.
|
||||
| If a URL is not set, the corresponding heartbeat is disabled.
|
||||
|
|
||||
| The queue URL may contain the placeholder "{latency_ms}", which is replaced
|
||||
| with the time in milliseconds between dispatching and executing the job.
|
||||
|
|
||||
*/
|
||||
|
||||
'scheduler_url' => env('HEARTBEAT_SCHEDULER_URL'),
|
||||
|
||||
'queue_url' => env('HEARTBEAT_QUEUE_URL'),
|
||||
|
||||
/*
|
||||
| Timeout in seconds for the HTTP request to the heartbeat URLs.
|
||||
*/
|
||||
'timeout' => (int) env('HEARTBEAT_TIMEOUT', 5),
|
||||
];
|
||||
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\Console\Commands\Heartbeat;
|
||||
|
||||
use App\Console\Commands\Heartbeat\HeartbeatSendCommand;
|
||||
use App\Jobs\HeartbeatQueueJob;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Http\Client\ConnectionException;
|
||||
use Illuminate\Http\Client\Request;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Queue;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use Tests\TestCase;
|
||||
|
||||
#[CoversClass(HeartbeatSendCommand::class)]
|
||||
class HeartbeatSendCommandTest extends TestCase
|
||||
{
|
||||
public function test_sends_scheduler_heartbeat_and_dispatches_queue_job_if_both_urls_are_configured(): void
|
||||
{
|
||||
// Arrange
|
||||
Carbon::setTestNow(Carbon::createFromTimestampMs(1_700_000_000_123));
|
||||
config([
|
||||
'heartbeat.scheduler_url' => 'https://monitor.example.com/scheduler',
|
||||
'heartbeat.queue_url' => 'https://monitor.example.com/queue',
|
||||
]);
|
||||
Http::fake([
|
||||
'https://monitor.example.com/scheduler' => Http::response('OK', 200),
|
||||
]);
|
||||
Queue::fake();
|
||||
|
||||
// Act
|
||||
$exitCode = $this->withoutMockingConsoleOutput()->artisan('heartbeat:send');
|
||||
|
||||
// Assert
|
||||
$this->assertSame(Command::SUCCESS, $exitCode);
|
||||
Http::assertSentCount(1);
|
||||
Http::assertSent(fn (Request $request): bool => $request->url() === 'https://monitor.example.com/scheduler'
|
||||
&& $request->method() === 'GET');
|
||||
Queue::assertPushed(HeartbeatQueueJob::class, fn (HeartbeatQueueJob $job): bool => $job->dispatchedAtMs === 1_700_000_000_123);
|
||||
}
|
||||
|
||||
public function test_only_sends_scheduler_heartbeat_if_queue_url_is_not_configured(): void
|
||||
{
|
||||
// Arrange
|
||||
config([
|
||||
'heartbeat.scheduler_url' => 'https://monitor.example.com/scheduler',
|
||||
'heartbeat.queue_url' => null,
|
||||
]);
|
||||
Http::fake([
|
||||
'https://monitor.example.com/scheduler' => Http::response('OK', 200),
|
||||
]);
|
||||
Queue::fake();
|
||||
|
||||
// Act
|
||||
$exitCode = $this->withoutMockingConsoleOutput()->artisan('heartbeat:send');
|
||||
|
||||
// Assert
|
||||
$this->assertSame(Command::SUCCESS, $exitCode);
|
||||
Http::assertSentCount(1);
|
||||
Queue::assertNotPushed(HeartbeatQueueJob::class);
|
||||
}
|
||||
|
||||
public function test_only_dispatches_queue_job_if_scheduler_url_is_not_configured(): void
|
||||
{
|
||||
// Arrange
|
||||
config([
|
||||
'heartbeat.scheduler_url' => null,
|
||||
'heartbeat.queue_url' => 'https://monitor.example.com/queue',
|
||||
]);
|
||||
Http::fake();
|
||||
Queue::fake();
|
||||
|
||||
// Act
|
||||
$exitCode = $this->withoutMockingConsoleOutput()->artisan('heartbeat:send');
|
||||
|
||||
// Assert
|
||||
$this->assertSame(Command::SUCCESS, $exitCode);
|
||||
Http::assertNothingSent();
|
||||
Queue::assertPushed(HeartbeatQueueJob::class);
|
||||
}
|
||||
|
||||
public function test_does_nothing_if_no_url_is_configured(): void
|
||||
{
|
||||
// Arrange
|
||||
config([
|
||||
'heartbeat.scheduler_url' => null,
|
||||
'heartbeat.queue_url' => '',
|
||||
]);
|
||||
Http::fake();
|
||||
Queue::fake();
|
||||
|
||||
// Act
|
||||
$exitCode = $this->withoutMockingConsoleOutput()->artisan('heartbeat:send');
|
||||
|
||||
// Assert
|
||||
$this->assertSame(Command::SUCCESS, $exitCode);
|
||||
Http::assertNothingSent();
|
||||
Queue::assertNothingPushed();
|
||||
}
|
||||
|
||||
public function test_succeeds_and_still_dispatches_queue_job_if_scheduler_heartbeat_returns_error_status(): void
|
||||
{
|
||||
// Arrange
|
||||
config([
|
||||
'heartbeat.scheduler_url' => 'https://monitor.example.com/scheduler',
|
||||
'heartbeat.queue_url' => 'https://monitor.example.com/queue',
|
||||
]);
|
||||
Http::fake([
|
||||
'https://monitor.example.com/scheduler' => Http::response(null, 500),
|
||||
]);
|
||||
Queue::fake();
|
||||
|
||||
// Act
|
||||
$exitCode = $this->withoutMockingConsoleOutput()->artisan('heartbeat:send');
|
||||
|
||||
// Assert
|
||||
$this->assertSame(Command::SUCCESS, $exitCode);
|
||||
Queue::assertPushed(HeartbeatQueueJob::class);
|
||||
}
|
||||
|
||||
public function test_succeeds_if_scheduler_heartbeat_times_out(): void
|
||||
{
|
||||
// Arrange
|
||||
config([
|
||||
'heartbeat.scheduler_url' => 'https://monitor.example.com/scheduler',
|
||||
'heartbeat.queue_url' => null,
|
||||
]);
|
||||
Http::fake([
|
||||
'https://monitor.example.com/scheduler' => function (): void {
|
||||
throw new ConnectionException('Connection timed out');
|
||||
},
|
||||
]);
|
||||
Queue::fake();
|
||||
|
||||
// Act
|
||||
$exitCode = $this->withoutMockingConsoleOutput()->artisan('heartbeat:send');
|
||||
|
||||
// Assert
|
||||
$this->assertSame(Command::SUCCESS, $exitCode);
|
||||
}
|
||||
}
|
||||
@@ -111,4 +111,40 @@ class KernelTest extends TestCase
|
||||
// Assert
|
||||
$this->assertCount(0, $events);
|
||||
}
|
||||
|
||||
public function test_heartbeat_runs_every_minute_if_a_heartbeat_url_is_configured(): void
|
||||
{
|
||||
// Arrange
|
||||
config([
|
||||
'heartbeat.scheduler_url' => null,
|
||||
'heartbeat.queue_url' => 'https://monitor.example.com/queue',
|
||||
]);
|
||||
|
||||
// Act
|
||||
$schedule = app()->make(Kernel::class)->resolveConsoleSchedule();
|
||||
$events = collect($schedule->events())->filter(fn ($event) => str_contains($event->command, 'heartbeat:send'))->values();
|
||||
|
||||
// Assert
|
||||
$this->assertCount(1, $events);
|
||||
$this->assertSame('* * * * *', $events[0]->expression);
|
||||
$this->assertTrue($events[0]->onOneServer);
|
||||
$this->assertTrue($events[0]->filtersPass(app()));
|
||||
}
|
||||
|
||||
public function test_heartbeat_does_not_run_if_no_heartbeat_url_is_configured(): void
|
||||
{
|
||||
// Arrange
|
||||
config([
|
||||
'heartbeat.scheduler_url' => null,
|
||||
'heartbeat.queue_url' => null,
|
||||
]);
|
||||
|
||||
// Act
|
||||
$schedule = app()->make(Kernel::class)->resolveConsoleSchedule();
|
||||
$events = collect($schedule->events())->filter(fn ($event) => str_contains($event->command, 'heartbeat:send'))->values();
|
||||
|
||||
// Assert
|
||||
$this->assertCount(1, $events);
|
||||
$this->assertFalse($events[0]->filtersPass(app()));
|
||||
}
|
||||
}
|
||||
|
||||
124
tests/Unit/Jobs/HeartbeatQueueJobTest.php
Normal file
124
tests/Unit/Jobs/HeartbeatQueueJobTest.php
Normal file
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\Jobs;
|
||||
|
||||
use App\Jobs\HeartbeatQueueJob;
|
||||
use Illuminate\Http\Client\ConnectionException;
|
||||
use Illuminate\Http\Client\Request;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use Tests\TestCase;
|
||||
|
||||
#[CoversClass(HeartbeatQueueJob::class)]
|
||||
class HeartbeatQueueJobTest extends TestCase
|
||||
{
|
||||
public function test_replaces_latency_placeholder_with_time_between_dispatch_and_execution(): void
|
||||
{
|
||||
// Arrange
|
||||
Carbon::setTestNow(Carbon::createFromTimestampMs(1_700_000_001_500));
|
||||
config([
|
||||
'heartbeat.queue_url' => 'https://monitor.example.com/queue?status=up&ping={latency_ms}',
|
||||
]);
|
||||
Http::fake();
|
||||
$job = new HeartbeatQueueJob(1_700_000_000_250);
|
||||
|
||||
// Act
|
||||
$job->handle();
|
||||
|
||||
// Assert
|
||||
Http::assertSentCount(1);
|
||||
Http::assertSent(fn (Request $request): bool => $request->url() === 'https://monitor.example.com/queue?status=up&ping=1250'
|
||||
&& $request->method() === 'GET');
|
||||
}
|
||||
|
||||
public function test_calls_url_unchanged_if_it_has_no_placeholder(): void
|
||||
{
|
||||
// Arrange
|
||||
config([
|
||||
'heartbeat.queue_url' => 'https://monitor.example.com/queue',
|
||||
]);
|
||||
Http::fake();
|
||||
$job = new HeartbeatQueueJob(Carbon::now()->getTimestampMs());
|
||||
|
||||
// Act
|
||||
$job->handle();
|
||||
|
||||
// Assert
|
||||
Http::assertSentCount(1);
|
||||
Http::assertSent(fn (Request $request): bool => $request->url() === 'https://monitor.example.com/queue');
|
||||
}
|
||||
|
||||
public function test_latency_is_never_negative(): void
|
||||
{
|
||||
// Arrange
|
||||
Carbon::setTestNow(Carbon::createFromTimestampMs(1_700_000_000_000));
|
||||
config([
|
||||
'heartbeat.queue_url' => 'https://monitor.example.com/queue?ping={latency_ms}',
|
||||
]);
|
||||
Http::fake();
|
||||
$job = new HeartbeatQueueJob(1_700_000_005_000);
|
||||
|
||||
// Act
|
||||
$job->handle();
|
||||
|
||||
// Assert
|
||||
Http::assertSent(fn (Request $request): bool => $request->url() === 'https://monitor.example.com/queue?ping=0');
|
||||
}
|
||||
|
||||
public function test_does_nothing_if_queue_url_is_not_configured(): void
|
||||
{
|
||||
// Arrange
|
||||
config([
|
||||
'heartbeat.queue_url' => null,
|
||||
]);
|
||||
Http::fake();
|
||||
$job = new HeartbeatQueueJob(Carbon::now()->getTimestampMs());
|
||||
|
||||
// Act
|
||||
$job->handle();
|
||||
|
||||
// Assert
|
||||
Http::assertNothingSent();
|
||||
}
|
||||
|
||||
public function test_does_not_throw_if_request_fails(): void
|
||||
{
|
||||
// Arrange
|
||||
config([
|
||||
'heartbeat.queue_url' => 'https://monitor.example.com/queue',
|
||||
]);
|
||||
Http::fake([
|
||||
'https://monitor.example.com/queue' => function (): void {
|
||||
throw new ConnectionException('Connection timed out');
|
||||
},
|
||||
]);
|
||||
$job = new HeartbeatQueueJob(Carbon::now()->getTimestampMs());
|
||||
|
||||
// Act
|
||||
$job->handle();
|
||||
|
||||
// Assert
|
||||
Http::assertSentCount(0);
|
||||
}
|
||||
|
||||
public function test_does_not_throw_if_response_has_error_status(): void
|
||||
{
|
||||
// Arrange
|
||||
config([
|
||||
'heartbeat.queue_url' => 'https://monitor.example.com/queue',
|
||||
]);
|
||||
Http::fake([
|
||||
'https://monitor.example.com/queue' => Http::response(null, 500),
|
||||
]);
|
||||
$job = new HeartbeatQueueJob(Carbon::now()->getTimestampMs());
|
||||
|
||||
// Act
|
||||
$job->handle();
|
||||
|
||||
// Assert
|
||||
Http::assertSentCount(1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user