php – 异步子进程中的Symfony2命令

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 异步子进程中的Symfony2命令脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我是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”

我认为这意味着该过程没有得到我的数据库配置…

我只是想运行一个异步进程,还有其他方法吗?

也许一种方法调用不需要答案的应用程序命令继续?

也许我需要使用注射?

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命令全部内容,希望文章能够帮你解决php – 异步子进程中的Symfony2命令所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。