微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php-使用Symfony Process组件进行Ajax轮询

我正在启动一个长期运行的任务,该任务返回有关Symfony Process组件任务进度的增量输出.

一个示例显示了如何获取实时输出,另一个示例显示了如何运行异步任务.

我要实现的目标是将getIncrementalOutput的结果传递回ajax轮询功能,以便我可以实时更新前端.

在这两种情况下,似乎进程-> start()都被阻塞,因为我的ajax调用需要一分钟才能返回,并且到那时该任务已完成.

我想我想避免将进度写入数据库文件,而直接从运行的PHP任务获取输出.

不确定是否有可能.

解决方法:

尽管我不完全了解您要创建的内容,但是我已经写了类似的内容,看着它可能会回答您的问题:

首先,我创建了一个执行长期任务的命令:

class GenerateCardBarcodesCommand extends Command 
{
    protected function configure()
    {
        $this
            ->setName('app:generate-card-barcodes')
            ->setDescription('Generate the customer cards with barcodes')
            ->addArgument('id', InputArgument::required, 'id of the Loy/Card entity')
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $id = $input->getArgument('id');
        // generate stuff and put them in the database
    }
}

在控制器中,该过程已启动,并且有一个ajax操作

class CardController extends Controller
{
    public function newAction(Request $request)
    {
                // run command in background to start generating barcodes
                // NOTE: unset DYLD_LIBRARY_PATH is a fix for MacOSX develop using MAMP.
                // @see https://stackoverflow.com/questions/19008960/phantomjs-on-mac-os-x-works-from-the-command-line-not-via-exec
                $process = new Process(sprintf('unset DYLD_LIBRARY_PATH ; PHP ../../apps/spin/console spin:loy:generate-card-barcodes %d', $entity->getId()));
                $process->start();

                sleep(1); // wait for process to start

                // check for errors and output them through flashbag
                if (!$process->isRunning())
                    if (!$process->isSuccessful())
                        $this->get('session')->getFlashBag()->add('error', "Oops! The process fininished with an error:".$process->getErrorOutput());

                // otherwise assume the process is still running. It's progress will be displayed on the card index
                return $this->redirect($this->generateUrl('loy_card'));
     }

    public function ajaxCreateBarcodesAction($id)
    {
        $em = $this->getDoctrine()->getManager();
        $entity = $this->getEntity($id);
        $count = (int)$em->getRepository('ExtendasspinBundle:Loy\CustomerCard')->getCount($entity);
        return new Response(floor($count / ($entity->getNoCards() / 100)));
    }
}

//在树枝模板中检索ajax,它只是一个介于0到100之间的数字,用于jquery ui进度栏中.
    {{‘正在处理’| trans}} …

            <script type="text/javascript">
            $(function() {
                function pollLatestResponse() {
                    $.get("{{ path('loy_card_ajax_generate_barcodes', {'id': entity[0].id}) }}").done(function (perc) {
                        if (perc == 100)
                        {
                            clearInterval(pollTimer);
                            $('#download-{{entity[0].id}}').show();
                            $('#progress-{{entity[0].id}}').hide();
                        }
                        else
                        {
                            $('#progress-{{entity[0].id}}').progressbar("value", parseInt(perc));
                        }
                    });
                }

                var pollTimer;
                $(document).ready(function () {
                    $('#progress-{{entity[0].id}}').progressbar({"value": false});
                    pollTimer = setInterval(pollLatestResponse, 2000);
                });
            });
            </script>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