Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmccreary committed Aug 11, 2022
1 parent 23ecbea commit 4a2c1e4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
10 changes: 6 additions & 4 deletions src/Listeners/MonitorWaitTimes.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,18 @@ public function handle()
return;
}

// Here we will calculate the wait time in seconds for each of the queues the
// application is working or configured to monitor. Then we will filter
// the results and raise events for each of those with long wait times.
// 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()
->dump()
->map(function ($wait, $queue) use ($waitTime) {
return $waitTime->calculate($queue);
return $waitTime->calculateFor($queue);
})
->merge($results)
->all();
Expand Down
38 changes: 23 additions & 15 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 @@ -41,9 +45,11 @@ public function test_ignores_queue_with_long_wait()
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));
Expand All @@ -56,18 +62,20 @@ public function test_ignores_queue_with_long_wait()

public function test_monitors_specific_queue_with_long_wait()
{
config(['horizon.waits' => ['redis:shared-1' => 10]]);
config(['horizon.waits' => ['redis:default' => 0, 'redis:shared-1' => 10]]);

Event::fake();

$calc = Mockery::mock(WaitTimeCalculator::class);
$calc->expects('calculate')->andReturn([
'redis:default' => 60,
'redis:shared-1,shared-2' => 80,
]);
$calc->expects('calculate')->with('shared-1')->andReturn([
'redis:shared-1' => 15,
]);
$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));
Expand Down

0 comments on commit 4a2c1e4

Please sign in to comment.