Skip to content

Commit

Permalink
Add signal option to horizon:purge (e.g. SIGKILL) (#1226)
Browse files Browse the repository at this point in the history
* add signal option (e.g. SIGKILL)

* formatting

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
crishoj and taylorotwell authored Dec 14, 2022
1 parent 65cf61c commit 22b6d7c
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/Console/PurgeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class PurgeCommand extends Command
*
* @var string
*/
protected $signature = 'horizon:purge';
protected $signature = 'horizon:purge
{--signal=SIGTERM : The signal to send to the rogue processes}';

/**
* The console command description.
Expand Down Expand Up @@ -69,9 +70,13 @@ public function __construct(
*/
public function handle(MasterSupervisorRepository $masters)
{
$signal = is_numeric($signal = $this->option('signal'))
? $signal
: constant($signal);

foreach ($masters->names() as $master) {
if (Str::startsWith($master, MasterSupervisor::basename())) {
$this->purge($master);
$this->purge($master, $signal);
}
}
}
Expand All @@ -80,11 +85,12 @@ public function handle(MasterSupervisorRepository $masters)
* Purge any orphan processes.
*
* @param string $master
* @param int $signal
* @return void
*/
public function purge($master)
public function purge($master, $signal = SIGTERM)
{
$this->recordOrphans($master);
$this->recordOrphans($master, $signal);

$expired = $this->processes->orphanedFor(
$master, $this->supervisors->longestActiveTimeout()
Expand All @@ -93,7 +99,7 @@ public function purge($master)
collect($expired)->each(function ($processId) use ($master) {
$this->comment("Killing Process: {$processId}");

exec("kill {$processId}");
exec("kill -s {$signal} {$processId}");

$this->processes->forgetOrphans($master, [$processId]);
});
Expand All @@ -103,9 +109,10 @@ public function purge($master)
* Record the orphaned Horizon processes.
*
* @param string $master
* @param int $signal
* @return void
*/
protected function recordOrphans($master)
protected function recordOrphans($master, $signal)
{
$this->processes->orphaned(
$master, $orphans = $this->inspector->orphaned()
Expand All @@ -114,7 +121,7 @@ protected function recordOrphans($master)
foreach ($orphans as $processId) {
$this->info("Observed Orphan: {$processId}");

if (! posix_kill($processId, SIGTERM)) {
if (! posix_kill($processId, $signal)) {
$this->error("Failed to kill process for Orphan: {$processId} (".posix_strerror(posix_get_last_error()).')');
}
}
Expand Down

0 comments on commit 22b6d7c

Please sign in to comment.