add() public static method

Adds timer
public static add ( callable $cb, integer $timeout = null, integer | string $id = null, integer $priority = null ) : integer | string
$cb callable Callback
$timeout integer Timeout
$id integer | string Timer ID
$priority integer Priority
return integer | string Timer ID
Ejemplo n.º 1
0
 protected function runAsync($method, $timeout = 3000000.0)
 {
     Timer::add(function () use($method) {
         self::assertSame(0, 1, 'Some callbacks didnt finished in ' . $method);
     }, $timeout);
     EventLoop::$instance->run();
     //EventLoop::$instance->free();
     //EventLoop::$instance = null;
 }
 /**
  * @see \FutoIn\RI\Details\AsyncToolImpl::callLater
  */
 public function callLater($cb, $delay_ms = 0)
 {
     $t = new \StdClass();
     $t->id = \PHPDaemon\Core\Timer::add(function () use($cb, $t) {
         $this->cancelCall($t);
         $cb();
     }, $delay_ms);
     return $t;
 }
Ejemplo n.º 3
0
 /**
  * Constructor
  */
 public function __construct()
 {
     if (Daemon::loadModuleIfAbsent('inotify')) {
         $this->inotify = inotify_init();
         stream_set_blocking($this->inotify, 0);
     }
     Timer::add(function ($event) {
         Daemon::$process->fileWatcher->watch();
         if (sizeof(Daemon::$process->fileWatcher->files) > 0) {
             $event->timeout();
         }
     }, 1000000.0 * 1, 'fileWatcher');
 }
Ejemplo n.º 4
0
 /**
  * @param $p
  */
 protected function onPacket($p)
 {
     if ($p['op'] === 'spawnInstance') {
         $fullname = $p['appfullname'];
         $fullname = str_replace('-', ':', $fullname);
         if (mb_orig_strpos($fullname, ':') === false) {
             $fullname .= ':';
         }
         list($app, $name) = explode(':', $fullname, 2);
         Daemon::$appResolver->getInstance($app, $name, true, true);
     } elseif ($p['op'] === 'importFile') {
         if (!Daemon::$config->autoreimport->value) {
             Daemon::$process->gracefulRestart();
             return;
         }
         $path = $p['path'];
         Timer::add(function ($event) use($path) {
             if (Daemon::supported(Daemon::SUPPORT_RUNKIT_IMPORT)) {
                 //Daemon::log('--start runkit_import('.$path.')');
                 runkit_import($path, RUNKIT_IMPORT_FUNCTIONS | RUNKIT_IMPORT_CLASSES | RUNKIT_IMPORT_OVERRIDE);
                 //Daemon::log('--end runkit_import('.$path.')');
             } else {
                 $this->appInstance->log('Cannot import \'' . $path . '\': runkit_import is not callable.');
             }
             $event->finish();
         }, 5);
     } elseif ($p['op'] === 'call') {
         if (mb_orig_strpos($p['appfullname'], ':') === false) {
             $p['appfullname'] .= ':';
         }
         list($app, $name) = explode(':', $p['appfullname'], 2);
         if ($app = Daemon::$appResolver->getInstance($app, $name)) {
             $app->RPCall($p['method'], $p['args']);
         }
     }
 }
Ejemplo n.º 5
0
 /**
  * Shutdown this worker
  * @param boolean Hard? If hard, we shouldn't wait for graceful shutdown of the running applications.
  * @return boolean|null Ready?
  */
 protected function shutdown($hard = false)
 {
     $error = error_get_last();
     if ($error) {
         if ($error['type'] === E_ERROR) {
             Daemon::log('W#' . $this->pid . ' crashed by error \'' . $error['message'] . '\' at ' . $error['file'] . ':' . $error['line']);
         }
     }
     if (Daemon::$config->logevents->value) {
         $this->log('event shutdown(' . ($hard ? 'HARD' : '') . ') invoked.');
     }
     if (Daemon::$config->throwexceptiononshutdown->value) {
         throw new \Exception('event shutdown');
     }
     @ob_flush();
     if ($this->terminated === true) {
         if ($hard) {
             exit(0);
         }
         return;
     }
     $this->terminated = true;
     if ($hard) {
         $this->setState(Daemon::WSTATE_SHUTDOWN);
         exit(0);
     }
     $this->reloadReady = $this->appInstancesReloadReady();
     if ($this->reload && $this->graceful) {
         $this->reloadReady = $this->reloadReady && microtime(TRUE) > $this->reloadTime;
     }
     if (Daemon::$config->logevents->value) {
         $this->log('reloadReady = ' . Debug::dump($this->reloadReady));
     }
     Timer::remove('breakMainLoopCheck');
     Timer::add(function ($event) {
         $self = Daemon::$process;
         $self->reloadReady = $self->appInstancesReloadReady();
         if ($self->reload === TRUE) {
             $self->reloadReady = $self->reloadReady && microtime(TRUE) > $self->reloadTime;
         }
         if (!$self->reloadReady) {
             $event->timeout();
         } else {
             $self->eventBase->exit();
         }
     }, 1000000.0, 'checkReloadReady');
     while (!$this->reloadReady) {
         $this->eventBase->loop();
     }
     FileSystem::waitAllEvents();
     // ensure that all I/O events completed before suicide
     exit(0);
     // R.I.P.
 }
Ejemplo n.º 6
0
 function setTimeout($cb, $timeout = null, $id = null, $priority = null)
 {
     return \PHPDaemon\Core\Timer::add($cb, $timeout, $id, $priority);
 }
Ejemplo n.º 7
0
 public final function baz()
 {
     return function ($jobname, $job) {
         \PHPDaemon\Core\Timer::add(function ($event) use($jobname, $job) {
             // Job done
             $job->setResult($jobname, ['job' => 'baz', 'success' => false, 'line' => __LINE__]);
             $event->finish();
         }, 1000.0 * 300);
     };
 }
Ejemplo n.º 8
0
 /**
  * Runtime of Master process
  * @return void
  */
 protected function run()
 {
     Daemon::$process = $this;
     $this->prepareSystemEnv();
     class_exists('Timer');
     // ensure loading this class
     gc_enable();
     /* This line must be commented according to current libevent binding implementation. May be uncommented in future. */
     //$this->eventBase = new \EventBase;
     if ($this->eventBase) {
         $this->registerEventSignals();
     } else {
         $this->registerSignals();
     }
     $this->workers = new Collection();
     $this->collections['workers'] = $this->workers;
     $this->ipcthreads = new Collection();
     $this->collections['ipcthreads'] = $this->ipcthreads;
     Daemon::$appResolver->preload(true);
     $this->callbacks = new StackCallbacks();
     $this->spawnIPCThread();
     $this->spawnWorkers(min(Daemon::$config->startworkers->value, Daemon::$config->maxworkers->value));
     $this->timerCb = function ($event) use(&$cbs) {
         static $c = 0;
         ++$c;
         if ($c > 0xfffff) {
             $c = 1;
         }
         if ($c % 10 == 0) {
             gc_collect_cycles();
         }
         if (!$this->lastMpmActionTs || microtime(true) - $this->lastMpmActionTs > $this->minMpmActionInterval) {
             $this->callMPM();
         }
         if ($event) {
             $event->timeout();
         }
     };
     if ($this->eventBase) {
         // we are using libevent in Master
         Timer::add($this->timerCb, 1000000.0 * Daemon::$config->mpmdelay->value, 'MPM');
         while (!$this->breakMainLoop) {
             $this->callbacks->executeAll($this);
             if (!$this->eventBase->dispatch()) {
                 break;
             }
         }
     } else {
         // we are NOT using libevent in Master
         $lastTimerCall = microtime(true);
         while (!$this->breakMainLoop) {
             $this->callbacks->executeAll($this);
             if (microtime(true) > $lastTimerCall + Daemon::$config->mpmdelay->value) {
                 call_user_func($this->timerCb, null);
                 $lastTimerCall = microtime(true);
             }
             $this->sigwait();
         }
     }
 }