Skip to content

Commit

Permalink
dont die option for supervisor
Browse files Browse the repository at this point in the history
  • Loading branch information
halaei committed Nov 10, 2020
1 parent dcd682b commit d7d547e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

#v0.7.0
- Supervisor can return instead of exit using dontDie option.
- Bugfix in handling Throwable errors.

#v0.6.1
- New feature: Process.

Expand Down
28 changes: 15 additions & 13 deletions src/Supervisor/Supervisor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Illuminate\Contracts\Events\Dispatcher as Events;
use Illuminate\Contracts\Foundation\Application;
use Closure;
use Symfony\Component\Debug\Exception\FatalThrowableError;

class Supervisor
{
Expand Down Expand Up @@ -63,6 +62,7 @@ public function __construct(Application $laravel, Cache $cache, Bus $bus, Events
*
* @param Closure|string|object $command
* @param SupervisorOptions|null $options
* @return int
*/
public function supervise($command, SupervisorOptions $options = null)
{
Expand Down Expand Up @@ -92,6 +92,9 @@ public function supervise($command, SupervisorOptions $options = null)
// this process should restart based on other indications. If so, we'll stop
// this process and let whatever is "monitoring" it restart the process.
$this->stopIfNecessary($options, $state);
if ($state->exitStatus !== false) {
return $state->exitStatus;
}
}
}

Expand All @@ -107,18 +110,11 @@ protected function run(Closure $closure, SupervisorOptions $options, SupervisorS
try {
$closure();
$this->events->dispatch(new RunSucceed($options, $state));
} catch (\Exception $e) {
if ($options->stopOnError) {
$state->shouldQuit = true;
}
$this->exceptions->report($e);
$this->events->dispatch(new RunFailed($options, $state, $e));
sleep(1);
} catch (\Throwable $e) {
if ($options->stopOnError) {
$state->shouldQuit = true;
}
$this->exceptions->report(new FatalThrowableError($e));
$this->exceptions->report($e);
$this->events->dispatch(new RunFailed($options, $state, $e));
sleep(1);
}
Expand Down Expand Up @@ -261,10 +257,10 @@ protected function memoryExceeded($memoryLimit)
protected function stopIfNecessary(SupervisorOptions $options, SupervisorState $state)
{
if ($this->memoryExceeded($options->memory)) {
$this->stop(12);
$this->stop(12, ! $options->dontDie, $state);
} elseif ($state->shouldQuit || $this->shouldRestart($state->lastRestart) ||
$this->events->until(new LoopCompleting($options, $state)) === false) {
$this->stop();
$this->stop(0, ! $options->dontDie, $state);
}
}

Expand All @@ -280,13 +276,19 @@ protected function pause()
* Stop listening and bail out of the script.
*
* @param int $status
* @param bool $exit
* @param SupervisorState $state
* @return void
*/
protected function stop($status = 0)
protected function stop($status, $exit, SupervisorState $state)
{
$state->exitStatus = $status;

$this->events->dispatch(new SupervisorStopping($status));

exit($status);
if ($exit) {
exit($status);
}
}

/**
Expand Down
11 changes: 10 additions & 1 deletion src/Supervisor/SupervisorOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,26 @@ class SupervisorOptions
*/
public $stopOnError;

/**
* Prefer to return instead of calling exit() if possible.
*
* @var bool
*/
public $dontDie;

/**
* @param int $timeout
* @param int|float $memory
* @param bool $force
* @param bool $stopOnError
* @param bool $dontDie
*/
public function __construct($timeout = 60, $memory = 128, $force = false, $stopOnError = false)
public function __construct($timeout = 60, $memory = 128, $force = false, $stopOnError = false, $dontDie = false)
{
$this->timeout = $timeout;
$this->memory = $memory;
$this->force = $force;
$this->stopOnError = $stopOnError;
$this->dontDie = $dontDie;
}
}
5 changes: 5 additions & 0 deletions src/Supervisor/SupervisorState.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ class SupervisorState
* @var int|null
*/
public $lastRestart;

/**
* @var false|int
*/
public $exitStatus = false;
}

0 comments on commit d7d547e

Please sign in to comment.