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配置. 现在错误得到充分意义:“[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()); 也许这不是一个美丽的方式,但它适用于我:) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |