Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Koc/938628 to your computer and use it in GitHub Desktop.
Save Koc/938628 to your computer and use it in GitHub Desktop.
This is a Symfony2 Doctrine MongoDB ODM Tail Cursor console command. It can be ran as a daemon and it will tail a mongodb collection with a tailable cursor and process the documents with a given service as the "processor". You must specify the document, t
<?php
namespace MyCompany\Bundle\MyBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\HttpFoundation\Request;
use ReflectionClass;
use Exception;
class TailCursorCommand extends Command
{
protected function configure()
{
$this
->setName('doctrine:mongodb:tail-cursor')
->setDescription('Tails a mongodb cursor and processes the documents that come through')
->addArgument('document', InputArgument::REQUIRED, 'The document we are going to tail the cursor for.')
->addArgument('finder', InputArgument::REQUIRED, 'The repository finder method which returns the cursor to tail.')
->addArgument('processor', InputArgument::REQUIRED, 'The service id to use to process the documents.')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$dm = $this->container->get('doctrine.odm.mongodb.document_manager');
$repository = $dm->getRepository($input->getArgument('document'));
$repositoryReflection = new ReflectionClass(get_class($repository));
$documentReflection = $repository->getDocumentManager()->getMetadataFactory()->getMetadataFor($input->getArgument('document'))->getReflectionClass();
$processor = $this->container->get($input->getArgument('processor'));
$processorReflection = new ReflectionClass(get_class($processor));
$method = $input->getArgument('finder');
$output->writeln(sprintf('Getting cursor for <info>%s</info> from <info>%s#%s</info>', $input->getArgument('document'), $repositoryReflection->getShortName(), $method));
$output->writeln(null);
$cursor = $repository->$method()
->tailable(true);
if (!count($cursor)) {
$output->writeln('<comment>Nothing found, waiting to try again</comment>');
}
foreach ($cursor as $document) {
$id = $document->getId();
$output->writeln(null);
$output->writeln(sprintf('Processing <info>%s</info> with id of <info>%s</info>', $documentReflection->getShortName(), (string) $id));
$output->writeln(null);
$output->writeln(sprintf(' <info>%s</info><comment>#</comment><info>process</info>(<info>%s</info> <comment>$document</comment>)', $processorReflection->getShortName(), $documentReflection->getShortName()));
$output->writeln(null);
try {
$processor->process($output, $document);
} catch (Exception $e) {
$output->writeln(sprintf('Error occurred while processing document: <error>%s</error>', $e->getMessage()));
continue;
}
$dm->flush(array('safe' => true));
$dm->clear();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment