From 3605a971b4975a0dc3b784c69d63dde3784030c8 Mon Sep 17 00:00:00 2001 From: Constantin Graf Date: Thu, 19 Nov 2026 12:08:11 +0100 Subject: [PATCH] Fix stale heartbeat lock, skip queue heartbeat on sync driver and enforce a heartbeat timeout --- .env.example | 1 + .../Heartbeat/HeartbeatSendCommand.php | 16 +++++++-- app/Console/Kernel.php | 3 +- app/Jobs/HeartbeatQueueJob.php | 3 +- config/heartbeat.php | 7 +++- .../Heartbeat/HeartbeatSendCommandTest.php | 33 +++++++++++++++++++ tests/Unit/Console/KernelTest.php | 1 + 7 files changed, 57 insertions(+), 7 deletions(-) diff --git a/.env.example b/.env.example index f8cf0629..c1c9d16a 100644 --- a/.env.example +++ b/.env.example @@ -40,6 +40,7 @@ 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. +# The queue heartbeat is skipped if the queue connection uses the sync driver. # HEARTBEAT_SCHEDULER_URL= # HEARTBEAT_QUEUE_URL= diff --git a/app/Console/Commands/Heartbeat/HeartbeatSendCommand.php b/app/Console/Commands/Heartbeat/HeartbeatSendCommand.php index 3ce1b5a4..ea956e7c 100644 --- a/app/Console/Commands/Heartbeat/HeartbeatSendCommand.php +++ b/app/Console/Commands/Heartbeat/HeartbeatSendCommand.php @@ -50,7 +50,8 @@ class HeartbeatSendCommand extends Command if ($schedulerUrl !== null) { try { - Http::timeout((int) config('heartbeat.timeout', 5)) + Http::timeout((int) config('heartbeat.timeout')) + ->connectTimeout(2) ->get($schedulerUrl) ->throw(); $this->info('Scheduler heartbeat sent.'); @@ -63,8 +64,17 @@ class HeartbeatSendCommand extends Command } if ($queueUrl !== null) { - HeartbeatQueueJob::dispatch(Carbon::now()->getTimestampMs()); - $this->info('Queue heartbeat job dispatched.'); + $queueConnection = config('queue.default'); + if (config('queue.connections.'.$queueConnection.'.driver') === 'sync') { + // With the sync driver the job would run inline in the scheduler, so the heartbeat would not prove that a queue worker is running. + Log::warning('Skipped queue heartbeat, because the queue connection uses the sync driver', [ + 'queue_connection' => $queueConnection, + ]); + $this->warn('Skipped queue heartbeat, because the queue connection uses the sync driver.'); + } else { + HeartbeatQueueJob::dispatch(Carbon::now()->getTimestampMs()); + $this->info('Queue heartbeat job dispatched.'); + } } return self::SUCCESS; diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 15adc309..66a1f982 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -51,8 +51,7 @@ class Kernel extends ConsoleKernel $schedule->command('heartbeat:send') ->when(fn (): bool => filled(config('heartbeat.scheduler_url')) || filled(config('heartbeat.queue_url'))) ->everyMinute() - ->onOneServer() - ->withoutOverlapping(); + ->onOneServer(); } /** diff --git a/app/Jobs/HeartbeatQueueJob.php b/app/Jobs/HeartbeatQueueJob.php index 99d769b6..db3c4b8a 100644 --- a/app/Jobs/HeartbeatQueueJob.php +++ b/app/Jobs/HeartbeatQueueJob.php @@ -45,7 +45,8 @@ class HeartbeatQueueJob implements ShouldQueue $url = str_replace('{latency_ms}', (string) $latencyMs, $queueUrl); try { - Http::timeout((int) config('heartbeat.timeout', 5)) + Http::timeout((int) config('heartbeat.timeout')) + ->connectTimeout(2) ->get($url) ->throw(); } catch (Throwable $exception) { diff --git a/config/heartbeat.php b/config/heartbeat.php index 1fa20a2a..87774d31 100644 --- a/config/heartbeat.php +++ b/config/heartbeat.php @@ -16,6 +16,9 @@ return [ | The queue URL may contain the placeholder "{latency_ms}", which is replaced | with the time in milliseconds between dispatching and executing the job. | + | The queue heartbeat is skipped if the queue connection uses the "sync" + | driver, since there is no queue worker to monitor in that case. + | */ 'scheduler_url' => env('HEARTBEAT_SCHEDULER_URL'), @@ -24,6 +27,8 @@ return [ /* | Timeout in seconds for the HTTP request to the heartbeat URLs. + | An empty or zero value falls back to the default, since a timeout of 0 + | would disable the timeout. */ - 'timeout' => (int) env('HEARTBEAT_TIMEOUT', 5), + 'timeout' => max(1, (int) (env('HEARTBEAT_TIMEOUT') ?: 5)), ]; diff --git a/tests/Unit/Console/Commands/Heartbeat/HeartbeatSendCommandTest.php b/tests/Unit/Console/Commands/Heartbeat/HeartbeatSendCommandTest.php index 8a746f04..bbf736d3 100644 --- a/tests/Unit/Console/Commands/Heartbeat/HeartbeatSendCommandTest.php +++ b/tests/Unit/Console/Commands/Heartbeat/HeartbeatSendCommandTest.php @@ -11,9 +11,11 @@ use Illuminate\Http\Client\ConnectionException; use Illuminate\Http\Client\Request; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Http; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Queue; use PHPUnit\Framework\Attributes\CoversClass; use Tests\TestCase; +use TiMacDonald\Log\LogEntry; #[CoversClass(HeartbeatSendCommand::class)] class HeartbeatSendCommandTest extends TestCase @@ -25,6 +27,7 @@ class HeartbeatSendCommandTest extends TestCase config([ 'heartbeat.scheduler_url' => 'https://monitor.example.com/scheduler', 'heartbeat.queue_url' => 'https://monitor.example.com/queue', + 'queue.default' => 'database', ]); Http::fake([ 'https://monitor.example.com/scheduler' => Http::response('OK', 200), @@ -69,6 +72,7 @@ class HeartbeatSendCommandTest extends TestCase config([ 'heartbeat.scheduler_url' => null, 'heartbeat.queue_url' => 'https://monitor.example.com/queue', + 'queue.default' => 'database', ]); Http::fake(); Queue::fake(); @@ -107,6 +111,7 @@ class HeartbeatSendCommandTest extends TestCase config([ 'heartbeat.scheduler_url' => 'https://monitor.example.com/scheduler', 'heartbeat.queue_url' => 'https://monitor.example.com/queue', + 'queue.default' => 'database', ]); Http::fake([ 'https://monitor.example.com/scheduler' => Http::response(null, 500), @@ -141,4 +146,32 @@ class HeartbeatSendCommandTest extends TestCase // Assert $this->assertSame(Command::SUCCESS, $exitCode); } + + public function test_skips_queue_heartbeat_if_queue_connection_uses_sync_driver(): void + { + // Arrange + config([ + 'heartbeat.scheduler_url' => 'https://monitor.example.com/scheduler', + 'heartbeat.queue_url' => 'https://monitor.example.com/queue', + 'queue.default' => 'sync', + ]); + 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'); + Queue::assertNotPushed(HeartbeatQueueJob::class); + Log::assertLoggedTimes(fn (LogEntry $log): bool => $log->level === 'warning' + && $log->message === 'Skipped queue heartbeat, because the queue connection uses the sync driver' + && $log->context === ['queue_connection' => 'sync'], + 1 + ); + } } diff --git a/tests/Unit/Console/KernelTest.php b/tests/Unit/Console/KernelTest.php index 4d03873f..d9c5d9ee 100644 --- a/tests/Unit/Console/KernelTest.php +++ b/tests/Unit/Console/KernelTest.php @@ -128,6 +128,7 @@ class KernelTest extends TestCase $this->assertCount(1, $events); $this->assertSame('* * * * *', $events[0]->expression); $this->assertTrue($events[0]->onOneServer); + $this->assertFalse($events[0]->withoutOverlapping); $this->assertTrue($events[0]->filtersPass(app())); }