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

Monitor specific queue for long wait if configured #1174

Closed
wants to merge 2 commits into from
Closed
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
18 changes: 14 additions & 4 deletions src/Listeners/MonitorWaitTimes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 43 additions & 9 deletions tests/Feature/MonitorWaitTimesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -34,23 +38,53 @@ 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]]);

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));
$listener->lastMonitoredAt = CarbonImmutable::now()->subDays(1);
$listener->lastMonitoredAt = CarbonImmutable::now()->subDay();

$listener->handle();

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';
});
}
}