我是 Symfony2 的新手,在尝试像这样运行异步命令时被阻止:
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));
}}
当我从命令行调用它时效果很好:
php app/console my:command uk
但是当我将它称为 Symfony2 进程时它不起作用:
$process = new Process("php ../app/console my:command $country");
$process->start();
我收到一个数据库错误:“[MongoWriteConcernException] 127.0.0.1:27017: not master”
我认为这意味着该进程没有获取我的数据库配置...
我只想运行一个异步进程,还有其他方法吗?
也许是一种调用应用程序命令的方式,不需要答案就可以继续运行?
也许我需要使用注入(inject)?
PS:我当前的命令只是一个测试,最后它应该是一个“昂贵”的操作......
最佳答案
嗯,我知道发生了什么……
我使用多种环境:DEV、TEST 和 PROD。
而且我还使用不同的服务器。
所以DEV环境是我自己的机器,配置了简单的mongodb。 但是 TEST 环境在具有 副本集 配置的其他服务器上...
现在错误得到充分理解:"[MongoWriteConcernException] 127.0.0.1:27017: not master"
为了解决这个问题,我刚刚在进程中添加了环境参数 (--env=),一切都很顺利:
$process = new Process("php ../app/console my:command $country --env=test");
实际上,为了获得正确的环境,我使用了这个:
$this->get('kernel')->getEnvironment();
让我的代码如下:
$process = new Process("php ../app/console my:command $country --env=".$this->get('kernel')->getEnvironment());
也许不是一个漂亮的方法,但它对我有用:)
关于php - 异步子进程中的 Symfony2 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23888047/