Author: Zorin Vasily (maintainer@daemon.io)
Inheritance: extends SplStack, use trait PHPDaemon\Traits\ClassWatchdog, use trait PHPDaemon\Traits\StaticObjectWatchdog
Exemplo n.º 1
0
 /**
  * Sends a frame.
  * @param  string $data Frame's data.
  * @param  integer $type Frame's type. See the constants.
  * @param  callback $cb Optional. Callback called when the frame is received by client.
  * @callback $cb ( )
  * @return boolean Success.
  */
 public function sendFrame($data, $type = 0x0, $cb = null)
 {
     if ($this->finished) {
         return false;
     }
     $this->framesBuffer[] = $data;
     if ($cb !== null) {
         $this->onWrite->push($cb);
     }
     $this->flush();
     return true;
 }
Exemplo n.º 2
0
 /**
  * Lookup a hash
  * @param $hash
  * @param $cb
  * @return $this
  */
 public function lookup($hash, $cb)
 {
     $cb = CallbackWrapper::wrap($cb);
     if (!$this->count()) {
         $this->queue->push(function () use($hash, $cb) {
             $this->lookup($hash, $cb);
         });
         return;
     }
     if (!$this->valid()) {
         $this->rewind();
     }
     $process = $this->current();
     $process->lookup($hash, $cb);
     $this->next();
     return $this;
 }
Exemplo n.º 3
0
 /**
  * Spawn IPC process
  * @param $n - integer - number of workers to spawn
  * @return boolean - success
  */
 protected function spawnIPCThread()
 {
     if (FileSystem::$supported) {
         eio_event_loop();
     }
     $thread = new IPC();
     $this->ipcthreads->push($thread);
     $this->callbacks->push(function ($self) use($thread) {
         $thread->start();
         $pid = $thread->getPid();
         if ($pid < 0) {
             Daemon::$process->log('could not fork IPCThread');
         } elseif ($pid === 0) {
             // worker
             $this->log('Unexcepted execution return to outside of Thread->start()');
             exit;
         }
     });
     if ($this->eventBase) {
         $this->eventBase->stop();
     }
     return true;
 }
Exemplo n.º 4
0
 /**
  * Runtime of Worker process.
  * @return void
  */
 protected function run()
 {
     $this->lambdaCache = new CappedStorageHits();
     $this->lambdaCache->setMaxCacheSize(Daemon::$config->lambdacachemaxsize->value);
     $this->lambdaCache->setCapWindow(Daemon::$config->lambdacachecapwindow->value);
     $this->callbacks = new StackCallbacks();
     if (Daemon::$process instanceof Master) {
         Daemon::$process->unregisterSignals();
     }
     if (Daemon::$process && Daemon::$process->eventBase) {
         Daemon::$process->eventBase->reinit();
         $this->eventBase = Daemon::$process->eventBase;
     } else {
         $this->eventBase = new \EventBase();
     }
     Daemon::$process = $this;
     if (Daemon::$logpointerAsync) {
         $oldfd = Daemon::$logpointerAsync->fd;
         Daemon::$logpointerAsync->fd = null;
         Daemon::$logpointerAsync = null;
     }
     class_exists('Timer');
     $this->autoReloadLast = time();
     $this->reloadDelay = Daemon::$config->mpmdelay->value + 2;
     $this->setState(Daemon::WSTATE_PREINIT);
     if (Daemon::$config->autogc->value > 0) {
         gc_enable();
         gc_collect_cycles();
     } else {
         gc_disable();
     }
     if (Daemon::$runworkerMode) {
         if (!Daemon::$config->verbosetty->value) {
             fclose(STDIN);
             fclose(STDOUT);
             fclose(STDERR);
         }
         Daemon::$appResolver->preload(true);
     }
     $this->prepareSystemEnv();
     $this->overrideNativeFuncs();
     $this->setState(Daemon::WSTATE_INIT);
     $this->dnsBase = new \EventDnsBase($this->eventBase, false);
     // @TODO: test with true
     $this->registerEventSignals();
     FileSystem::init();
     FileSystem::initEvent();
     Daemon::openLogs();
     $this->IPCManager = Daemon::$appResolver->getInstanceByAppName('\\PHPDaemon\\IPCManager\\IPCManager');
     if (!$this->IPCManager) {
         $this->log('cannot instantiate IPCManager');
     }
     Daemon::$appResolver->preload();
     foreach (Daemon::$appInstances as $app) {
         foreach ($app as $appInstance) {
             if (!$appInstance->ready) {
                 $appInstance->ready = true;
                 $appInstance->onReady();
             }
         }
     }
     $this->setState(Daemon::WSTATE_IDLE);
     Timer::add(function ($event) {
         if (!Daemon::$runworkerMode) {
             if ($this->IPCManager) {
                 $this->IPCManager->ensureConnection();
             }
         }
         $this->breakMainLoopCheck();
         if ($this->breakMainLoop) {
             $this->eventBase->exit();
             return;
         }
         if (Daemon::checkAutoGC()) {
             $this->callbacks->push(function ($thread) {
                 gc_collect_cycles();
             });
             $this->eventBase->exit();
         }
         $event->timeout();
     }, 1000000.0 * 1, 'breakMainLoopCheck');
     if (Daemon::$config->autoreload->value > 0) {
         Timer::add(function ($event) {
             static $n = 0;
             $list = get_included_files();
             $s = sizeof($list);
             if ($s > $n) {
                 $slice = array_map('realpath', array_slice($list, $n));
                 Daemon::$process->IPCManager->sendPacket(['op' => 'addIncludedFiles', 'files' => $slice]);
                 $n = $s;
             }
             $event->timeout();
         }, 1000000.0 * Daemon::$config->autoreload->value, 'watchIncludedFiles');
     }
     while (!$this->breakMainLoop) {
         $this->callbacks->executeAll($this);
         if (!$this->eventBase->dispatch()) {
             break;
         }
     }
     $this->shutdown();
 }