/** * @param Exception\JobRuntimeException $e * @return StorageApiEvent */ public function runtimeError(Exception\JobRuntimeException $e) { $event = $this->prepareEvent(); $event->setType(StorageApiEvent::TYPE_ERROR)->setMessage(sprintf(self::MESSAGE_END, $this->job->getId()))->setResults(array('error' => $e->getMessage()))->setDescription($e->getMessage()); $this->save($event); return $event; }
public function execute(SyrupJob $job) { parent::build($job); $jobLogger = new JobLogger($this->job, $this->storageApi); try { $jobLogger->start($this->stopwatch->getEvent(self::STOPWATCH_ORCHESTRATION_EVENT)); if ($this->job->getTasks()) { /** @var StorageApi\OrchestrationTask[] $rows */ $orchestrationTasks = array_map(function ($line) { $task = new StorageApi\OrchestrationTask(); $task->fromArray(json_decode(json_encode($line), true)); return $task; }, $this->job->getTasks()); } else { $orchestrationTasks = $this->loadTasks($this->job, $this->token); if (!$this->job->getTasks() && $orchestrationTasks) { $this->logger->info('orchestration.job.emptyTasks', array('jobId' => $this->job->getId(), 'orchestrationId' => $this->job->getOrchestrationId(), 'orchestrationName' => $this->job->getOrchestrationName(), 'projectId' => $this->job->getProjectId())); } } // fill result foreach ($orchestrationTasks as $task) { $this->jobResult->addTaskResult(new TaskResult($task)); } $this->saveJobResult($this->jobResult); foreach ($orchestrationTasks as $task) { // skip inactive taks if (!$task->getActive()) { continue; } try { $taskResult = (new TaskResult($task))->setProcessingStatus(); $this->jobResult->addTaskResult($taskResult); $this->saveJobResult($this->jobResult); $result = $this->executeJobTask($this->job, $taskResult, $this->storageApi); $this->jobResult->addTaskResult($result); $this->saveJobResult($this->jobResult); if (!$task->getContinueOnFailure() && $result->getError()) { break; } if ($task->getContinueOnFailure() && $result->getError()) { continue; } } catch (\Exception $taskE) { //@FIXME better handling = not log to sapi throw Exception\JobRuntimeException::factory($taskE); } } $jobLogger->end($this->jobResult); $status = StatusConverter::orchestratorToSyrup($this->jobResult->getEventType()); $result = array('tasks' => $this->jobResult->toArray()); if ($status === SyrupJob::STATUS_SUCCESS) { return $result; } if ($status === SyrupJob::STATUS_WARNING) { $eData = array('jobId' => $this->job->getId(), 'orchestrationId' => $this->orchestration->getId(), 'projectId' => $this->orchestration->getProjectId()); $e = new JobException(500, sprintf('Some of tasks failed'), null, $eData); $e->setStatus($status)->setResult($result); throw $e; } if ($status === SyrupJob::STATUS_ERROR) { $eData = array('jobId' => $this->job->getId(), 'orchestrationId' => $this->orchestration->getId(), 'projectId' => $this->orchestration->getProjectId()); $e = new JobException(500, sprintf('Job failed', $job->getId()), null, $eData); $e->setStatus($status)->setResult($result); throw $e; } } catch (Exception\JobInvalidStateException $e) { $jobLogger->stateError($e); // any change of job status, job is probably processing } catch (Exception\JobRuntimeException $e) { throw $e->getPrevious(); } return array(); }