Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add signal option to horizon:purge (e.g. SIGKILL) #1226

Merged
merged 2 commits into from
Dec 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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