Skip to content

Commit

Permalink
Merge pull request #196 from Provoker/graceful-stop
Browse files Browse the repository at this point in the history
Graceful stop
  • Loading branch information
kakserpom committed Jan 25, 2015
2 parents 3892bf3 + f9b9587 commit d63074e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 3 deletions.
9 changes: 6 additions & 3 deletions PHPDaemon/Core/Bootstrap.php
Expand Up @@ -30,7 +30,7 @@ class Bootstrap {
* @var array
*/
protected static $commands = [
'start', 'stop', 'hardstop', 'update', 'reload', 'restart', 'hardrestart', 'fullstatus', 'status', 'configtest', 'log', 'runworker'
'start', 'stop', 'hardstop', 'gracefulstop', 'update', 'reload', 'restart', 'hardrestart', 'fullstatus', 'status', 'configtest', 'log', 'runworker'
];

/**
Expand Down Expand Up @@ -427,6 +427,9 @@ public static function init($configFile = null) {
elseif ($runmode == 'stop') {
Bootstrap::stop();
}
elseif ($runmode == 'gracefulstop') {
Bootstrap::stop(4);
}
elseif ($runmode == 'hardstop') {
echo '[HARDSTOP] Sending SIGINT to ' . Bootstrap::$pid . '... ';

Expand Down Expand Up @@ -468,7 +471,7 @@ public static function init($configFile = null) {
* @return void
*/
protected static function printUsage() {
echo 'usage: ' . Daemon::$runName . " (start|(hard)stop|update|reload|(hard)restart|fullstatus|status|configtest|log|runworker|help) ...\n";
echo 'usage: ' . Daemon::$runName . " (start|(hard|graceful)stop|update|reload|(hard)restart|fullstatus|status|configtest|log|runworker|help) ...\n";
}

/**
Expand Down Expand Up @@ -537,7 +540,7 @@ public static function runworker() {
* @return void
*/
public static function stop($mode = 1) {
$ok = Bootstrap::$pid && posix_kill(Bootstrap::$pid, $mode === 3 ? SIGINT : SIGTERM);
$ok = Bootstrap::$pid && posix_kill(Bootstrap::$pid, $mode === 3 ? SIGINT : (($mode === 4) ? SIGTSTP : SIGTERM));

if (!$ok) {
echo '[WARN]. It seems that phpDaemon is not running' . (Bootstrap::$pid ? ' (PID ' . Bootstrap::$pid . ')' : '') . ".\n";
Expand Down
1 change: 1 addition & 0 deletions PHPDaemon/Thread/Generic.php
Expand Up @@ -75,6 +75,7 @@ abstract class Generic {
SIGWINCH => 'SIGWINCH',
SIGUSR1 => 'SIGUSR1',
SIGUSR2 => 'SIGUSR2',
SIGTSTP => 'SIGTSTP',
];

/**
Expand Down
12 changes: 12 additions & 0 deletions PHPDaemon/Thread/IPC.php
Expand Up @@ -281,6 +281,18 @@ public function sigquit() {
parent::sigquit();
}

/**
* Handler of the SIGTSTP (graceful stop) signal in worker process.
* @return void
*/
protected function sigtstp() {
if (Daemon::$config->logsignals->value) {
$this->log('Caught SIGTSTP (graceful stop all workers).');
}

$this->shutdown();
}

/**
* Handler of the SIGHUP (reload config) signal in worker process.
* @return void
Expand Down
13 changes: 13 additions & 0 deletions PHPDaemon/Thread/Master.php
Expand Up @@ -397,6 +397,19 @@ protected function sigquit() {
$this->shutdown(SIGQUIT);
}

/**
* Handler for the SIGTSTP (graceful stop all workers) signal in master process
* @return void
*/
protected function sigtstp() {
if (Daemon::$config->logsignals->value) {
$this->log('Caught SIGTSTP (graceful stop all workers).');
}

$this->signalToChildren(SIGTSTP);
$this->shutdown(SIGTSTP);
}

/**
* Handler for the SIGHUP (reload config) signal in master process
* @return void
Expand Down
23 changes: 23 additions & 0 deletions PHPDaemon/Thread/Worker.php
Expand Up @@ -533,6 +533,17 @@ public function gracefulRestart() {
$this->setState($this->state);
}

/**
* Graceful stop
* @return void
*/
public function gracefulStop() {
$this->breakMainLoop = true;
$this->graceful = true;
$this->reloadTime = microtime(true) + $this->reloadDelay;
$this->setState($this->state);
}

/**
* Asks the running applications the whether we can go to shutdown current (old) worker.
* @return boolean - Ready?
Expand Down Expand Up @@ -727,6 +738,18 @@ protected function sigusr2() {
$this->gracefulRestart();
}

/**
* Handler of the SIGTSTP (graceful stop) signal in worker process.
* @return void
*/
protected function sigtstp() {
if (Daemon::$config->logsignals->value) {
$this->log('caught SIGTSTP (graceful stop).');
}

$this->gracefulStop();
}

/**
* Handler of the SIGTTIN signal in worker process.
* @return void
Expand Down

0 comments on commit d63074e

Please sign in to comment.