Ejemplo n.º 1
0
 protected function handleRequest(array $request)
 {
     $this->logger->info('Handling request from queue', ['node' => $this->name, 'parentNode' => $this->parentName]);
     $childRequestKey = $request['key'];
     // Check the result data in the storage
     $data = $this->node->checkData($childRequestKey);
     if ($data) {
         $this->node->notify(json_encode(['data_key' => $childRequestKey, 'request_key' => $childRequestKey]));
         $this->logger->info('Result data found, notification sent', ['child_request_key' => $childRequestKey]);
         return true;
     }
     // @todo Возможно ли возникновение разных запросов к родительскому узлу при одинаковых запросах клиентских узлов?!
     if (!$this->node->checkIndex($childRequestKey)) {
         $requestKey = $this->prepareRequest($request);
         if (!is_string($requestKey) || empty($requestKey)) {
             throw new \Exception(__CLASS__ . "::prepareRequest() must return the key of request");
         }
         // Связываем запрос от дочернего узла с запросом настоящего узла к родительскому.
         // Делается это для того, чтобы после обработки можно было предоставить результаты по тому запросу, по которому их ожидают.
         $this->node->addToIndex($childRequestKey, $requestKey);
         // Теперь нужно здесь же по уведомлению вытаскивать все запросы клиентов и их комманды и уже ставить таск с ними.
         // Таким образом отпадает необходимость в data_key и можно по-прежнему использовать request_key в качестве идентификатора данных
         // + универсальный воркер по идее будет
         $this->node->saveRequestCommands($childRequestKey, $request['commands']);
     } else {
         $this->logger->info('Trying to add duplicate task', ['child_request_key' => $childRequestKey]);
     }
 }