/** * {@inheritdoc} */ public function validate($entity, Constraint $constraint) { if ($entity instanceof \Akeneo\Bundle\BatchBundle\Entity\JobInstance) { if (!$this->connectorRegistry->getJob($entity)) { $this->context->addViolationAt($constraint->property, $constraint->message, array('{{ job_type }}' => $entity->getType())); } } }
/** * {@inheritdoc} */ public function validate($entity, Constraint $constraint) { if ($entity instanceof \Akeneo\Bundle\BatchBundle\Entity\JobInstance) { if (!$this->connectorRegistry->getJob($entity)) { $this->context->buildViolation($constraint->message, ['%job_type%' => $entity->getType()])->atPath($constraint->property)->addViolation(); } } }
/** * Get the list of jobs that are declared among all the connectors * * @return array with job alias => job */ protected function getAllJobs() { $jobs = []; $jobTypes = $this->connectorRegistry->getConnectors(); foreach ($jobTypes as $jobType) { $connectorTypes = $this->connectorRegistry->getJobs($jobType); foreach ($connectorTypes as $jobsConnectorType) { $jobs = array_merge($jobs, $jobsConnectorType); } } return $jobs; }
/** * @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 JobInstance $jobInstance * @return JobResult */ protected function doJob(JobInstance $jobInstance, JobExecution $jobExecution) { $jobResult = new JobResult(); $jobResult->setSuccessful(false); $isTransactionRunning = $this->isTransactionRunning(); if (!$isTransactionRunning) { $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); $isSuccessful = $this->handleJobResult($jobExecution, $jobResult); if (!$isTransactionRunning && $isSuccessful && !$this->validationMode) { $this->entityManager->commit(); } elseif (!$isTransactionRunning) { $this->entityManager->rollback(); } // trigger save of JobExecution and JobInstance $this->batchJobRepository->getJobManager()->flush(); if (!$this->isClearSkipped($jobExecution)) { $this->batchJobRepository->getJobManager()->clear(); } } catch (\Exception $exception) { if (!$isTransactionRunning) { $this->entityManager->rollback(); } $jobExecution->addFailureException($exception); $jobResult->addFailureException($exception->getMessage()); $this->saveFailedJobExecution($jobExecution); } return $jobResult; }
/** * Add alias field * * @param FormBuilderInterface $builder * * @return \Pim\Bundle\ImportExportBundle\Form\Type\JobInstanceType */ protected function addAliasField(FormBuilderInterface $builder) { $choices = []; foreach ($this->connectorRegistry->getJobs($this->jobType) as $connector => $jobs) { if ('oro_importexport' !== $connector) { foreach ($jobs as $key => $job) { $choices[$connector][$key] = $job->getName(); } } } $builder->add('alias', 'choice', array('choices' => $choices, 'required' => true, 'by_reference' => false, 'mapped' => false, 'empty_value' => 'Select a job', 'empty_data' => null, 'label' => 'Job')); return $this; }
/** * Get a job instance * * @param int $id * @param bool $checkStatus * * @throws NotFoundHttpException * * @return Job|RedirectResponse */ protected function getJobInstance($id, $checkStatus = true) { $jobInstance = $this->findOr404('AkeneoBatchBundle:JobInstance', $id); // Fixme: should look at the job execution to see the status of a job instance execution if ($checkStatus && $jobInstance->getStatus() === JobInstance::STATUS_IN_PROGRESS) { throw $this->createNotFoundException(sprintf('The %s "%s" is currently in progress', $jobInstance->getType(), $jobInstance->getLabel())); } $job = $this->connectorRegistry->getJob($jobInstance); if (!$job) { throw $this->createNotFoundException(sprintf('The following %s does not exist anymore. Please check configuration:<br />' . 'Connector: %s<br />' . 'Type: %s<br />' . 'Alias: %s', $this->getJobType(), $jobInstance->getConnector(), $jobInstance->getType(), $jobInstance->getAlias())); } $jobInstance->setJob($job); return $jobInstance; }
/** * Get a job instance * * @param int $id * @param bool $checkStatus * * @throws NotFoundHttpException * * @return Job|RedirectResponse */ protected function getJobInstance($id, $checkStatus = true) { $jobInstance = $this->jobInstanceRepository->find($id); if (null === $jobInstance) { throw new NotFoundHttpException('Akeneo\\Component\\Batch\\Model\\JobInstance entity not found'); } // Fixme: should look at the job execution to see the status of a job instance execution if ($checkStatus && $jobInstance->getStatus() === JobInstance::STATUS_IN_PROGRESS) { throw new NotFoundHttpException(sprintf('The %s "%s" is currently in progress', $jobInstance->getType(), $jobInstance->getLabel())); } $job = $this->connectorRegistry->getJob($jobInstance); if (!$job) { throw new NotFoundHttpException(sprintf('The following %s does not exist anymore. Please check configuration:<br />' . 'Connector: %s<br />' . 'Type: %s<br />' . 'Alias: %s', $this->getJobType(), $jobInstance->getConnector(), $jobInstance->getType(), $jobInstance->getAlias())); } $jobInstance->setJob($job); return $jobInstance; }
/** * Return filter choices for the connector column * * @param string $type * * @return array */ protected function getConnectorChoices($type) { $connectors = array_keys($this->registry->getJobs($type)); return empty($connectors) ? [] : array_combine($connectors, $connectors); }