/** * @inheritdoc */ public function configurePayload(OptionsResolver $resolver) { $resolver->setRequired(0); $resolver->setAllowedTypes(0, 'numeric'); $resolver->setNormalizer(0, function (Options $options, $value) { if (null === ($listing = $this->scheduler->findFeed($value))) { throw new InvalidArgumentException(sprintf('Feed with id "%d" does not exist', $value)); } return $listing; }); $resolver->setDefaults([1 => false]); $resolver->setNormalizer(1, function (Options $options, $value) { return (bool) $value; }); }
/** * @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; }