public function testFailureExceptions() { $obj = new JobResult(); $obj->addFailureException('Error 1'); $obj->addFailureException('Error 2'); $this->assertEquals(array('Error 1', 'Error 2'), $obj->getFailureExceptions()); }
/** * @param JobExecution $jobExecution * @param JobInstance $jobInstance * @return JobResult */ protected function doJob(JobInstance $jobInstance, JobExecution $jobExecution) { $jobResult = new JobResult(); $jobResult->setSuccessful(false); try { $job = $this->batchJobRegistry->getJob($jobInstance); if (!$job) { throw new RuntimeException(sprintf('Can\'t find job "%s"', $jobInstance->getAlias())); } $job->execute($jobExecution); $failureExceptions = $this->collectFailureExceptions($jobExecution); if ($jobExecution->getStatus()->getValue() == BatchStatus::COMPLETED && !$failureExceptions) { $jobResult->setSuccessful(true); } else { foreach ($failureExceptions as $failureException) { $jobResult->addFailureException($failureException); } } // trigger save of JobExecution and JobInstance $this->batchJobRepository->getJobManager()->flush(); $this->batchJobRepository->getJobManager()->clear(); } catch (\Exception $exception) { $jobExecution->addFailureException($exception); $jobResult->addFailureException($exception->getMessage()); $this->saveFailedJobExecution($jobExecution); } return $jobResult; }
/** * @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; }
/** * @dataProvider eventDataProvider * * @param string $jobName * @param string $exceptionMessage * @param string $expectedMessage */ public function testProcess($jobName, $exceptionMessage, $expectedMessage) { $jobResult = new JobResult(); $jobResult->addFailureException($exceptionMessage); $event = new SyncEvent($jobName, [], $jobResult); $this->listener->process($event); $exceptions = $jobResult->getFailureExceptions(); $this->assertEquals($expectedMessage, reset($exceptions)); }
/** * @param JobExecution $jobExecution * @param JobInstance $jobInstance * @return JobResult */ protected function doJob(JobInstance $jobInstance, JobExecution $jobExecution) { $jobResult = new JobResult(); $jobResult->setSuccessful(false); $this->entityManager->beginTransaction(); try { $job = $this->batchJobRegistry->getJob($jobInstance); if (!$job) { throw new RuntimeException(sprintf('Can\'t find job "%s"', $jobInstance->getAlias())); } $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()); } return $jobResult; }
/** * @param JobExecution $jobExecution * @param JobResult $jobResult * * @return bool */ protected function handleJobResult(JobExecution $jobExecution, JobResult $jobResult) { $failureExceptions = $this->collectFailureExceptions($jobExecution); $isSuccessful = $jobExecution->getStatus()->getValue() === BatchStatus::COMPLETED && !$failureExceptions; if ($isSuccessful) { $jobResult->setSuccessful(true); } elseif ($failureExceptions) { foreach ($failureExceptions as $failureException) { $jobResult->addFailureException($failureException); } } return $isSuccessful; }