Skip to content

Commit

Permalink
Merge pull request #55 from neighborhoods/leverage-aggressive-error-h…
Browse files Browse the repository at this point in the history
…andler-for-more-better-forking

Leverage ErrorException to add fork failure context.
  • Loading branch information
rhift authored Feb 15, 2019
2 parents 7228b58 + 9e6938a commit ae4b303
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 7 deletions.
12 changes: 8 additions & 4 deletions src/Process/Forked.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ abstract class Forked extends ProcessAbstract implements ProcessInterface
public function start(): ProcessInterface
{
$this->_create(self::PROP_HAS_FORKED, true);
$processId = $this->_getProcessStrategy()->fork();
if ($processId === self::FORK_FAILURE_CODE) {
throw (new Exception())->setCode(Exception::CODE_FORK_FAILED);
} elseif ($processId > 0) {
try {
$processId = $this->_getProcessStrategy()->fork();
} /** @noinspection PhpRedundantCatchClauseInspection */
catch (\ErrorException $errorException) {
throw (new Exception())->setCode(Exception::CODE_FORK_FAILED)->setPrevious($errorException);
}

if ($processId > 0) {
// This is executed in the parent process.
$this->_setProcessId($processId);
} else {
Expand Down
2 changes: 0 additions & 2 deletions src/Process/Pool/Strategy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ services:
shared: false
class: Neighborhoods\Kojo\Process\Pool\Strategy\Server
calls:
- [setAlarmProcessTypeCode, ['root']]
- [setFillProcessTypeCode, ['root']]
- [setLogger, ['@process.pool.logger']]
- [setMaximumLoadAverage, ['%process.pool.strategy-server.maximum_load_average%']]
process.pool.strategy-server:
Expand Down
1 change: 1 addition & 0 deletions src/Process/Pool/Strategy/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function initializePool(): StrategyInterface
$this->_getProcessPool()->addChildProcess($process);
} catch (Exception $forkedException) {
$this->_getLogger()->critical($forkedException->getMessage(), [(string)$forkedException]);
$this->_getProcessPool()->getProcess()->exit();
}
}
if ($this->_hasFillProcessTypeCode() && $this->_getProcessPool()->canEnvironmentSustainAdditionProcesses()) {
Expand Down
2 changes: 1 addition & 1 deletion src/Process/Strategy/ProcessControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ public function fork(): int
{
return pcntl_fork();
}
}
}
1 change: 1 addition & 0 deletions src/ProcessAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public function exit(): void
{
$this->unregisterShutdownMethod();
$this->_getProcessPool()->terminateChildProcesses();
$this->_getLogger()->debug("Exiting.");
exit($this->_exitCode);
}

Expand Down
4 changes: 4 additions & 0 deletions src/Runtime/ExceptionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ public function setCode(string $code): ExceptionInterface;
public function addMessage(string $additionalMessage): ExceptionInterface;

public function jsonSerialize();

public function setPrevious(\Throwable $previous): ExceptionInterface;

public function addPossibleMessage(string $code, string $message): ExceptionInterface;
}

0 comments on commit ae4b303

Please sign in to comment.