diff --git a/src/Listeners/MonitorWaitTimes.php b/src/Listeners/MonitorWaitTimes.php index 5653e2c7..7ac98d53 100644 --- a/src/Listeners/MonitorWaitTimes.php +++ b/src/Listeners/MonitorWaitTimes.php @@ -5,7 +5,6 @@ use Carbon\CarbonImmutable; use Laravel\Horizon\Contracts\MetricsRepository; use Laravel\Horizon\Events\LongWaitDetected; -use Laravel\Horizon\Events\SupervisorLooped; use Laravel\Horizon\WaitTimeCalculator; class MonitorWaitTimes @@ -41,7 +40,7 @@ public function __construct(MetricsRepository $metrics) * @param \Laravel\Horizon\Events\SupervisorLooped $event * @return void */ - public function handle(SupervisorLooped $event) + public function handle() { if (! $this->dueToMonitor()) { return; @@ -53,7 +52,8 @@ public function handle(SupervisorLooped $event) $results = app(WaitTimeCalculator::class)->calculate(); $long = collect($results)->filter(function ($wait, $queue) { - return $wait > (config("horizon.waits.{$queue}") ?? 60); + return config("horizon.waits.{$queue}") !== 0 + && $wait > (config("horizon.waits.{$queue}") ?? 60); }); // Once we have determined which queues have long wait times we will raise the diff --git a/tests/Feature/MonitorWaitTimesTest.php b/tests/Feature/MonitorWaitTimesTest.php index e35c7541..1d561bce 100644 --- a/tests/Feature/MonitorWaitTimesTest.php +++ b/tests/Feature/MonitorWaitTimesTest.php @@ -3,13 +3,10 @@ namespace Laravel\Horizon\Tests\Feature; use Carbon\CarbonImmutable; -use Illuminate\Contracts\Redis\Factory as RedisFactory; use Illuminate\Support\Facades\Event; use Laravel\Horizon\Contracts\MetricsRepository; use Laravel\Horizon\Events\LongWaitDetected; -use Laravel\Horizon\Events\SupervisorLooped; use Laravel\Horizon\Listeners\MonitorWaitTimes; -use Laravel\Horizon\Supervisor; use Laravel\Horizon\Tests\IntegrationTest; use Laravel\Horizon\WaitTimeCalculator; use Mockery; @@ -20,10 +17,6 @@ public function test_queues_with_long_waits_are_found() { Event::fake(); - $redis = Mockery::mock(RedisFactory::class); - $redis->shouldReceive('setnx')->with('monitor:time-to-clear', 1)->andReturn(1); - $redis->shouldReceive('expire')->with('monitor:time-to-clear', 60); - $calc = Mockery::mock(WaitTimeCalculator::class); $calc->shouldReceive('calculate')->andReturn([ 'redis:test-queue' => 10, @@ -31,13 +24,33 @@ public function test_queues_with_long_waits_are_found() ]); $this->app->instance(WaitTimeCalculator::class, $calc); - $listener = new MonitorWaitTimes(resolve(MetricsRepository::class), $redis); - $listener->lastMonitoredAt = CarbonImmutable::now()->subDays(1); + $listener = new MonitorWaitTimes(resolve(MetricsRepository::class)); + $listener->lastMonitoredAt = CarbonImmutable::now()->subDay(); - $listener->handle(new SupervisorLooped(Mockery::mock(Supervisor::class))); + $listener->handle(); Event::assertDispatched(LongWaitDetected::class, function ($event) { return $event->connection == 'redis' && $event->queue == 'test-queue-2'; }); } + + public function test_queue_ignores_long_waits() + { + config(['horizon.waits' => ['redis:ignore-queue' => 0]]); + + Event::fake(); + + $calc = Mockery::mock(WaitTimeCalculator::class); + $calc->expects('calculate')->andReturn([ + 'redis:ignore-queue' => 10, + ]); + $this->app->instance(WaitTimeCalculator::class, $calc); + + $listener = new MonitorWaitTimes(resolve(MetricsRepository::class)); + $listener->lastMonitoredAt = CarbonImmutable::now()->subDays(1); + + $listener->handle(); + + Event::assertNotDispatched(LongWaitDetected::class); + } }