Skip to content

Commit

Permalink
Output console error when terminating due to memory usage (#1391)
Browse files Browse the repository at this point in the history
* Show error in console when php artisan:horizon terminates when using more than configured memory

* Update HorizonCommand.php

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
SanderMuller and taylorotwell authored Feb 20, 2024
1 parent 3bf97ef commit 7475de7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Listeners/MonitorMasterSupervisorMemory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/Feature/MonitorMasterSupervisorMemoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit 7475de7

Please sign in to comment.