/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $interval = $input->getOption('interval'); $batchJobs = $this->getObsoleteBatchJobsQueryBuilder($this->prepareDateInterval($interval)); $batchJobsIterator = new DeletionQueryResultIterator($batchJobs); $batchJobsIterator->setBufferSize(self::FLUSH_BATCH_SIZE); $batchJobsIterator->setHydrationMode(AbstractQuery::HYDRATE_SCALAR); if (!count($batchJobsIterator)) { $output->writeln('<info>There are no jobs eligible for clean up</info>'); return; } $output->writeln(sprintf('<comment>Batch jobs will be deleted:</comment> %d', count($batchJobsIterator))); $this->deleteRecords($batchJobsIterator, 'AkeneoBatchBundle:JobExecution'); $jobExecutions = $this->getObsoleteJobInstancesQueryBuilder(); $jobInstanceIterator = new DeletionQueryResultIterator($jobExecutions); $jobInstanceIterator->setBufferSize(self::FLUSH_BATCH_SIZE); $jobInstanceIterator->setHydrationMode(AbstractQuery::HYDRATE_SCALAR); $this->deleteRecords($jobInstanceIterator, 'AkeneoBatchBundle:JobInstance'); $output->writeln('<info>Batch job history cleanup complete</info>'); }
/** * @param string $entityClass * @param array $excludedWorkflowNames * @param int|null $batchSize * @throws \Exception */ public function resetWorkflowData($entityClass, $excludedWorkflowNames = array(), $batchSize = null) { $entityManager = $this->getEntityManager(); $batchSize = $batchSize ?: self::DELETE_BATCH_SIZE; // select entities for reset $queryBuilder = $this->getEntityManager()->createQueryBuilder(); $queryBuilder->select('workflowItem.id')->from($entityClass, 'entity')->innerJoin('entity.workflowItem', 'workflowItem')->innerJoin('workflowItem.definition', 'workflowDefinition')->orderBy('workflowItem.id'); if ($excludedWorkflowNames) { $queryBuilder->andWhere($queryBuilder->expr()->notIn('workflowDefinition.name', $excludedWorkflowNames)); } $iterator = new DeletionQueryResultIterator($queryBuilder); $iterator->setBufferSize($batchSize); if ($iterator->count() == 0) { return; } // wrap all operation into transaction $entityManager->beginTransaction(); try { // iterate over workflow items $workflowItemIds = array(); foreach ($iterator as $workflowItem) { $workflowItemIds[] = $workflowItem['id']; if (count($workflowItemIds) == $batchSize) { $this->clearWorkflowItems($entityClass, $workflowItemIds); $workflowItemIds = array(); } } if ($workflowItemIds) { $this->clearWorkflowItems($entityClass, $workflowItemIds); } $entityManager->commit(); } catch (\Exception $e) { $entityManager->rollback(); throw $e; } }