Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 command within an asynchronous subprocess

I am new on Symfony2 and I got blocked when trying to run an asynchronous command like this:

class MyCommand extends ContainerAwareCommand{

protected function configure()
{
    $this
        ->setName('my:command')
        ->setDescription('My command')
        ->addArgument(
            'country',
            InputArgument::REQUIRED,
            'Which country?'
        )
    ;
}

protected function execute(InputInterface $input, OutputInterface $output)
{

    $country = $input->getArgument('country');


    // Obtain the doctrine manager
    $dm = $this->getContainer()->get('doctrine_mongodb.odm.document_manager');

   $users = $dm->getRepository('MyBundle:User')
        ->findBy( array('country'=>$country));
}}

That works perfectly when I call it from my command line:

php app/console my:command uk

But it doesn't work when I call it trowh a Symfony2 Process:

 $process = new Process("php ../app/console my:command $country");
 $process->start();

I get a database error: "[MongoWriteConcernException] 127.0.0.1:27017: not master"

I think that means that the process is not getting my database configuration...

I just want to run an asynchronous process, is there other way to do it?

Maybe a way to call the Application Command that do not require the answer to keep going ?

Maybe I need to use injection?

PS: My current command is just a test, at the end it should be an 'expensive' operation...

like image 388
blueocean Avatar asked May 27 '14 11:05

blueocean


1 Answers

Well, I found out what happened...

I use multiple environments: DEV, TEST and PROD.

And I also use differents servers.

So the DEV environment is my own machine with a simple mongodb configuration. But the TEST environment is on other server with a replica set configuration...

Now the error get full sense: "[MongoWriteConcernException] 127.0.0.1:27017: not master"

To solve it, I've just added the environment parameter (--env=) to the process and everything worked like a charm:

$process = new Process("php ../app/console my:command $country --env=test");

Actually, to get the correct environment I use this:

$this->get('kernel')->getEnvironment();

Which let's my code as follows:

$process = new Process("php ../app/console my:command $country --env=".$this->get('kernel')->getEnvironment());

Maybe is not a beautifull way to do it, but it works for me :)

like image 90
blueocean Avatar answered Oct 19 '22 06:10

blueocean