/**
  * @param ImportPart[]    $parts
  * @param OutputInterface $output
  */
 protected function scheduleParts(array $parts, OutputInterface $output)
 {
     foreach ($parts as $part) {
         if ($this->processor->isRunning($part)) {
             continue;
         }
         $this->scheduler->schedulePart($part);
         $output->writeln(sprintf('Rescheduled part <comment>%d</comment> of <comment>%s</comment> import with id <comment>%s</comment>', $part->getPosition(), $part->getImport()->getFeed()->getOrigin()->getTitle(), $part->getImport()->getId()));
     }
 }
 /**
  * @param InputInterface  $input
  * @param OutputInterface $output
  * @param Feed[]          $feeds
  * @param int             $minutes
  * @param bool            $force
  *
  * @return int
  */
 protected function scheduleImports(InputInterface $input, OutputInterface $output, array $feeds, $minutes, $force = false)
 {
     if (empty($feeds)) {
         $output->writeln('No feeds to schedule');
         return 0;
     }
     $num = 0;
     $factor = $minutes / sizeof($feeds);
     foreach ($feeds as $feedId => $priority) {
         $offset = round($factor * $num++);
         $date = new \DateTime(sprintf('+%d minutes', $offset));
         /** @var Feed $feed */
         $feed = $this->importScheduler->findFeed($feedId);
         if ($input->isInteractive()) {
             $this->checkForUnfinishedImports($feed, $input, $output);
         }
         $output->writeln(sprintf('Scheduling import for <info>%s</info> feed <info>%d</info> to run at <info>%s</info>', $feed->getOrigin()->getName(), $feed->getId(), $date->format('Y-m-d H:i:s')));
         try {
             $import = $this->importFactory->createImport($feed, $date, $force);
             $output->writeln(sprintf('Created import <info>%d</info>', $import->getId()));
             $output->writeln('Scheduling parts');
             foreach ($import->getParts() as $part) {
                 $this->importScheduler->schedulePart($part);
             }
         } catch (\Exception $e) {
             // we could get an exception when a new import cannot be created, for example when an existing import
             // for this feed is still running.
             $output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
             continue;
         }
     }
     return 0;
 }
 /**
  * @inheritdoc
  */
 public function execute(array $payload)
 {
     /** @var Feed $feed */
     /** @var bool $force */
     list($feed, $force) = $payload;
     try {
         $import = $this->importFactory->createImport($feed, new \DateTime(), $force);
         $this->logger->info(sprintf('Created import %d', $import->getId()));
         $this->logger->debug('Scheduling parts');
         foreach ($import->getParts() as $part) {
             $this->scheduler->schedulePart($part);
         }
         return true;
     } catch (\Exception $e) {
         // we could get an exception when a new import cannot be created, for example when an existing import
         // for this feed is still running.
         $this->logger->error(sprintf('<error>%s</error>', $e->getMessage()));
         return false;
     }
 }