Exemplo n.º 1
0
 public function process()
 {
     $request = $this->node->request();
     if (!$request) {
         return true;
     }
     $requestKey = $request['key'];
     // Check the result data in the storage
     $data = $this->node->checkData($requestKey);
     if ($data) {
         $this->node->notify(json_encode(['data_key' => $requestKey, 'request_key' => $requestKey]));
         $this->logger->info('Result data found, notification sent', ['request_key' => $requestKey]);
         return true;
     }
     $request['key'] = $requestKey;
     $workload = json_encode($request);
     if (!$this->node->checkIndex($requestKey)) {
         $this->node->addToIndex($requestKey);
         $this->addTaskBackground($this->function, $workload);
         $this->logger->info('Task added', ['request_key' => $requestKey]);
     } else {
         $this->logger->info('Trying to add duplicate task', ['request_key' => $requestKey]);
     }
     return parent::process();
 }
Exemplo n.º 2
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]);
     }
 }