private function setTimeoutWatcher(int $secs = self::HEARTBEAT_TIMEOUT_SECONDS) { $this->timeoutWatcherId = once(function () { $this->logger->log(Level::DEBUG, "Connection to {$this->roomIdentifier} timed out"); $this->endpoint->close(); }, $secs * 1000); $this->logger->log(Level::DEBUG, "Created timeout watcher #{$this->timeoutWatcherId}"); }
private function scheduleActionsForUnapprovedRoom(Identifier $identifier) { $now = time(); $inviteTime = (yield $this->storage->getInviteTimestamp($identifier)); $remind1Delay = ($now - ($inviteTime + 3600 * 12)) * 1000; $remind2Delay = ($now - ($inviteTime + 3600 * 23)) * 1000; $leaveDelay = ($now - ($inviteTime + 3600 * 24)) * 1000; if ($remind1Delay > 0) { $this->timerWatchers[$identifier->getIdentString()]['remind1'] = once(function () use($identifier) { unset($this->timerWatchers[$identifier->getIdentString()]['remind1']); $this->enqueueAction([$this, 'unapprovedRoomFirstReminder'], $identifier); }, $remind1Delay); } if ($remind2Delay > 0) { $this->timerWatchers[$identifier->getIdentString()]['remind2'] = once(function () use($identifier) { unset($this->timerWatchers[$identifier->getIdentString()]['remind2']); $this->enqueueAction([$this, 'unapprovedRoomSecondReminder'], $identifier); }, $remind2Delay); } if ($leaveDelay > 0) { $this->timerWatchers[$identifier->getIdentString()]['leave'] = once(function () use($identifier) { unset($this->timerWatchers[$identifier->getIdentString()]['leave']); $this->enqueueAction([$this, 'unapprovedRoomLeave'], $identifier); }, $leaveDelay); } }
public function rescheduleUpcomingReminders(ChatRoom $room, array $reminders) : \Generator { if (!$reminders) { return; } $this->watchers = []; foreach ($reminders as $key) { $key = (string) $key; $value = (yield $this->storage->get($key, $room)); $text = $value['text']; $name = $value['username']; $stamp = $value['timestamp']; $seconds = $stamp - time(); if ($seconds <= 0) { continue; } if (null !== ($pingableName = (yield $this->chatClient->getPingableName($room, $name)))) { $target = "@{$pingableName}"; $reply = $target . " " . $text; $this->watchers[] = once(function () use($room, $key, $reply) { (yield $this->storage->unset($key, $room)); return $this->chatClient->postMessage($room, $reply, PostFlags::ALLOW_PINGS); }, $seconds * 1000); } } }