Beispiel #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();
 }
Beispiel #2
0
 public function process(\GearmanJob $job)
 {
     if (!parent::process($job)) {
         return true;
     }
     if (!isset($this->workload['commands']) || !is_array($this->workload['commands'])) {
         $this->logger->warn('Nothing to do, commands list is empty', ['request_key' => $this->workload['key']]);
         return true;
     }
     foreach ($this->workload['commands'] as $c) {
         $name = $c['name'];
         $this->logger->info('Prepare command', ['command' => $name]);
         $command = CommandRegistry::get($name);
         $c['params'] = isset($c['params']) ? $c['params'] : null;
         $this->results[$name] = $command->execute($c['params'], $this->workload);
     }
     try {
         $this->saveResults();
     } catch (\MongoException $e) {
         $this->logger->fatal('Failed to save the results of data processing', ['worker' => __CLASS__]);
     }
     $notify = ['request_key' => $this->workload['key'], 'data_key' => $this->workload['key']];
     $this->node->notify(json_encode($notify));
     $this->logger->info("Task complete", ['request_key' => $this->workload['key']]);
     return true;
 }
Beispiel #3
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]);
     }
 }