/** * Runtime of Master process * @return void */ public function run() { Daemon::$process = $this; $this->prepareSystemEnv(); class_exists('Timer'); // ensure loading this class gc_enable(); $this->eventBase = event_base_new(); $this->registerEventSignals(); FS::initEvent(); $this->fileWatcher = new FileWatcher(); $this->workers = new ThreadCollection(); $this->collections['workers'] = $this->workers; Daemon::$appResolver = (require Daemon::$config->path->value); $this->IPCManager = Daemon::$appResolver->getInstanceByAppName('IPCManager'); Daemon::$appResolver->preload(true); $this->callbacks = new SplStack(); $this->spawnWorkers(min(Daemon::$config->startworkers->value, Daemon::$config->maxworkers->value)); Timer::add(function ($event) use(&$cbs) { $self = Daemon::$process; static $c = 0; ++$c; if ($c > 0xfffff) { $c = 1; } if ($c % 10 == 0) { $self->workers->removeTerminated(true); gc_collect_cycles(); } else { $self->workers->removeTerminated(); } if (isset(Daemon::$config->mpm->value) && is_callable(Daemon::$config->mpm->value)) { call_user_func(Daemon::$config->mpm->value); } else { // default MPM $state = Daemon::getStateOfWorkers($self); if ($state) { $n = max(min(Daemon::$config->minspareworkers->value - $state['idle'], Daemon::$config->maxworkers->value - $state['alive']), Daemon::$config->minworkers->value - $state['alive']); if ($n > 0) { Daemon::log('Spawning ' . $n . ' worker(s).'); $self->spawnWorkers($n); event_base_loopbreak($self->eventBase); } $n = min($state['idle'] - Daemon::$config->maxspareworkers->value, $state['alive'] - Daemon::$config->minworkers->value); if ($n > 0) { Daemon::log('Stopping ' . $n . ' worker(s).'); $self->stopWorkers($n); } } } $event->timeout(); }, 1000000.0 * Daemon::$config->mpmdelay->value, 'MPM'); while (!$this->breakMainLoop) { while (!$this->callbacks->isEmpty()) { call_user_func($this->callbacks->shift(), $this); } event_base_loop($this->eventBase); } }
function halt() { event_base_loopbreak($this->handle); return $this; }
public function stop() { if (!$this->_started) { return; } self::$_logger->info('Server stops. The event loop ends.'); event_base_loopbreak($this->_eventBase); if ($this->_listenEvent) { event_del($this->_listenEvent); event_free($this->_listenEvent); $this->_listenEvent = NULL; } if ($this->_sock) { socket_close($this->_sock); $this->_sock = NULL; } foreach ($this->_streams as $stream) { $stream->forceClose(); } // TODO: need to clear the streams too foreach ($this->_signalEvents as $signo => $event) { event_del($event); event_free($event); unset($this->_signalEvents[$signo]); } $this->_started = FALSE; }
private function become_intern($request_message) { $intern = new Prefork_Intern($this); $pid = $this->fork_process(); if ($pid === 0) { // Interns only past this point event_base_loopbreak($this->event_base); event_base_reinit($this->event_base); foreach ($this->events as $i => $event) { event_del($event); event_free($event); unset($this->events[$i]); } event_base_free($this->event_base); $intern->start_request($request_message); $this->intern = $intern; return true; } $this->intern_pid = $pid; if (!$this->single_interns) { $this->make_offer(); } return false; }
/** * 退出时间循环 * @return [void] [none] */ public function loopbreak() { event_base_loopbreak($this->_eventBase); }
/** * Break loop * * @return void */ public function breakLoop() { $this->isTerminating = true; event_base_loopbreak($this->handle); }
/** * Signal callback * @param int $Signal */ public function evcb_Signal($hEvent, $Events, $Signal) { switch ($Signal) { case SIGALRM: $this->SendCommand('GC'); break; case SIGUSR2: $this->SendCommand('Report'); break; case SIGTERM: case SIGTRAP: $this->SendCommand('Exit'); foreach ($this->ChildProcess as $pid) { pcntl_waitpid($status, $pid); } event_base_loopbreak($this->BaseEvent); sem_remove($this->SEM); shmop_delete($this->SHM); shmop_close($this->SHM); } }
/** * Immediately stops the loop, and doesn't handle any further pending * events. * * Returns true on success or false on error. * * @return bool */ public function breakLoop() { return event_base_loopbreak($this->resource); }
/** * Abort the active event loop immediately. The behaviour is similar to break statement. * * @see event_base_loopbreak * * @throws EventException * * @return EventBaseInterface */ public function loopBreak() { if (false === event_base_loopbreak($this->resource)) { throw $this->exception('Could not break loop (event_base_loopbreak)'); } return $this; }