Exemplo n.º 1
0
 /**
  * @param string $jobType
  * @param string $jobName
  * @param array $configuration
  * @return JobResult
  */
 public function executeJob($jobType, $jobName, array $configuration = array())
 {
     // create and persist job instance and job execution
     $jobInstance = new JobInstance(self::CONNECTOR_NAME, $jobType, $jobName);
     $jobInstance->setCode($this->generateJobCode($jobName));
     $jobInstance->setLabel(sprintf('%s.%s', $jobType, $jobName));
     $jobInstance->setRawConfiguration($configuration);
     $jobExecution = new JobExecution();
     $jobExecution->setJobInstance($jobInstance);
     $jobResult = new JobResult();
     $jobResult->setSuccessful(false);
     $this->entityManager->beginTransaction();
     try {
         $job = $this->jobRegistry->getJob($jobInstance);
         if (!$job) {
             throw new RuntimeException(sprintf('Can\'t find job "%s"', $jobName));
         }
         // TODO: Refactor whole logic of job execution to perform actions in transactions
         $job->execute($jobExecution);
         $failureExceptions = $this->collectFailureExceptions($jobExecution);
         if ($jobExecution->getStatus()->getValue() == BatchStatus::COMPLETED && !$failureExceptions) {
             $this->entityManager->commit();
             $jobResult->setSuccessful(true);
         } else {
             $this->entityManager->rollback();
             foreach ($failureExceptions as $failureException) {
                 $jobResult->addFailureException($failureException);
             }
         }
     } catch (\Exception $exception) {
         $this->entityManager->rollback();
         $jobExecution->addFailureException($exception);
         $jobResult->addFailureException($exception->getMessage());
     }
     // save job instance
     $this->entityManager->persist($jobInstance);
     $this->entityManager->flush($jobInstance);
     // set data to JobResult
     $jobResult->setJobId($jobInstance->getId());
     $jobResult->setJobCode($jobInstance->getCode());
     /** @var JobExecution $jobExecution */
     $jobExecution = $jobInstance->getJobExecutions()->first();
     if ($jobExecution) {
         $stepExecution = $jobExecution->getStepExecutions()->first();
         if ($stepExecution) {
             $context = $this->contextRegistry->getByStepExecution($stepExecution);
             $jobResult->setContext($context);
         }
     }
     return $jobResult;
 }
Exemplo n.º 2
0
 /**
  * Set data to JobResult
  * TODO: Find a way to work with multiple amount of job and step executions
  * TODO https://magecore.atlassian.net/browse/BAP-2600
  *
  * @param JobResult $jobResult
  * @param JobInstance $jobInstance
  */
 protected function setJobResultData(JobResult $jobResult, JobInstance $jobInstance)
 {
     $jobResult->setJobId($jobInstance->getId());
     $jobResult->setJobCode($jobInstance->getCode());
     /** @var JobExecution $jobExecution */
     $jobExecution = $jobInstance->getJobExecutions()->first();
     if ($jobExecution) {
         $stepExecutions = $jobExecution->getStepExecutions();
         /** @var StepExecution $firstStepExecution */
         $firstStepExecution = $stepExecutions->first();
         if ($firstStepExecution) {
             $context = $this->contextRegistry->getByStepExecution($firstStepExecution);
             if ($stepExecutions->count() > 1) {
                 /** @var StepExecution $stepExecution */
                 foreach ($stepExecutions->slice(1) as $stepExecution) {
                     ContextHelper::mergeContextCounters($context, $this->contextRegistry->getByStepExecution($stepExecution));
                 }
             }
             $jobResult->setContext($context);
         }
     }
 }