Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

refactored

  • Loading branch information...
commit 29b6f4925663c3709d6f93c0b01f48ed88fac8d8 1 parent 655b460
Bilal Amarni authored May 20, 2012
71  Command/TailableCursorCommand.php
... ...
@@ -0,0 +1,71 @@
  1
+<?php
  2
+
  3
+namespace Doctrine\Bundle\MongoDBTailableCursorBundle\Command;
  4
+
  5
+use Doctrine\Bundle\MongoDBTailableCursorBundle\ProcessorInterface;
  6
+use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  7
+use Symfony\Component\Console\Input\InputArgument;
  8
+use Symfony\Component\Console\Input\InputInterface;
  9
+use Symfony\Component\Console\Output\OutputInterface;
  10
+
  11
+class TailableCursorCommand extends ContainerAwareCommand
  12
+{
  13
+    protected function configure()
  14
+    {
  15
+        $this
  16
+            ->setName('doctrine:mongodb:tail-cursor')
  17
+            ->setDescription('Tails a mongodb cursor and processes the documents that come through')
  18
+            ->addArgument('document', InputArgument::REQUIRED, 'The document we are going to tail the cursor for.')
  19
+            ->addArgument('finder', InputArgument::REQUIRED, 'The repository finder method which returns the cursor to tail.')
  20
+            ->addArgument('processor', InputArgument::REQUIRED, 'The service id to use to process the documents.')
  21
+        ;
  22
+    }
  23
+
  24
+    protected function execute(InputInterface $input, OutputInterface $output)
  25
+    {
  26
+        $dm = $this->getContainer()->get('doctrine.odm.mongodb.document_manager');
  27
+        $repository = $dm->getRepository($input->getArgument('document'));
  28
+        $repositoryReflection = new \ReflectionClass(get_class($repository));
  29
+        $documentReflection = $repository->getDocumentManager()->getMetadataFactory()->getMetadataFor($input->getArgument('document'))->getReflectionClass();
  30
+        $processor = $this->getContainer()->get($input->getArgument('processor'));
  31
+
  32
+        if (!$processor instanceof ProcessorInterface) {
  33
+            throw new \InvalidArgumentException('A tailable cursor processor must implement the ProcessorInterface.');
  34
+        }
  35
+
  36
+        $processorReflection = new \ReflectionClass(get_class($processor));
  37
+        $method = $input->getArgument('finder');
  38
+
  39
+        $output->writeln(sprintf('Getting cursor for <info>%s</info> from <info>%s#%s</info>', $input->getArgument('document'), $repositoryReflection->getShortName(), $method));
  40
+        $output->writeln(null);
  41
+
  42
+        $cursor = $repository->$method();
  43
+
  44
+        while (true) {
  45
+            while (!$cursor->hasNext()) {
  46
+                $output->writeln('<comment>Nothing found, waiting to try again</comment>');
  47
+                // read all results so far, wait for more
  48
+                sleep(10);
  49
+            }
  50
+            $cursor->next();
  51
+            $document = $cursor->current();
  52
+            $id = $document->getId();
  53
+
  54
+            $output->writeln(null);
  55
+            $output->writeln(sprintf('Processing <info>%s</info> with id of <info>%s</info>', $documentReflection->getShortName(), (string) $id));
  56
+            $output->writeln(null);
  57
+            $output->writeln(sprintf('   <info>%s</info><comment>#</comment><info>process</info>(<info>%s</info> <comment>$document</comment>)', $processorReflection->getShortName(), $documentReflection->getShortName()));
  58
+            $output->writeln(null);
  59
+
  60
+            try {
  61
+                $processor->process($document);
  62
+            } catch (\Exception $e) {
  63
+                $output->writeln(sprintf('Error occurred while processing document: <error>%s</error>', $e->getMessage()));
  64
+                continue;
  65
+            }
  66
+
  67
+            $dm->flush();
  68
+            $dm->clear();
  69
+        }
  70
+    }
  71
+}
9  Doctrine/ODM/MongoDB/Symfony/TailableCursorBundle/TailableCursorBundle.php
... ...
@@ -1,9 +0,0 @@
1  
-<?php
2  
-
3  
-namespace Doctrine\ODM\MongoDB\Symfony\TailableCursorBundle;
4  
-
5  
-use Symfony\Component\HttpKernel\Bundle\Bundle;
6  
-
7  
-class TailableCursorBundle extends Bundle
8  
-{
9  
-}
9  DoctrineMongoDBTailableCursorBundle.php
... ...
@@ -0,0 +1,9 @@
  1
+<?php
  2
+
  3
+namespace Doctrine\Bundle\MongoDBTailableCursorBundle;
  4
+
  5
+use Symfony\Component\HttpKernel\Bundle\Bundle;
  6
+
  7
+class DoctrineMongoDBTailableCursorBundle extends Bundle
  8
+{
  9
+}
4  ...mfony/TailableCursorBundle/ProcessorInterface.php → ProcessorInterface.php
... ...
@@ -1,8 +1,8 @@
1 1
 <?php
2 2
 
3  
-namespace Doctrine\ODM\MongoDB\Symfony\TailableCursorBundle;
  3
+namespace Doctrine\Bundle\MongoDBTailableCursorBundle;
4 4
 
5 5
 interface ProcessorInterface
6 6
 {
7 7
     function process($document);
8  
-}
  8
+}

0 notes on commit 29b6f49

Please sign in to comment.
Something went wrong with that request. Please try again.