Skip to content

Commit

Permalink
Show error in console when php artisan:horizon terminates when using …
Browse files Browse the repository at this point in the history
…more than configured memory
  • Loading branch information
SanderMuller committed Feb 20, 2024
1 parent 3bf97ef commit 8fce4fc
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/Console/HorizonCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ public function handle(MasterSupervisorRepository $masters)
$environment = $this->option('environment') ?? config('horizon.env') ?? config('app.env');

$master = (new MasterSupervisor($environment))->handleOutputUsing(function ($type, $line) {
$this->output->write($line);
if ($type === 'error') {
$this->components->error($line);
} else {
$this->output->write($line);
}
});

ProvisioningPlan::get(MasterSupervisor::name())->deploy($environment);
Expand Down
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 8fce4fc

Please sign in to comment.