/** * Open logs. * @return void */ public static function openLogs() { if (Daemon::$config->logging->value) { Daemon::$logpointer = fopen(Daemon::$config->logstorage->value, 'a'); if (isset(Daemon::$config->group->value)) { chgrp(Daemon::$config->logstorage->value, Daemon::$config->group->value); // @TODO: rewrite to async I/O } if (isset(Daemon::$config->user->value)) { chown(Daemon::$config->logstorage->value, Daemon::$config->user->value); // @TODO: rewrite to async I/O } if (Daemon::$process instanceof Daemon_WorkerThread && FS::$supported) { FS::open(Daemon::$config->logstorage->value, 'a!', function ($file) { Daemon::$logpointerAsync = $file; if (!$file) { return; } }); } } else { Daemon::$logpointer = null; Daemon::$logpointerAsync = null; } }
/** * Runtime of Worker process. * @return void */ public function run() { FS::init(); Daemon::$process = $this; if (Daemon::$logpointerAsync) { $oldfd = Daemon::$logpointerAsync->fd; Daemon::$logpointerAsync->fd = null; Daemon::$logpointerAsync = null; } $this->autoReloadLast = time(); $this->reloadDelay = Daemon::$config->mpmdelay->value + 2; $this->setStatus(4); if (Daemon::$config->autogc->value > 0) { gc_enable(); } else { gc_disable(); } $this->prepareSystemEnv(); $this->overrideNativeFuncs(); $this->setStatus(6); $this->eventBase = event_base_new(); $this->registerEventSignals(); FS::init(); // re-init FS::initEvent(); Daemon::openLogs(); $this->fileWatcher = new FileWatcher(); $this->IPCManager = Daemon::$appResolver->getInstanceByAppName('IPCManager'); Daemon::$appResolver->preload(); foreach (Daemon::$appInstances as $app) { foreach ($app as $appInstance) { if (!$appInstance->ready) { $appInstance->ready = TRUE; $appInstance->onReady(); } } } $this->setStatus(1); Timer::add(function ($event) { $self = Daemon::$process; if ($self->checkState() !== TRUE) { $self->closeSockets(); $self->breakMainLoop = TRUE; event_base_loopexit($self->eventBase); return; } $event->timeout(); }, 1000000.0 * 1, 'checkState'); if (Daemon::$config->autoreload->value > 0) { Timer::add(function ($event) { $self = Daemon::$process; static $n = 0; $inc = array_unique(array_map('realpath', get_included_files())); $s = sizeof($inc); if ($s > $n) { $slice = array_slice($inc, $n); Daemon::$process->IPCManager->sendPacket(array('op' => 'addIncludedFiles', 'files' => $slice)); $n = $s; } $event->timeout(); }, 1000000.0 * Daemon::$config->autoreload->value, 'watchIncludedFiles'); } while (!$this->breakMainLoop) { event_base_loop($this->eventBase); } }