使用Symfony Process组件进行Ajax轮询


Ajax polling with Symfony Process component

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

其中一个示例展示了如何获得实时输出,另一个示例则展示了如何运行异步任务。

我试图实现的是将getIncrementalOutput的结果传递回ajax轮询函数,这样我就可以实时更新前端。

在这两种情况下,process->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 http://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)));
    }
}

//在trick模板中检索ajax,它只是一个从0到100的数字,在jqueryui progressbar中使用。{{"正在处理"| 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>