From 7475de7eb5b465c2da84218002fe1a62b8175da0 Mon Sep 17 00:00:00 2001 From: Sander Muller Date: Tue, 20 Feb 2024 16:14:10 +0100 Subject: [PATCH] Output console error when terminating due to memory usage (#1391) * Show error in console when php artisan:horizon terminates when using more than configured memory * Update HorizonCommand.php --------- Co-authored-by: Taylor Otwell --- src/Listeners/MonitorMasterSupervisorMemory.php | 6 +++++- tests/Feature/MonitorMasterSupervisorMemoryTest.php | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Listeners/MonitorMasterSupervisorMemory.php b/src/Listeners/MonitorMasterSupervisorMemory.php index ebf1582f..569d5505 100644 --- a/src/Listeners/MonitorMasterSupervisorMemory.php +++ b/src/Listeners/MonitorMasterSupervisorMemory.php @@ -17,9 +17,13 @@ public function handle(MasterSupervisorLooped $event) { $master = $event->master; - if ($master->memoryUsage() > config('horizon.memory_limit', 64)) { + $memoryLimit = config('horizon.memory_limit', 64); + + if ($master->memoryUsage() > $memoryLimit) { event(new MasterSupervisorOutOfMemory($master)); + $master->output('error', 'Memory limit exceeded: Using '.ceil($master->memoryUsage()).'/'.$memoryLimit.'MB. Consider increasing horizon.memory_limit.'); + $master->terminate(12); } } diff --git a/tests/Feature/MonitorMasterSupervisorMemoryTest.php b/tests/Feature/MonitorMasterSupervisorMemoryTest.php index 362f4a4c..587f4792 100644 --- a/tests/Feature/MonitorMasterSupervisorMemoryTest.php +++ b/tests/Feature/MonitorMasterSupervisorMemoryTest.php @@ -17,6 +17,7 @@ public function test_supervisor_is_terminated_when_using_too_much_memory() $master = Mockery::mock(MasterSupervisor::class); $master->shouldReceive('memoryUsage')->andReturn(192); + $master->shouldReceive('output')->once()->with('error', 'Memory limit exceeded: Using 192/64MB. Consider increasing horizon.memory_limit.'); $master->shouldReceive('terminate')->once()->with(12); $monitor->handle(new MasterSupervisorLooped($master));