/** * Start the worker and wait for requests */ public function listen() { $context = new \ZMQContext(); $server = new \ZMQSocket($context, \ZMQ::SOCKET_PULL); $server->bind('tcp://127.0.0.1:' . ($this->defaultPort + $this->client->getId() - 1)); $this->logger->info('Client worker ' . $this->client . ' is ready'); while (true) { $request = $server->recv(); $this->logger->debug('Client worker ' . $this->client . ' receiving request : ' . $request); // Check if the input is valid, ignore if wrong $request = json_decode($request, true); if (!$this->isValidInput($request)) { $this->logger->error('Client worker ' . $this->client . ' received an invalid input'); continue; } try { // Call the right method in the client and push to redis the result $result = call_user_func_array(array($this->client, $request['command']), $request['parameters']); } catch (ClientNotReadyException $e) { $this->logger->warning('Client worker ' . $this->client . ' received a request (#' . $request['invokeId'] . ') whereas the client is not ready. This is normal in case of client reconnection process. Ignoring.'); continue; } $key = $this->key . '.client.commands.' . $request['invokeId']; $this->redis->rpush($key, serialize($result)); $this->redis->expire($key, $this->expire); } }
/** * Cleaning all asynchronous client processes registered by cache files * * @param bool $throwException * @param null|LOLClientInterface $client * * @throws \RuntimeException */ protected function cleanAsyncClients($throwException = false, $client = null) { $this->logger->info('Cleaning cached async clients...'); $cachePath = __DIR__ . '/../../../../' . ConfigurationLoader::get('cache.path') . '/' . 'clientpids'; if (!is_dir($cachePath)) { if (!mkdir($cachePath, 0777, true)) { throw new \RuntimeException('Cannot write in the cache folder'); } } if (null != $client) { $path = $cachePath . '/client_' . $client->getId() . '.pid'; if (!is_file($path)) { return; } Process::killProcess($path, $throwException, $this->logger, $client); } else { $iterator = new \DirectoryIterator($cachePath); foreach ($iterator as $pidFile) { if ($pidFile->isDir()) { continue; } Process::killProcess($pidFile->getRealPath(), $throwException, $this->logger); } } }