/** * Hook for modify job after job execution * * @param Job $job * @return void */ public function postExecution(Job $job) { if ($job->getId() !== $this->job->getId()) { throw new \InvalidArgumentException('Given job must be same as previous executed'); } if ($job->getComponent() !== $this->job->getComponent()) { throw new \InvalidArgumentException('Given job must be same as previous executed'); } $job->setResult($job->getResult() + array(self::HOOK_RESULT_KEY => self::HOOK_RESULT_VALUE)); $this->jobMapper->update($job); }
public function cleanup(Metadata\Job $job) { $result = $job->getResult(); if (!$result) { $result = array(); } $esJob = new Job(); $esJob->build($job); try { $token = new Token($this->storageApi); } catch (StorageApiException $e) { $this->logger->error('Cleanup error - invalid token', array('jobId' => $esJob->getId())); throw new UserException(sprintf("Invalid token for job %d", $esJob->getId()), $e); } $orchestration = $this->orchestrationManager->findOrchestrationById($esJob->getOrchestrationId(), $token); if (!$orchestration) { $this->logger->error('Cleanup error - orchestration not found', array('jobId' => $esJob->getId())); throw new UserException(sprintf("Orchestration %s not found. Could not update last job", $esJob->getOrchestrationId())); } if (!empty($result['tasks'])) { foreach ($result['tasks'] as $key => $task) { // skip non processing tasks if ($task['status'] !== Metadata\Job::STATUS_PROCESSING) { continue; } $httpClient = new Client(array(), $this->logger); // skip tasks without URL if (empty($task['jobUrl'])) { $retriesCount = 1; $jobsCount = 0; do { $waitSeconds = min(pow(2, $retriesCount), 20); $this->logger->debug('Poll jobs count', array('sleep' => $waitSeconds)); sleep($waitSeconds); try { $jobsCount = $httpClient->getNonFinishedChildJobsCount($esJob, $this->encryptor); } catch (\GuzzleHttp\Exception\RequestException $e) { $this->logger->error('Cleanup jobs count error', array('sleep' => $waitSeconds, 'exception' => $e)); } catch (\Exception $e) { $this->logger->error('Cleanup jobs count error', array('sleep' => $waitSeconds, 'exception' => $e)); } $retriesCount++; } while ($jobsCount != 0); $result['tasks'][$key]['status'] = Metadata\Job::STATUS_TERMINATED; $result['tasks'][$key]['endTime'] = (new \DateTime())->format('c'); } else { $this->logger->debug('Check task status', array('task' => $task)); $retriesCount = 1; do { $data = array('status' => 'unknown'); $waitSeconds = min(pow(2, $retriesCount), 20); $this->logger->debug('Poll task status', array('task' => $task, 'sleep' => $waitSeconds)); sleep($waitSeconds); try { $response = $httpClient->getJobStatus($task['jobUrl'], $esJob, $this->encryptor); if ($response) { $data = ResponseDecoder::decode($response); $this->logger->debug('Poll response', array('task' => $task, 'reponse' => $data)); } else { $this->logger->error('Any poll response', array('task' => $task, 'sleep' => $waitSeconds)); } } catch (\GuzzleHttp\Exception\RequestException $e) { $this->logger->error('Cleanup poll job error', array('task' => $task, 'sleep' => $waitSeconds, 'exception' => $e)); } catch (\Exception $e) { $this->logger->error('Cleanup poll job error', array('task' => $task, 'sleep' => $waitSeconds, 'exception' => $e)); } $retriesCount++; } while (!array_key_exists('isFinished', $data) || $data['isFinished'] != 1); $result['tasks'][$key]['status'] = $data['status']; if (!empty($data['endTime'])) { $result['tasks'][$key]['endTime'] = $data['endTime']; } if (!empty($data['durationSeconds'])) { $result['tasks'][$key]['duration'] = $data['durationSeconds']; } } } } $jobEsManager = $this->jobManagerFactory->createJobManager($token); $this->logger->debug('Cleanup job', array('jobId' => $esJob->getId())); $job->setResult($result); $esJob->setResults($result); $jobEsManager->updateResult($esJob, $result); }
public function postCleanup(Job $job) { $oldRes = $job->getResult(); $job->setResult(['message' => $oldRes['message'] . '&cleared']); $this->jobMapper->update($job); }
/** * Hook for modify job after job execution * * @param Job $job * @return void */ public function postExecution(Job $job) { $job->setResult($job->getResult() + array(self::HOOK_RESULT_KEY => self::HOOK_RESULT_VALUE)); $this->jobMapper->update($job); }
public function build(\Keboola\Syrup\Job\Metadata\Job $job) { $params = $job->getRawParams(); $process = $job->getProcess(); $token = $job->getToken(); $project = $job->getProject(); $this->setId($job->getId()); if (!empty($params['config'])) { $this->setConfig($params['config']); } $this->setProjectId($project['id']); $this->setOrchestrationId($params['orchestration']['id']); $this->setToken($token['token']); $this->setTokenId($token['id']); $this->setTokenDesc($token['description']); $this->setTokenOwnerName($project['name']); $this->setStatus($job->getStatus()); $this->setStartTime($job->getStartTime()); $this->setEndTime($job->getEndTime()); $this->setResults($job->getResult()); if (!empty($params['tasks'])) { $this->setTaks($params['tasks']); } $this->setCreatedTime($job->getCreatedTime()); $this->setOrchestrationName($params['orchestration']['name']); $this->setInitializedBy($params['initializedBy']); $this->setInitiatorTokenId($params['initiator']['id']); $this->setInitiatorTokenDesc($params['initiator']['description']); $this->setInitiatorUserAgent($params['initiator']['userAgent']); $this->setNotificationsEmails($params['notificationsEmails']); $this->setRunId($job->getRunId()); $this->setPid($process['pid']); $this->setWorkerAddress($process['host']); }