Exemple #1
0
 public static function cancelCurrentTimeoutTimer()
 {
     if (static::$timeoutTimer !== null) {
         static::$timeoutTimer->cancel();
         static::$timeoutTimer = null;
     }
 }
Exemple #2
0
 public function add(TimerInterface $timer)
 {
     $interval = $timer->getInterval();
     $scheduledAt = $interval + $this->getTime();
     $this->timers->attach($timer, $scheduledAt);
     $this->scheduler->insert($timer, -$scheduledAt);
 }
Exemple #3
0
 public function eventListenerRemoved()
 {
     //If it's the last event
     if ($this->countListeners(self::EVENT_CHANGE) === 0) {
         $this->poll_timer->cancel();
     }
 }
Exemple #4
0
 /**
  * Cancels timer
  */
 public function stop()
 {
     if ($this->timer && $this->timer->isActive()) {
         $this->timer->cancel();
     }
     $this->timer = null;
 }
 public function add(TimerInterface $timer)
 {
     $interval = $timer->getInterval();
     if ($interval < self::MIN_RESOLUTION) {
         throw new InvalidArgumentException('Timer events do not support sub-millisecond timeouts.');
     }
     $scheduledAt = $interval + $this->getTime();
     $this->timers->attach($timer, $scheduledAt);
     $this->scheduler->insert($timer, -$scheduledAt);
 }
Exemple #6
0
 /**
  * @param string
  * @return \React\Promise\PromiseInterface
  */
 function query($query)
 {
     return $this->pool->getConnection()->then(function (mysqli $conn) use($query) {
         $status = $conn->query($query, MYSQLI_ASYNC);
         if ($status === false) {
             $this->pool->free($conn);
             throw new Exception($conn->error);
         }
         $id = spl_object_hash($conn);
         $this->conn[$id] = $conn;
         $this->query[$id] = $query;
         $this->deferred[$id] = $deferred = new Deferred();
         if (!isset($this->timer)) {
             $this->timer = $this->loop->addPeriodicTimer(0.01, function () {
                 $links = $errors = $reject = $this->conn;
                 mysqli_poll($links, $errors, $reject, 0);
                 // don't wait, just check
                 $each = array('links' => $links, 'errors' => $errors, 'reject' => $reject);
                 foreach ($each as $type => $connects) {
                     /**
                      * @var $conn mysqli
                      */
                     foreach ($connects as $conn) {
                         $id = spl_object_hash($conn);
                         if (isset($this->conn[$id])) {
                             $deferred = $this->deferred[$id];
                             if ($type == 'links') {
                                 /**
                                  * @var $result mysqli_result
                                  */
                                 $result = $conn->reap_async_query();
                                 if ($result === false) {
                                     $deferred->reject(new Exception($conn->error . '; sql: ' . $this->query[$id]));
                                 } else {
                                     $deferred->resolve(new Result($result, $conn->insert_id, $conn->affected_rows));
                                 }
                             }
                             if ($type == 'errors') {
                                 $deferred->reject(new Exception($conn->error . '; sql: ' . $this->query[$id]));
                             }
                             if ($type == 'reject') {
                                 $deferred->reject(new Exception('Query was rejected; sql: ' . $this->query[$id]));
                             }
                             unset($this->deferred[$id], $this->conn[$id], $this->query[$id]);
                             $this->pool->free($conn);
                         }
                     }
                 }
                 if (empty($this->conn)) {
                     $this->timer->cancel();
                     $this->timer = null;
                 }
             });
         }
         return $deferred->promise();
     });
 }
Exemple #7
0
 /**
  */
 private function onTick()
 {
     try {
         $this->lastResult = $this->task->__invoke($this);
         ++$this->iterations;
         if ($this->timer->isPeriodic()) {
             $this->deferred()->notify($this->lastResult);
             if ($this->maxIterations() !== null && $this->iterations() >= $this->maxIterations()) {
                 $this->cancel();
             }
         } else {
             $this->deferred()->resolve($this->lastResult);
         }
     } catch (\Exception $e) {
         $this->deferred()->reject($e);
         $this->timer->cancel();
     }
 }
