Beispiel #1
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();
     }
 }
Beispiel #2
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 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 isPeriodic()
 {
     return $this->timer->isPeriodic();
 }