/**
  * @param OutputInterface $output
  * @param array           $feeds
  * @param integer         $minutes
  * @param boolean         $force
  *
  * @return integer
  */
 protected function scheduleImports(OutputInterface $output, array $feeds, $minutes, $force = false)
 {
     if (empty($feeds)) {
         return 0;
     }
     $repo = $this->getFeedRepository();
     $manager = $this->importManager;
     $num = 0;
     $factor = $minutes / sizeof($feeds);
     foreach ($feeds as $id => $priority) {
         /** @var Feed $feed */
         $feed = $repo->find($id);
         $offset = round($factor * $num++);
         $date = new \DateTime(sprintf('+%d minutes', $offset));
         $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')));
         // schedule all resources
         try {
             $dispatcher = $manager->getEventDispatcher();
             $import = $manager->createImport($feed, $date, $force);
             $reader = $manager->createImportReader($import, $dispatcher, $manager->getReaderOptions($import));
         } catch (\RuntimeException $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;
         }
         $output->writeln(sprintf('Created import <info>%d</info>, adding parts...', $import->getId()));
         try {
             while ($reader->getResources()->count()) {
                 $resource = $reader->getResources()->dequeue();
                 $transportConfig = TransportFactory::createConfigFromTransport($resource->getTransport());
                 $manager->createImportPart($import, $transportConfig, $date);
                 $output->writeln(sprintf('=> <comment>%s</comment> (<comment>%d</comment> resources left)', (string) $resource->getTransport(), $reader->getResources()->count()));
             }
         } catch (FeedException $e) {
             $output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
         }
         if ($import->getParts()->count() === 0) {
             $output->writeln('No parts created, finishing import now');
             $manager->startImport($import);
             $manager->finishImport($import);
             continue;
         }
         $output->write('Scheduling jobs for all parts...');
         foreach ($import->getParts() as $part) {
             $delay = $date->getTimestamp() - time();
             if ($delay < 0) {
                 $delay = 0;
             }
             $this->queueManager->addForObject(ImportPartExecutor::NAME, $part, $delay);
         }
         $output->writeln(' <info>done!</info>');
     }
     return 0;
 }
 public function testAddForObjectWithArguments()
 {
     $executor = new ObjectExecutor();
     $action = $executor->getName();
     $this->manager->addExecutor($executor);
     $priority = 10;
     $delay = 10;
     $ttr = 1200;
     $this->pheanstalk->expects($this->once())->method('putInTube')->with($action, json_encode(['test']), $priority, $delay, $ttr)->will($this->returnValue(1234));
     $this->assertEquals(1234, $this->manager->addForObject($action, new \ArrayObject(), $delay, $priority, $ttr));
 }
 /**
  * @inheritdoc
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $action = $input->getArgument('action');
     $payload = $input->getArgument('payload');
     $priority = $input->getOption('priority');
     $delay = $input->getOption('delay');
     $ttr = $input->getOption('ttr');
     $dql = $input->getOption('dql');
     if ($payload && $dql) {
         throw new \InvalidArgumentException('You cannot provide both a <comment>payload</comment> and a <comment>--dql</comment> query.');
     }
     if (!empty($payload)) {
         $job = $this->manager->add($action, $payload, $delay, $priority, $ttr);
         $output->writeln(sprintf('Scheduled job <info>%d</info> with payload <info>%s</info>', $job, json_encode($payload)));
         return 0;
     }
     if (empty($dql)) {
         throw new \InvalidArgumentException('You must provide either a <comment>payload</comment> or a <comment>--dql</comment> query.');
     }
     if (!$this->doctrine) {
         $output->writeln('<error>Doctrine is required for the --dql option</error>');
         return 1;
     }
     $doctrine = $this->doctrine->getManager();
     if (!$doctrine instanceof EntityManagerInterface) {
         $output->writeln('<error>Sorry, only Doctrine\'s ORM is supported at this point. You\'re welcome to submit a PR of course!</error>');
         return 1;
     }
     $meta = null;
     $query = $doctrine->createQuery($dql);
     foreach ($query->iterate() as list($entity)) {
         if (!$meta) {
             $meta = $doctrine->getClassMetadata(get_class($entity));
         }
         $job = $this->manager->addForObject($action, $entity, $delay, $priority, $ttr);
         $output->writeln(sprintf('Scheduled job <info>%d</info> for entity <info>%s: %s</info>', $job, json_encode($meta->getIdentifierValues($entity)), $this->entityToString($entity)));
         $doctrine->clear();
     }
     return 0;
 }
 /**
  * @param SourceEvent $event
  */
 public function onSourceProcess(SourceEvent $event)
 {
     $this->queueManager->addForObject(SourceProcessExecutor::NAME, $event->getSource());
 }
 /**
  * @param PartEvent $event
  */
 public function onPartScheduled(PartEvent $event)
 {
     $this->queueManager->addForObject(ImportPartExecutor::NAME, $event->getPart(), null, null, $this->timeToRun);
 }
 /**
  * Schedules a source process job
  *
  * @param SourceInterface $source
  */
 protected function scheduleSourceProcess(SourceInterface $source)
 {
     $this->queueManager->addForObject(SourceProcessExecutor::NAME, $source);
 }
 /**
  * @param SourceEvent $event
  */
 public function onScrapeRevisitSource(SourceEvent $event)
 {
     $this->queueManager->addForObject(ScrapeRevisitSourceExecutor::NAME, $event->getSource());
 }