mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-12-06 17:52:16 +00:00
Fix stale heartbeat lock, skip queue heartbeat on sync driver and enforce a heartbeat timeout
This commit is contained in:
committed by
Constantin Graf
parent
9b742372f0
commit
3605a971b4
@@ -40,6 +40,7 @@ QUEUE_CONNECTION=sync
|
|||||||
|
|
||||||
# Heartbeat (optional push/heartbeat monitor URLs to check that the scheduler and queue are running)
|
# 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 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_SCHEDULER_URL=
|
||||||
# HEARTBEAT_QUEUE_URL=
|
# HEARTBEAT_QUEUE_URL=
|
||||||
|
|
||||||
|
|||||||
@@ -50,7 +50,8 @@ class HeartbeatSendCommand extends Command
|
|||||||
|
|
||||||
if ($schedulerUrl !== null) {
|
if ($schedulerUrl !== null) {
|
||||||
try {
|
try {
|
||||||
Http::timeout((int) config('heartbeat.timeout', 5))
|
Http::timeout((int) config('heartbeat.timeout'))
|
||||||
|
->connectTimeout(2)
|
||||||
->get($schedulerUrl)
|
->get($schedulerUrl)
|
||||||
->throw();
|
->throw();
|
||||||
$this->info('Scheduler heartbeat sent.');
|
$this->info('Scheduler heartbeat sent.');
|
||||||
@@ -63,8 +64,17 @@ class HeartbeatSendCommand extends Command
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($queueUrl !== null) {
|
if ($queueUrl !== null) {
|
||||||
HeartbeatQueueJob::dispatch(Carbon::now()->getTimestampMs());
|
$queueConnection = config('queue.default');
|
||||||
$this->info('Queue heartbeat job dispatched.');
|
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;
|
return self::SUCCESS;
|
||||||
|
|||||||
@@ -51,8 +51,7 @@ class Kernel extends ConsoleKernel
|
|||||||
$schedule->command('heartbeat:send')
|
$schedule->command('heartbeat:send')
|
||||||
->when(fn (): bool => filled(config('heartbeat.scheduler_url')) || filled(config('heartbeat.queue_url')))
|
->when(fn (): bool => filled(config('heartbeat.scheduler_url')) || filled(config('heartbeat.queue_url')))
|
||||||
->everyMinute()
|
->everyMinute()
|
||||||
->onOneServer()
|
->onOneServer();
|
||||||
->withoutOverlapping();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -45,7 +45,8 @@ class HeartbeatQueueJob implements ShouldQueue
|
|||||||
$url = str_replace('{latency_ms}', (string) $latencyMs, $queueUrl);
|
$url = str_replace('{latency_ms}', (string) $latencyMs, $queueUrl);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Http::timeout((int) config('heartbeat.timeout', 5))
|
Http::timeout((int) config('heartbeat.timeout'))
|
||||||
|
->connectTimeout(2)
|
||||||
->get($url)
|
->get($url)
|
||||||
->throw();
|
->throw();
|
||||||
} catch (Throwable $exception) {
|
} catch (Throwable $exception) {
|
||||||
|
|||||||
@@ -16,6 +16,9 @@ return [
|
|||||||
| The queue URL may contain the placeholder "{latency_ms}", which is replaced
|
| The queue URL may contain the placeholder "{latency_ms}", which is replaced
|
||||||
| with the time in milliseconds between dispatching and executing the job.
|
| 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'),
|
'scheduler_url' => env('HEARTBEAT_SCHEDULER_URL'),
|
||||||
@@ -24,6 +27,8 @@ return [
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
| Timeout in seconds for the HTTP request to the heartbeat URLs.
|
| 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)),
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -11,9 +11,11 @@ use Illuminate\Http\Client\ConnectionException;
|
|||||||
use Illuminate\Http\Client\Request;
|
use Illuminate\Http\Client\Request;
|
||||||
use Illuminate\Support\Carbon;
|
use Illuminate\Support\Carbon;
|
||||||
use Illuminate\Support\Facades\Http;
|
use Illuminate\Support\Facades\Http;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
use Illuminate\Support\Facades\Queue;
|
use Illuminate\Support\Facades\Queue;
|
||||||
use PHPUnit\Framework\Attributes\CoversClass;
|
use PHPUnit\Framework\Attributes\CoversClass;
|
||||||
use Tests\TestCase;
|
use Tests\TestCase;
|
||||||
|
use TiMacDonald\Log\LogEntry;
|
||||||
|
|
||||||
#[CoversClass(HeartbeatSendCommand::class)]
|
#[CoversClass(HeartbeatSendCommand::class)]
|
||||||
class HeartbeatSendCommandTest extends TestCase
|
class HeartbeatSendCommandTest extends TestCase
|
||||||
@@ -25,6 +27,7 @@ class HeartbeatSendCommandTest extends TestCase
|
|||||||
config([
|
config([
|
||||||
'heartbeat.scheduler_url' => 'https://monitor.example.com/scheduler',
|
'heartbeat.scheduler_url' => 'https://monitor.example.com/scheduler',
|
||||||
'heartbeat.queue_url' => 'https://monitor.example.com/queue',
|
'heartbeat.queue_url' => 'https://monitor.example.com/queue',
|
||||||
|
'queue.default' => 'database',
|
||||||
]);
|
]);
|
||||||
Http::fake([
|
Http::fake([
|
||||||
'https://monitor.example.com/scheduler' => Http::response('OK', 200),
|
'https://monitor.example.com/scheduler' => Http::response('OK', 200),
|
||||||
@@ -69,6 +72,7 @@ class HeartbeatSendCommandTest extends TestCase
|
|||||||
config([
|
config([
|
||||||
'heartbeat.scheduler_url' => null,
|
'heartbeat.scheduler_url' => null,
|
||||||
'heartbeat.queue_url' => 'https://monitor.example.com/queue',
|
'heartbeat.queue_url' => 'https://monitor.example.com/queue',
|
||||||
|
'queue.default' => 'database',
|
||||||
]);
|
]);
|
||||||
Http::fake();
|
Http::fake();
|
||||||
Queue::fake();
|
Queue::fake();
|
||||||
@@ -107,6 +111,7 @@ class HeartbeatSendCommandTest extends TestCase
|
|||||||
config([
|
config([
|
||||||
'heartbeat.scheduler_url' => 'https://monitor.example.com/scheduler',
|
'heartbeat.scheduler_url' => 'https://monitor.example.com/scheduler',
|
||||||
'heartbeat.queue_url' => 'https://monitor.example.com/queue',
|
'heartbeat.queue_url' => 'https://monitor.example.com/queue',
|
||||||
|
'queue.default' => 'database',
|
||||||
]);
|
]);
|
||||||
Http::fake([
|
Http::fake([
|
||||||
'https://monitor.example.com/scheduler' => Http::response(null, 500),
|
'https://monitor.example.com/scheduler' => Http::response(null, 500),
|
||||||
@@ -141,4 +146,32 @@ class HeartbeatSendCommandTest extends TestCase
|
|||||||
// Assert
|
// Assert
|
||||||
$this->assertSame(Command::SUCCESS, $exitCode);
|
$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
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -128,6 +128,7 @@ class KernelTest extends TestCase
|
|||||||
$this->assertCount(1, $events);
|
$this->assertCount(1, $events);
|
||||||
$this->assertSame('* * * * *', $events[0]->expression);
|
$this->assertSame('* * * * *', $events[0]->expression);
|
||||||
$this->assertTrue($events[0]->onOneServer);
|
$this->assertTrue($events[0]->onOneServer);
|
||||||
|
$this->assertFalse($events[0]->withoutOverlapping);
|
||||||
$this->assertTrue($events[0]->filtersPass(app()));
|
$this->assertTrue($events[0]->filtersPass(app()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user