From d7aec4ea543c80794685f4e89343d8dde607f3c3 Mon Sep 17 00:00:00 2001 From: Jason McCreary Date: Thu, 11 Aug 2022 13:16:48 -0400 Subject: [PATCH 1/2] Calculate wait for configured queues --- src/Listeners/MonitorWaitTimes.php | 18 +++++++--- tests/Feature/MonitorWaitTimesTest.php | 48 ++++++++++++++++++++++---- 2 files changed, 55 insertions(+), 11 deletions(-) diff --git a/src/Listeners/MonitorWaitTimes.php b/src/Listeners/MonitorWaitTimes.php index 7ac98d53..09ecb815 100644 --- a/src/Listeners/MonitorWaitTimes.php +++ b/src/Listeners/MonitorWaitTimes.php @@ -46,10 +46,20 @@ public function handle() return; } - // Here we will calculate the wait time in seconds for each of the queues that - // the application is working. Then, we will filter the results to find the - // queues with the longest wait times and raise events for each of these. - $results = app(WaitTimeCalculator::class)->calculate(); + // Here we will calculate the wait time in seconds for each of the queues + // the application is working or monitoring. Then we will compare the + // results against the configured times for any long wait times. + $waitTime = app(WaitTimeCalculator::class); + $results = $waitTime->calculate(); + + $results = collect(config('horizon.waits')) + ->diffKeys($results) + ->filter() + ->map(function ($wait, $queue) use ($waitTime) { + return $waitTime->calculateFor($queue); + }) + ->merge($results) + ->all(); $long = collect($results)->filter(function ($wait, $queue) { return config("horizon.waits.{$queue}") !== 0 diff --git a/tests/Feature/MonitorWaitTimesTest.php b/tests/Feature/MonitorWaitTimesTest.php index 1d561bce..76557fea 100644 --- a/tests/Feature/MonitorWaitTimesTest.php +++ b/tests/Feature/MonitorWaitTimesTest.php @@ -15,13 +15,17 @@ class MonitorWaitTimesTest extends IntegrationTest { public function test_queues_with_long_waits_are_found() { + config(['horizon.waits' => []]); + Event::fake(); $calc = Mockery::mock(WaitTimeCalculator::class); - $calc->shouldReceive('calculate')->andReturn([ - 'redis:test-queue' => 10, - 'redis:test-queue-2' => 80, - ]); + $calc->expects('calculate') + ->withNoArgs() + ->andReturn([ + 'redis:test-queue' => 10, + 'redis:test-queue-2' => 80, + ]); $this->app->instance(WaitTimeCalculator::class, $calc); $listener = new MonitorWaitTimes(resolve(MetricsRepository::class)); @@ -41,9 +45,11 @@ public function test_queue_ignores_long_waits() Event::fake(); $calc = Mockery::mock(WaitTimeCalculator::class); - $calc->expects('calculate')->andReturn([ - 'redis:ignore-queue' => 10, - ]); + $calc->expects('calculate') + ->withNoArgs() + ->andReturn([ + 'redis:ignore-queue' => 10, + ]); $this->app->instance(WaitTimeCalculator::class, $calc); $listener = new MonitorWaitTimes(resolve(MetricsRepository::class)); @@ -53,4 +59,32 @@ public function test_queue_ignores_long_waits() Event::assertNotDispatched(LongWaitDetected::class); } + + public function test_monitors_specific_queue_with_long_wait() + { + config(['horizon.waits' => ['redis:default' => 0, 'redis:shared-1' => 10]]); + + Event::fake(); + + $calc = Mockery::mock(WaitTimeCalculator::class); + $calc->expects('calculate') + ->withNoArgs() + ->andReturn([ + 'redis:default' => 90, + 'redis:shared-1,shared-2' => 25, + ]); + $calc->expects('calculateFor') + ->with('redis:shared-1') + ->andReturn(15); + $this->app->instance(WaitTimeCalculator::class, $calc); + + $listener = new MonitorWaitTimes(resolve(MetricsRepository::class)); + $listener->lastMonitoredAt = CarbonImmutable::now()->subDay(); + + $listener->handle(); + + Event::assertDispatched(LongWaitDetected::class, function ($event) { + return $event->connection == 'redis' && $event->queue == 'shared-1'; + }); + } } From 44dbdf51251038029cf3a037f766b14c3d793f3b Mon Sep 17 00:00:00 2001 From: Jason McCreary Date: Thu, 11 Aug 2022 13:22:58 -0400 Subject: [PATCH 2/2] Boyscouting --- tests/Feature/MonitorWaitTimesTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Feature/MonitorWaitTimesTest.php b/tests/Feature/MonitorWaitTimesTest.php index 76557fea..410b34c9 100644 --- a/tests/Feature/MonitorWaitTimesTest.php +++ b/tests/Feature/MonitorWaitTimesTest.php @@ -38,7 +38,7 @@ public function test_queues_with_long_waits_are_found() }); } - public function test_queue_ignores_long_waits() + public function test_ignores_queue_with_long_wait() { config(['horizon.waits' => ['redis:ignore-queue' => 0]]); @@ -53,7 +53,7 @@ public function test_queue_ignores_long_waits() $this->app->instance(WaitTimeCalculator::class, $calc); $listener = new MonitorWaitTimes(resolve(MetricsRepository::class)); - $listener->lastMonitoredAt = CarbonImmutable::now()->subDays(1); + $listener->lastMonitoredAt = CarbonImmutable::now()->subDay(); $listener->handle();