public function run()
 {
     proc_nice(Daemon::$settings['masterpriority']);
     gc_enable();
     register_shutdown_function(array($this, 'onShutdown'));
     $this->collections = array('workers' => new threadCollection());
     Thread::setproctitle(Daemon::$runName . ': master process' . (Daemon::$settings['pidfile'] !== Daemon::$settings['defaultpidfile'] ? ' (' . Daemon::$settings['pidfile'] . ')' : ''));
     Daemon::$appResolver = (require Daemon::$settings['path']);
     Daemon::$appResolver->preloadPrivileged();
     $this->spawnWorkers(min(Daemon::$settings['startworkers'], Daemon::$settings['maxworkers']));
     $mpmLast = time();
     $autoReloadLast = time();
     while (TRUE) {
         pcntl_signal_dispatch();
         $this->sigwait(1, 0);
         clearstatcache();
         if (Daemon::$logpointerpath !== Daemon::parseStoragepath(Daemon::$settings['logstorage'])) {
             $this->sigusr1();
         }
         $c = 1;
         if (time() > $mpmLast + Daemon::$parsedSettings['mpmdelay']) {
             $mpmLast = time();
             ++$c;
             if ($c > 0xfffff) {
                 $c = 0;
             }
             if ($c % 10 == 0) {
                 $this->collections['workers']->removeTerminated(TRUE);
                 gc_collect_cycles();
             } else {
                 $this->collections['workers']->removeTerminated();
             }
             if (isset(Daemon::$settings['mpm']) && is_callable($c = Daemon::$settings['mpm'])) {
                 call_user_func($c);
             } else {
                 $state = Daemon::getStateOfWorkers($this);
                 if ($state) {
                     $n = max(min(Daemon::$settings['minspareworkers'] - $state['idle'], Daemon::$settings['maxworkers'] - $state['alive']), Daemon::$settings['minworkers'] - $state['alive']);
                     if ($n > 0) {
                         Daemon::log('Spawning ' . $n . ' worker(s).');
                         $this->spawnWorkers($n);
                     }
                     $n = min($state['idle'] - Daemon::$settings['maxspareworkers'], $state['alive'] - Daemon::$settings['minworkers']);
                     if ($n > 0) {
                         Daemon::log('Stopping ' . $n . ' worker(s).');
                         $this->stopWorkers($n);
                     }
                 }
             }
         }
     }
 }
Ejemplo n.º 2
0
 public static function openLogs()
 {
     if (Daemon::$settings['logging']) {
         if (Daemon::$logpointer) {
             fclose(Daemon::$logpointer);
             Daemon::$logpointer = FALSE;
         }
         Daemon::$logpointer = fopen(Daemon::$logpointerpath = Daemon::parseStoragepath(Daemon::$settings['logstorage']), 'a+');
         if (isset(Daemon::$settings['group'])) {
             chgrp(Daemon::$logpointerpath, Daemon::$settings['group']);
         }
         if (isset(Daemon::$settings['user'])) {
             chown(Daemon::$logpointerpath, Daemon::$settings['user']);
         }
     }
 }