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

[5.0] Check if parent is still running #881

Merged
merged 2 commits into from
Sep 3, 2020
Merged
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions src/Console/SupervisorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class SupervisorCommand extends Command
{--tries=0 : Number of times to attempt a job before logging it failed}
{--balance-cooldown=3 : The number of seconds to wait in between auto-scaling attempts}
{--balance-max-shift=1 : The maximum number of processes to increase or decrease per one scaling}
{--workers-name=default : The name that should be assigned to the workers}';
{--workers-name=default : The name that should be assigned to the workers}
{--parent-id=0 : The parent process ID}';

/**
* The console command description.
Expand Down Expand Up @@ -127,7 +128,8 @@ protected function supervisorOptions()
$this->option('force'),
$this->option('nice'),
$this->option('balance-cooldown'),
$this->option('balance-max-shift')
$this->option('balance-max-shift'),
$this->option('parent-id')
);
}

Expand Down
2 changes: 2 additions & 0 deletions src/ProvisioningPlan.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ protected function convert($supervisor, $options)
return [Str::camel($key) => $value];
})->all();

$options['parentId'] = getmypid();

return SupervisorOptions::fromArray(
Arr::add($options, 'name', $this->master.":{$supervisor}")
);
Expand Down
14 changes: 14 additions & 0 deletions src/Supervisor.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ public function ensureNoDuplicateSupervisors()
public function loop()
{
try {
$this->ensureParentIsRunning();

$this->processPendingSignals();

$this->processPendingCommands();
Expand All @@ -305,6 +307,18 @@ public function loop()
}
}

/**
* Ensure the parent process is still running.
*
* @return void
*/
protected function ensureParentIsRunning()
{
if ($this->options->parentId > 1 && posix_getppid() <= 1) {
$this->terminate();
}
}

/**
* Handle any pending commands for the supervisor.
*
Expand Down
12 changes: 11 additions & 1 deletion src/SupervisorOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ class SupervisorOptions
*/
public $minProcesses = 1;

/**
* The parent process identifier.
*
* @var int
*/
public $parentId = 0;

/**
* The process priority.
*
Expand Down Expand Up @@ -158,6 +165,7 @@ class SupervisorOptions
* @param int $nice
* @param int $balanceCooldown
* @param int $balanceMaxShift
* @param int $parentId
*/
public function __construct($name,
$connection,
Expand All @@ -176,7 +184,8 @@ public function __construct($name,
$force = false,
$nice = 0,
$balanceCooldown = 3,
$balanceMaxShift = 1)
$balanceMaxShift = 1,
$parentId = 0)
Copy link
Member

@driesvints driesvints Mar 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@halaei why didn't you implement this argument? Atm it'll always be zero.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @driesvints It seems I made a mistake. Setting parentId in constructor also doesn't work because it is still zero in ensureParentIsRunning.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@halaei what do you mean? If we fix this by passing the $parentId from the constructor to the property it should work, no? Why do you think it would still be zero?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@driesvints I just dump() the value! It was 0 for sure. I sent a draft PR

{
$this->name = $name;
$this->connection = $connection;
Expand All @@ -196,6 +205,7 @@ public function __construct($name,
$this->nice = $nice;
$this->balanceCooldown = $balanceCooldown;
$this->balanceMaxShift = $balanceMaxShift;
$this->parentId = 0;
}

/**
Expand Down