Exemple #8
0
 /**
  * Schedule a timer for execution.
  *
  * @param TimerInterface $timer
  */
 private function scheduleTimer(TimerInterface $timer)
 {
     $flags = Event::TIMEOUT;
     if ($timer->isPeriodic()) {
         $flags |= Event::PERSIST;
     }
     $event = new Event($this->eventBase, -1, $flags, $this->timerCallback, $timer);
     $this->timerEvents[$timer] = $event;
     $event->add($timer->getInterval());
 }
 private function waitForCompletion(TimerInterface $timer, $endless = false)
 {
     do {
         $donePid = pcntl_waitpid($this->childPid, $status, WNOHANG | WUNTRACED);
         switch ($donePid) {
             case $this->childPid:
                 //donePid is child pid
                 //child done
                 $this->isRunning = false;
                 $timer->cancel();
                 $this->emit('stopped', array($this->childPid, $status, $this));
                 return;
             case 0:
                 //donePid is empty
                 //everything fine.
                 //process still running
                 if ($endless === true) {
                     usleep(100);
                 }
                 break;
             case -1:
                 //donePid is unknown
             //donePid is unknown
             default:
                 $this->emit('error', array(new \RuntimeException("{$this->childPid} PID returned unexpected status. Maybe its not a child of this.")));
                 return;
         }
     } while ($endless === true);
 }
Exemple #10
0
 public function isRunning()
 {
     return $this->timer instanceof TimerInterface && $this->timer->isActive();
 }
 /**
  * Callback called after the CTCP PING timeout is reached.
  *
  * @param \React\EventLoop\Timer\TimerInterface $caller
  */
 public function callbackGrimReaper(TimerInterface $caller)
 {
     $connection = $caller->getData();
     $this->getLogger()->debug('CTCP PING timeout reached, closing connection');
     $this->getEventQueueFactory()->getEventQueue($connection)->ircQuit();
 }
Exemple #12
0
 /**
  * @override
  * @inheritDoc
  */
 public function isTimerActive(\React\EventLoop\Timer\TimerInterface $timer)
 {
     return $this->loop->isTimerActive($timer->getActualTimer());
 }
Exemple #13
0
 public function loopTick(TimerInterface $timer)
 {
     if (count($this->conns) == 0) {
         // If we are shutting down, and have nothing to check, kill the timer.
         if ($this->shuttingDown) {
             // TODO: Possible race condition if shutdown also queues queries, such as a final save.
             // This could be prematurely cancelled.
             $timer->cancel();
         }
         // Nothing in the queue.
         return;
     }
     $reads = $errors = $rejects = [];
     foreach ($this->conns as $conn) {
         $reads[] = $conn['mysqli'];
     }
     // Returns immediately, the non-blocking magic!
     if (mysqli_poll($reads, $errors, $rejects, 0) < 1) {
         return;
     }
     /** @var Connection $read */
     foreach ($reads as $read) {
         /** @var Deferred $deferred */
         $deferred = $this->conns[$read->id]['deferred'];
         $result = $read->reap_async_query();
         if ($result !== false) {
             $deferred->resolve($result);
             // $result is true for non-select queries.
             if ($result instanceof \mysqli_result) {
                 // If userland code has already freed the result, this will throw a warning.
                 // No need to throw a warning here...
                 // If you know how to check if the result has already been freed, please PR!
                 @$result->free();
             }
         } else {
             $deferred->reject($read->error);
         }
         // Release the connection
         $this->pool->releaseConnection($read);
         unset($this->conns[$read->id]);
     }
     // Check error pile.
     // Current understanding is that this would only happen if the connection
     // was closed, or not opened correctly.
     foreach ($errors as $error) {
         $this->pool->releaseConnection($error);
         unset($this->conns[$error->id]);
         throw new \Exception('Unexpected mysqli_poll $error.');
     }
     // Check rejection pile.
     // Current understanding is that this would only happen if we passed a
     // connection that was already reaped. But... maybe not.
     foreach ($rejects as $reject) {
         $this->pool->releaseConnection($reject);
         unset($this->conns[$reject->id]);
         throw new \Exception('Unexpected mysqli_poll $reject.');
     }
     // Duplicated check to avoid one extra tick!
     // If we are shutting down, cancel timer once connections finish.
     if ($this->shuttingDown && count($this->conns) == 0) {
         $timer->cancel();
     }
 }
Exemple #14
0
 private function stop()
 {
     $this->timer->cancel();
     $this->timer = null;
 }
 private function setupTimer(TimerInterface $timer)
 {
     $dummyCallback = function () {
     };
     $interval = $timer->getInterval();
     if ($timer->isPeriodic()) {
         $libevTimer = new \libev\TimerEvent($dummyCallback, $interval, $interval);
     } else {
         $libevTimer = new \libev\TimerEvent($dummyCallback, $interval);
     }
     $libevTimer->setCallback(function () use($timer) {
         call_user_func($timer->getCallback(), $timer);
         if (!$timer->isPeriodic()) {
             $timer->cancel();
         }
     });
     $this->timers->attach($timer, $libevTimer);
     $this->loop->add($libevTimer);
     return $timer;
 }
 /**
  * @return bool
  */
 public function isActive()
 {
     return $this->timer->isActive();
 }