/** * Create a new file driven handler instance. * * @param \Illuminate\Filesystem\Filesystem $files * @param React\EventLoop\LoopInterface $loop * @param string $path * * @return void */ public function __construct(Factory $factory, LoopInterface $loop, Server $server, $lifetime) { $this->factory = $factory; $this->lifetime = $lifetime; $this->server = $server; $this->loop = $loop; // PING redis connection every 5 minutes // and try to reconnect on connection error $this->timer = $loop->addPeriodicTimer(60 * 1, function () use($server) { $this->promise->then(function ($client) use($server) { $client->PING()->then(function ($pong) use($server) { $server->log('Redis server responded ping with: %s', [$pong]); return $pong == 'PONG'; }, function ($e) { return $this->reconnectByError($e); }); }); }); // server statistics for redis connection $this->loop->addPeriodicTimer(60 * 30, function () { $this->server->log('Server statistics to the last 30 minutes.'); $this->server->log('Best time of %fs, poor time of %fs and a average of %f seconds for total %d requests.', array_values($this->statistics)); $this->statistics = array('best' => 0, 'poor' => 0, 'avg' => 0, 'total' => 0); }); }
/** * * */ protected function updateCredentials($response, $callback) { $session = $response->originRequest()->session(); $url = $this->server->get('api_url') . $this->server['config']->get('grant_path'); $data = json_encode(array('client_id' => $this->server['config']->get('client_id'), 'client_secret' => $this->server['config']->get('client_secret'), 'refresh_token' => $session->get('oauth_grant.refresh_token'), 'grant_type' => 'refresh_token')); $request = $this->client->request('POST', $url, array('content-type' => 'application/json;charset=UTF-8', 'content-length' => strlen($data))); $request->on('response', function ($clientResponse) use($response, $request, $callback, $session) { if ($clientResponse->getCode() != 200) { $clientResponse->on('data', function ($data) use($clientResponse, $request, $callback, $session) { $session->forget('oauth_grant'); $this->server->log('Não foi possível autenticar o usuário utilizando o refresh token (%s).', [$data]); }); return $callback($clientResponse); } $clientResponse->on('data', function ($data) { $this->bufferData($data); }); $clientResponse->on('end', function ($data) use($clientResponse, $response, $callback) { $data = $this->getDataEnd(); $this->processResponse($response, json_decode($data, true)); $callback($clientResponse); }); }); $request->end($data); }
/** * Connect to server and retrun promise with client. * * @return \React\Promise\PromiseInterface */ public function createClient() { return $this->factory->createClient($this->savePath)->then(function ($client) { $this->server->log('Connected with success to Redis server.'); return $client; }, function ($e) { $this->server->log('Application got an error on try to connect with Redis server: %s.', [$e->getMessage()]); throw $e; }); }