public static function cancelCurrentTimeoutTimer() { if (static::$timeoutTimer !== null) { static::$timeoutTimer->cancel(); static::$timeoutTimer = null; } }
public function eventListenerRemoved() { //If it's the last event if ($this->countListeners(self::EVENT_CHANGE) === 0) { $this->poll_timer->cancel(); } }
/** * Cancels timer */ public function stop() { if ($this->timer && $this->timer->isActive()) { $this->timer->cancel(); } $this->timer = null; }
public function stop() { if ($this->isRunning()) { $this->timer->cancel(); $this->timer = null; } return $this; }
/** * @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(); }); }
/** */ 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(); } }
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); }
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(); } }
private function stop() { $this->timer->cancel(); $this->timer = null; }