/** * Run daemon. * * @return void */ public function run() { //no fail-safe: this daemon is intended to be run as plain script, otherwise it might fail as well and it can not be controlled. $this->logger->info('Controller for daemons started'); foreach ($this->daemons as $daemon) { if (!$daemon->isRunning()) { $this->logger->warning('Daemon "' . $daemon->getIdentifier() . '" is not running, attempting to invoke'); if ($invocation = $daemon->getInvocation()) { $this->logger->info('Starting daemon "' . $daemon->getIdentifier() . '" by command "' . $invocation . '"'); $this->env->runCliCommandDaemonize($daemon->getInvocation(), $daemon->getIdentifier(), $daemon->getBehavior()); } else { $this->logger->critical('Failed to start daemon "' . $daemon->getIdentifier() . '" as it is configured wrong: no invoking command found'); } } } $this->logger->info('Controller for daemons finished normally'); }
/** * Set fail safe handler. * * @return void */ protected function setFailSafeHandler() { if (!isset($this->config['failSafe']['run']) || !$this->config['failSafe']['run']) { return; } $mainCommand = $this->getMainCommand(); $searchCommand = $this->getSearchCommand(); $behavior = Environment::RUN_IGNORE_ON_CONCURRENT; //to ignore current copy register_shutdown_function(function () use($mainCommand, $searchCommand, $behavior) { if ($noRethrow = ob_get_contents()) { ob_end_clean(); exit; } $this->logger->alert('Daemon "' . $this->getIdentifier() . '" was rethrown'); $this->env->runCliCommandDaemonize($mainCommand, $searchCommand, $behavior); }); }