/** * Update configurable delta export. * * @param Channel $channel * @param JobInstance $jobInstance * @param string $identifier */ public function setLastExportDate(Channel $channel, JobInstance $jobInstance, $identifier) { $variantGroup = $this->groupRepository->findOneBy(['code' => $identifier]); if ($variantGroup) { $deltaConfigurableTable = $this->tableNameBuilder->getTableName('pim_magento_connector.entity.delta_configurable_export.class'); $exportableProducts = $this->productFilter->apply($channel, $variantGroup->getProducts()); foreach ($exportableProducts as $product) { $sql = <<<SQL INSERT INTO {$deltaConfigurableTable} (product_id, job_instance_id, last_export) VALUES (:product_id, :job_instance_id, :last_export) ON DUPLICATE KEY UPDATE last_export = :last_export SQL; $connection = $this->em->getConnection(); $query = $connection->prepare($sql); $now = new \DateTime('now', new \DateTimeZone('UTC')); $lastExport = $now->format('Y-m-d H:i:s'); $productId = $product->getId(); $jobInstanceId = $jobInstance->getId(); $query->bindParam(':last_export', $lastExport, PDO::PARAM_STR); $query->bindParam(':product_id', $productId, PDO::PARAM_INT); $query->bindParam(':job_instance_id', $jobInstanceId, PDO::PARAM_INT); $query->execute(); } } }
/** * {@inheritdoc} * * @return JobInstance */ protected function createEntity(array $data) { $job = new JobInstance($data['connector'], $data['type'], 'alias'); $job->setCode($data['code']); $job->setLabel($data['label']); return $job; }
/** * @param JobInstance|null $jobInstance */ public function clear(JobInstance $jobInstance = null) { $alias = self::DEFAULT_ALIAS; if ($jobInstance) { $alias = $jobInstance->getAlias(); } unset($this->contexts[$alias]); }
/** * Launch a job * TODO: refactor all this * * @param JobInstance $job */ protected function launchJob(JobInstance $job) { $app = new Application($this->container->get('kernel')); $cmd = new BatchCommand(); $cmd->setContainer($this->container); $cmd->setApplication($app); $cmd->run(new ArrayInput(['command' => 'akeneo:batch:job', 'code' => $job->getCode(), '--no-debug' => true, '--no-log' => true, '-v' => true]), new ConsoleOutput()); $execution = $this->getJobExecution($job); if (!$this->executionComplete($execution)) { throw new JobExecutionException($execution); } }
/** * {@inheritdoc} */ public function createJobExecution(JobInstance $jobInstance) { if (null !== $jobInstance->getId()) { $jobInstance = $this->jobManager->merge($jobInstance); } else { $this->jobManager->persist($jobInstance); } $jobExecution = new $this->jobExecutionClass(); $jobExecution->setJobInstance($jobInstance); $this->updateJobExecution($jobExecution); return $jobExecution; }
/** * Launch a job * TODO: refactor all this * * @param JobInstance $job */ protected function launchJob(JobInstance $job) { $app = new Application($this->container->get('kernel')); $cmd = new BatchCommand(); $cmd->setContainer($this->container); $cmd->setApplication($app); $cmd->run(new ArrayInput(['command' => 'akeneo:batch:job', 'code' => $job->getCode(), '--no-debug' => true]), new ConsoleOutput()); $execution = $this->getJobExecution($job); if (ExitStatus::COMPLETED !== $execution->getExitStatus()->getExitCode()) { throw new \Exception(sprintf('An error occured during execution of the job "%s", detailled trace can be found in %s', $job->getCode(), $execution->getLogFile())); } }
/** * Update export date for the given product. * * @param ProductInterface $product * @param JobInstance $jobInstance * @param string $table */ protected function updateExport(ProductInterface $product, JobInstance $jobInstance, $table) { $conn = $this->entityManager->getConnection(); $sql = "\n INSERT INTO {$table}\n (product_id, job_instance_id, last_export)\n VALUES (:product_id, :job_instance_id, :last_export)\n ON DUPLICATE KEY UPDATE last_export = :last_export\n "; $now = new \DateTime('now', new \DateTimeZone('UTC')); $formattedNow = $now->format('Y-m-d H:i:s'); $productId = $product->getId(); $jobInstanceId = $jobInstance->getId(); $query = $conn->prepare($sql); $query->bindParam(':last_export', $formattedNow, PDO::PARAM_STR); $query->bindParam(':product_id', $productId, PDO::PARAM_INT); $query->bindParam(':job_instance_id', $jobInstanceId, PDO::PARAM_INT); $query->execute(); }
protected function setUp() { $this->initClient(); $this->loadFixtures(['OroCRM\\Bundle\\MagentoBundle\\Tests\\Functional\\Fixture\\LoadNewsletterSubscriberData']); $this->strategy = $this->getContainer()->get('orocrm_magento.import.strategy.newsletter_subscriber.add_or_update'); $jobInstance = new JobInstance(); $jobInstance->setRawConfiguration(['channel' => 3]); $jobExecution = new JobExecution(); $jobExecution->setJobInstance($jobInstance); $this->stepExecution = new StepExecution('step', $jobExecution); $this->context = new StepExecutionProxyContext($this->stepExecution); $this->strategy->setImportExportContext($this->context); $this->strategy->setStepExecution($this->stepExecution); }
/** * To string * @return string */ public function __toString() { $startTime = self::formatDate($this->startTime); $endTime = self::formatDate($this->endTime); $updatedTime = self::formatDate($this->updatedTime); $jobInstanceCode = $this->jobInstance != null ? $this->jobInstance->getCode() : ''; $message = "startTime=%s, endTime=%s, updatedTime=%s, status=%d, exitStatus=%s, exitDescription=[%s], job=[%s]"; return sprintf($message, $startTime, $endTime, $updatedTime, $this->status, $this->exitStatus, $this->exitDescription, $jobInstanceCode); }
function it_normalizes_job_instance(JobInstance $jobinstance) { $jobinstance->getCode()->willReturn('product_export'); $jobinstance->getLabel()->willReturn('Product export'); $jobinstance->getConnector()->willReturn('myconnector'); $jobinstance->getType()->willReturn('EXPORT'); $jobinstance->getRawConfiguration()->willReturn(['delimiter' => ';']); $this->normalize($jobinstance)->shouldReturn(['code' => 'product_export', 'label' => 'Product export', 'connector' => 'myconnector', 'type' => 'EXPORT', 'configuration' => ['delimiter' => ';']]); }
/** * @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); // persist batch entities $this->batchJobManager->persist($jobInstance); $this->batchJobManager->persist($jobExecution); // do job $jobResult = $this->doJob($jobInstance, $jobExecution); // EntityManager can be closed when there was an exception in flush method called inside some jobs execution // Can't be implemented right now due to OroEntityManager external dependencies // on ExtendManager and FilterCollection if (!$this->entityManager->isOpen()) { $this->managerRegistry->resetManager(); $this->entityManager = $this->managerRegistry->getManager(); } // flush batch entities $this->batchJobManager->flush($jobInstance); $this->batchJobManager->flush($jobExecution); // set data to JobResult $jobResult->setJobId($jobInstance->getId()); $jobResult->setJobCode($jobInstance->getCode()); // TODO: Find a way to work with multiple amount of job and step executions // TODO: https://magecore.atlassian.net/browse/BAP-2600 /** @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; }
/** * Update product export date for the given product * @param string $identifier * @param JobInstance $jobInstance */ public function updateProductExport($identifier, JobInstance $jobInstance) { $now = new \DateTime('now', new \DateTimeZone('UTC')); $product = $this->productRepository->findByReference((string) $identifier); if (class_exists('\\PimEnterprise\\Bundle\\WorkflowBundle\\Model\\PublishedProduct')) { if ($product instanceof \PimEnterprise\Bundle\WorkflowBundle\Model\PublishedProduct) { /**@var \PimEnterprise\Bundle\WorkflowBundle\Model\PublishedProduct $product **/ $productId = $product->getOriginalProduct()->getId(); $product = $product->getOriginalProduct(); } } if (null != $product) { $productExport = $this->productExportRepository->findOneBy(array('product' => $product, 'jobInstance' => $jobInstance)); $conn = $this->entityManager->getConnection(); $jobInstance->getId(); $product->getId(); if (null === $productExport) { $sql = ' INSERT INTO pim_delta_product_export (product_id, job_instance_id, date) VALUES (:product_id, :job_instance_id, :date) '; } else { $sql = ' UPDATE pim_delta_product_export SET date = :date WHERE product_id = :product_id AND job_instance_id = :job_instance_id '; } $q = $conn->prepare($sql); $date = $now->format('Y-m-d H:i:s'); $productId = $product->getId(); $jobInstanceId = $jobInstance->getId(); $q->bindParam(':date', $date, PDO::PARAM_STR); $q->bindParam(':product_id', $productId, PDO::PARAM_INT); $q->bindParam(':job_instance_id', $jobInstanceId, PDO::PARAM_INT); $q->execute(); } }
/** * Get a registered job definition from a JobInstance * * @param JobInstance $jobInstance * * @return JobInterface * @throws \LogicException */ public function getJob(JobInstance $jobInstance) { if ($connector = $this->getConnector($jobInstance->getConnector(), $jobInstance->getType())) { if ($job = $this->getConnectorJob($connector, $jobInstance->getAlias())) { $job->setConfiguration($jobInstance->getRawConfiguration()); $jobInstance->setJob($job); return $job; } } return null; }
public function loadJobInstances(ObjectManager $manager) { $handle = fopen(__DIR__ . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'job_instance_data.csv', 'r'); $headers = fgetcsv($handle, 1000, ','); while (($data = fgetcsv($handle, 1000, ',')) !== false) { $combined = array_combine($headers, $data); $jobInstanceEntity = new JobInstance(); $jobInstanceEntity->setCode($combined['Code']); $jobInstanceEntity->setAlias($combined['Alias']); $jobInstanceEntity->setStatus($combined['Status']); $jobInstanceEntity->setConnector($combined['Connector']); $jobInstanceEntity->setType($combined['Type']); $manager->persist($jobInstanceEntity); $this->jobInstances[$combined['Id']] = $jobInstanceEntity; } fclose($handle); }
public function testToString() { $startTime = new \DateTime('2013-02-01 12:34:56'); $updatedTime = new \DateTime('2013-02-03 23:45:01'); $status = BatchStatus::STOPPED; $exitStatus = ExitStatus::FAILED; $jobInstance = new JobInstance('test_connector', JobInstance::TYPE_IMPORT, 'test_job_instance'); $jobInstance->setCode('job instance code'); $endTime = new \DateTime('2013-03-04 21:43:05'); $this->jobExecution->setStartTime($startTime); $this->jobExecution->setUpdatedTime($updatedTime); $this->jobExecution->setStatus(new BatchStatus($status)); $this->jobExecution->setExitStatus(new ExitStatus($exitStatus, 'Test description')); $this->jobExecution->setJobInstance($jobInstance); $this->jobExecution->setEndTime($endTime); $timezone = $startTime->format('P'); $expectedOutput = 'startTime=2013-02-01T12:34:56' . $timezone . ', endTime=2013-03-04T21:43:05' . $timezone . ', ' . 'updatedTime=2013-02-03T23:45:01' . $timezone . ', status=5, exitStatus=[FAILED] Test description, ' . 'exitDescription=[Test description], job=[job instance code]'; $this->assertEquals($expectedOutput, (string) $this->jobExecution); }
/** * Get normalized configuration * * @param JobInstance $job * * @return mixed */ protected function normalizeConfiguration(JobInstance $job) { return $job->getRawConfiguration(); }
/** * {@inheritdoc} */ protected function normalizeConfiguration(JobInstance $job) { $configuration = json_encode($job->getRawConfiguration()); return $configuration; }
/** * Configure job instance for uploaded file * * @param JobInstance $jobInstance * @param File $file * * @return bool */ protected function configureUploadJob(JobInstance $jobInstance, File $file) { $success = false; $job = $jobInstance->getJob(); foreach ($job->getSteps() as $step) { if (method_exists($step, 'getReader')) { $reader = $step->getReader(); if ($reader instanceof UploadedFileAwareInterface) { $constraints = $reader->getUploadedFileConstraints(); $this->fileError = $this->getValidator()->validateValue($file, $constraints); if ($this->fileError->count() !== 0) { foreach ($this->fileError as $error) { $this->addFlash('error', $error->getMessage()); } return false; } else { $reader->setUploadedFile($file); $success = true; } } } } return $success; }
/** * @param JobInstance $job * * @Given /^I should be on the ("([^"]*)" (import|export) job) page$/ */ public function iShouldBeOnTheJobPage(JobInstance $job) { $jobPage = sprintf('%s show', ucfirst($job->getType())); $expectedAddress = $this->getPage($jobPage)->getUrl(array('id' => $job->getId())); $this->assertAddress($expectedAddress); }
/** * Create and persist job instance. * * @param string $jobType * @param string $jobName * @param array $configuration * @return JobInstance */ protected function createJobInstance($jobType, $jobName, array $configuration) { $jobInstance = new JobInstance(self::CONNECTOR_NAME, $jobType, $jobName); $jobInstance->setCode($this->generateJobCode($jobName)); $jobInstance->setLabel(sprintf('%s.%s', $jobType, $jobName)); if (array_key_exists(self::JOB_CONTEXT_DATA_KEY, $configuration)) { unset($configuration[self::JOB_CONTEXT_DATA_KEY]); } $jobInstance->setRawConfiguration($configuration); $this->batchJobRepository->getJobManager()->persist($jobInstance); return $jobInstance; }
/** * @param TableNode $table * * @Given /^the following jobs?:$/ */ public function theFollowingJobs(TableNode $table) { $registry = $this->getContainer()->get('akeneo_batch.connectors'); foreach ($table->getHash() as $data) { $jobInstance = new JobInstance($data['connector'], $data['type'], $data['alias']); $jobInstance->setCode($data['code']); $jobInstance->setLabel($data['label']); $job = $registry->getJob($jobInstance); $jobInstance->setJob($job); $this->validate($jobInstance); $this->persist($jobInstance); } }
/** * {@inheritDoc} */ public function setConnector($connector) { $this->__initializer__ && $this->__initializer__->__invoke($this, 'setConnector', array($connector)); return parent::setConnector($connector); }