Back to DaemonApplication class

Method shutdown

protected void
shutdown
(mixed $restart = false)
Method to shut down the daemon and optionally restart it.
Parameters
  • bool $restart True to restart the daemon on exit.
Returns
  • void
Since
  • 1.7.0

Method shutdown - Source code

/**
 * Method to shut down the daemon and optionally restart it.
 *
 * @param   boolean  $restart  True to restart the daemon on exit.
 *
 * @return  void
 *
 * @since   1.7.0
 */
protected function shutdown($restart = false)
{
    // If we are already exiting, chill.
    if ($this->exiting) {
        return;
    } else {
        $this->exiting = true;
    }
    // If we aren't already daemonized then just kill the application.
    if (!$this->running && !$this->isActive()) {
        Log::add('Process was not daemonized yet, just halting current process', Log::INFO);
        $this->close();
    }
    // Only read the pid for the parent file.
    if ($this->parentId == $this->processId) {
        // Read the contents of the process id file as an integer.
        $fp = fopen($this->config->get('application_pid_file'), 'r');
        $pid = fread($fp, filesize($this->config->get('application_pid_file')));
        $pid = (int) $pid;
        fclose($fp);
        // Remove the process id file.
        @unlink($this->config->get('application_pid_file'));
        // If we are supposed to restart the daemon we need to execute the same command.
        if ($restart) {
            $this->close(exec(implode(' ', $GLOBALS['argv']) . ' > /dev/null &'));
        } else {
            passthru('kill -9 ' . $pid);
            $this->close();
        }
    }
}