/** * Instantiate a new CliProject object * * @param null $request the action request notation * @param array $parameters parameters to pass to the action * * @throws SynergyException */ public function __construct($request = null, array $parameters = array()) { register_tick_function(array(&$this, "checkExit")); // Check this is coming from the CLI if (PHP_SAPI !== 'cli') { throw new SynergyException(sprintf('%s must be run from command line project', __CLASS__)); } // Store or build the request $this->parameters = $parameters; if (!is_null($request)) { $this->request = $request; } else { $this->args = ArgumentParser::parseArguments(); $this->request = $this->args->getRequest(); } Logger::debug('CliProject started (pid=' . getmypid() . ')'); if (is_null($this->args->getRequest())) { Logger::emergency('No controller request provided'); exit(1); } $this->registerSignalHandler(); if ($this->args->arg('app')) { $this->setAppDir($this->args->arg('app')); } if ($this->args->arg('conf')) { $this->setConfigFilename($this->args->arg('conf')); } parent::__construct(); }
/** * Fork the process into two * You can create an optional method called preFork() which is called before the fork * Also an optional method called postFork() which is called after the fork on any remaining processes * (ie if you choose to daemonize then the original foreground process will not call the postFork() method) * * @param bool $daemonize kill the parent (original) process and let the new child run * * @return void */ protected function fork($daemonize = false) { if ($this->node !== 0) { return false; } // call any user created preFork method if (method_exists($this, 'preFork')) { $this->preFork(); } // force Propel to close connections so that it will reconnect on next query if (class_exists('\\Propel')) { \Propel::close(); } $pid = pcntl_fork(); switch ($pid) { case -1: Logger::emergency('Unable to fork process'); return; case 0: // this is the child $this->node++; break; default: // we are the original process if ($this->node == 0) { \Cli\line('Child node : pid=%y%s%n', $pid); if ($daemonize) { // exit; } else { $this->pid[] = $pid; } return; } } // promote the daemon process so it doesn't die because the parent has if ($daemonize && posix_setsid() === -1) { Logger::critical('Error creating daemon as session leader'); exit(1); } fclose(STDIN); fclose(STDOUT); fclose(STDERR); $this->stdIn = fopen('/dev/null', 'r'); // set fd/0 $this->stdOut = fopen('/dev/null', 'w'); // set fd/1 $this->stdErr = fopen('php://stdout', 'w'); // a hack to duplicate fd/1 to 2 // Silence any console output from the logger Logger::setSilentConsole(true); // call any user created postFork method if (method_exists($this, 'postFork')) { $this->postFork(); } }
/** * Daemonises the process */ protected function fork_to_bg() { $daemon_pid = pcntl_fork(); switch ($daemon_pid) { case -1: Logger::emergency('Unable to fork daemon process'); exit(1); // fork failed // fork failed case 0: // this child is our daemon $this->process_pid = getmypid(); break; default: // we are the parent - the one from the FG command line // return control to command line by exiting... \Cli\line('Daemon process running : pid=%y' . $daemon_pid . '%n'); exit(0); } // promote the daemon process so it doesn't die because the parent has if (posix_setsid() === -1) { Logger::critical('Error creating daemon as session leader'); exit(1); } fclose(STDIN); fclose(STDOUT); fclose(STDERR); $this->_stdIn = fopen('/dev/null', 'r'); // set fd/0 $this->_stdOut = fopen('/dev/null', 'w'); // set fd/1 $this->_stdErr = fopen('php://stdout', 'w'); // a hack to duplicate fd/1 to 2 // Silence any console output from the logger Logger::setSilentConsole(true); Logger::notice('Daemon process running : pid=' . getmypid()); }