Ejemplo n.º 1
0
 /**
  * @param TaskResult $taskResult
  * @return TaskLogger
  */
 public function getLogger(TaskResult $taskResult)
 {
     $taskExists = false;
     foreach ($this->phase->getTasks() as $task) {
         if ($task->getId() !== $taskResult->getId()) {
             continue;
         }
         $taskExists = true;
     }
     if (!$taskExists) {
         throw new \InvalidArgumentException('Non existing task given');
     }
     if (!array_key_exists($taskResult->getId(), $this->loggers)) {
         $this->loggers[$taskResult->getId()] = new TaskLogger($this->phase->getJob(), $taskResult->getTask(), $this->storageApi);
     }
     return $this->loggers[$taskResult->getId()];
 }
Ejemplo n.º 2
0
 /**
  * @param TaskResult $taskResult
  * @return $this
  */
 public function addTaskResult(TaskResult $taskResult)
 {
     $taskExists = false;
     foreach ($this->tasks as $key => $task) {
         if ($task->getId() !== $taskResult->getId()) {
             continue;
         }
         $this->tasks[$key] = $taskResult;
         $taskExists = true;
     }
     if (!$taskExists) {
         $this->tasks[] = $taskResult;
     }
     return $this;
 }
Ejemplo n.º 3
0
 /**
  * @param Job $job
  * @param TaskResult $taskResult
  * @return TaskResult
  */
 private function executeJobTask(Job $job, TaskResult $taskResult)
 {
     $stopwatchEvent = $this->stopwatch->start($taskResult->getId(), 'task');
     $logger = new TaskLogger($job, $taskResult->getTask(), $this->storageApi);
     $logger->start($stopwatchEvent);
     $startTime = time();
     try {
         $this->fillComponentData($taskResult);
         $httpClient = new Client(array(), $this->logger);
         $response = $httpClient->sendOrchestratorRequest($job, $taskResult->getTask(), $this->encryptor);
         $pollResponse = null;
         // handling async task
         if (ResponseValidator::isAsyncResponse($response)) {
             $taskJobUrl = null;
             try {
                 $data = ResponseDecoder::decode($response);
                 if (!empty($data['url'])) {
                     $taskJobUrl = $data['url'];
                 }
             } catch (\Exception $e) {
             }
             if ($taskJobUrl) {
                 $taskResult->setJobUrl($taskJobUrl);
                 $this->saveJobResult($this->jobResult);
             }
             $this->logTaskUsability($taskResult, $response);
             $retriesCount = 1;
             $timeout = $taskResult->getTimeoutMinutes() ? $taskResult->getTimeoutMinutes() : KeboolaOrchestratorBundle::TIMEOUT_MINUTES;
             // polling job
             do {
                 $data = array('status' => 'unknown');
                 if (time() >= $startTime + $timeout * 60) {
                     throw new Exception\JobRuntimeException(sprintf('Asynchronous task processing timeout after %d minutes', $timeout));
                 }
                 $waitSeconds = min(pow(2, $retriesCount), 20);
                 sleep($waitSeconds);
                 try {
                     $pollResponse = $httpClient->sendOrchestratorPollRequest($job, $response, $this->encryptor, $retriesCount);
                     if ($pollResponse) {
                         $data = ResponseDecoder::decode($pollResponse);
                     }
                 } catch (GuzzleException\RequestException $e) {
                     if ($e instanceof Exception\CurlException) {
                     } else {
                         $prevE = $e->getPrevious();
                         if (!$prevE || !$prevE instanceof Exception\JobRuntimeException) {
                             throw $e;
                         }
                     }
                 } catch (\Exception $e) {
                     // maybe json decode error, do nothing
                 }
                 $retriesCount++;
             } while (array_key_exists('status', $data) && !StatusConverter::isFinishedStatus($data['status']));
         } else {
             // stats log
             if (ResponseValidator::isSuccessfulResponse($response) || ResponseValidator::isInformationalResponse($response)) {
                 $this->logTaskUsability($taskResult, $response);
             }
         }
         if ($pollResponse) {
             $response = $pollResponse;
             if (empty($data) || !in_array($data['status'], array('success', 'ok'))) {
                 //@TODO logovat odpovedi, ktere vraci nesmyslne OK status
                 $fakeRequest = new Request($pollResponse ? 'GET' : 'POST', $response->getHeaderLine(Client::EFFECTIVE_URL_HEADER_NAME));
                 $clientExc = new GuzzleException\ClientException('Error response from component', $fakeRequest, $response);
                 throw $clientExc;
             }
         }
         if (!ResponseValidator::isSuccessfulResponse($response) && !ResponseValidator::isInformationalResponse($response)) {
             $fakeRequest = new Request($pollResponse ? 'GET' : 'POST', $response->getHeaderLine(Client::EFFECTIVE_URL_HEADER_NAME));
             $clientExc = new GuzzleException\ClientException('Response with error HTTP code', $fakeRequest, $response);
             throw $clientExc;
         }
         $endEvent = $logger->end();
     } catch (GuzzleException\ClientException $e) {
         $endEvent = $logger->clientError($e);
         if ($e->getResponse()) {
             $response = $e->getResponse();
         }
     } catch (GuzzleException\BadResponseException $e) {
         $endEvent = $logger->responseError($e);
         if ($e->getResponse()) {
             $response = $e->getResponse();
         }
     } catch (\Exception $e) {
         $endEvent = $logger->someError($e, $this->logger);
         $response = null;
     }
     $taskResult->setResult($endEvent, empty($response) ? null : $response);
     return $taskResult;
 }