Created
June 9, 2011 16:00
-
-
Save jmikola/1017050 to your computer and use it in GitHub Desktop.
Catching SIGINT to cleanup from a MongoDB mapReduce command
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace OpenSky\Bundle\MainBundle\Command; | |
| use Symfony\Bundle\FrameworkBundle\Command\Command; | |
| use Symfony\Component\Console\Input\InputInterface; | |
| use Symfony\Component\Console\Input\InputOption; | |
| use Symfony\Component\Console\Output\OutputInterface; | |
| class FooCommand extends Command | |
| { | |
| protected function configure() | |
| { | |
| $this | |
| ->setName('foo') | |
| ->addOption('timeout', 't', InputOption::VALUE_REQUIRED, 'MongoCursor timeout', 0) | |
| ; | |
| } | |
| protected function execute(InputInterface $input, OutputInterface $output) | |
| { | |
| $tmp = uniqid('foo.'); | |
| $output->writeln(sprintf('Using <info>%s</info> as the temporary collection.', $tmp)); | |
| \MongoCursor::$timeout = $input->getOption('timeout'); | |
| $dm = $this->container->get('doctrine.odm.mongodb.document_manager'); | |
| $db = $dm->getDocumentDatabase('OpenSky\Bundle\MainBundle\Document\Foo'); | |
| $cleanup = function() use ($db, $tmp, $output) { | |
| $db->$tmp->drop(); | |
| $output->writeln(sprintf('Dropped temporary collection <info>%s</info>.', $tmp)); | |
| exit; | |
| }; | |
| declare(ticks = 1) { | |
| pcntl_signal(\SIGINT, $cleanup); | |
| $db->command(array( | |
| 'mapreduce' => 'seller_followers', | |
| 'map' => new \MongoCode('...'), | |
| 'reduce' => new \MongoCode('...'), | |
| 'out' => $tmp, | |
| )); | |
| } | |
| call_user_func($cleanup); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment