Example #1
0
 /**
  * {@inheritdoc}
  */
 protected function dispatch(bool $blocking)
 {
     $flags = EventBase::LOOP_ONCE;
     if (!$blocking) {
         $flags |= EventBase::LOOP_NONBLOCK;
     }
     $this->base->loop($flags);
     // Dispatch I/O, timer, and signal callbacks.
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 protected function dispatch(int $level)
 {
     if ($this->deferred || $this->enable || $this->running <= $level) {
         $this->loop->loop(\EventBase::LOOP_NONBLOCK);
     } else {
         $this->loop->loop(\EventBase::LOOP_ONCE);
     }
 }
Example #3
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.
 }
Example #4
0
In another terminal window find out the pid and send SIGTERM, e.g.:

$ ps aux | grep examp
ruslan    3976  0.2  0.0 139896 11256 pts/1    S+   10:25   0:00 php examples/signal.php
ruslan    3978  0.0  0.0   9572   864 pts/2    S+   10:26   0:00 grep --color=auto examp
$ kill -TERM 3976

At the first terminal window you should catch the following:

Caught signal 15
*/
class MyEventSignal
{
    private $base, $ev;
    public function __construct($base)
    {
        $this->base = $base;
        $this->ev = Event::signal($base, SIGTERM, array($this, 'eventSighandler'), 'libo');
        $this->ev->add();
    }
    public function eventSighandler($no, $c)
    {
        echo "Caught signal {$no}\n";
        //print_r($c);
        $this->base->exit();
    }
}
$base = new EventBase();
$c = new MyEventSignal($base);
$base->loop();