管理中的长脚本,Symfony中的任务? [英] Long script in admin, task in Symfony?

查看:107
本文介绍了管理中的长脚本,Symfony中的任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在管理面板中,当管理员选择一个操作时,它将从API加载数据并将其保存到数据库.可能需要2秒钟到3分钟,具体取决于他要导入的内容.

In the admin panel, when the admin selects an action it will load data from an API and save it to the DB. It could take from 2 seconds to 3 minutes, depending on what he is importing.

我如何在Symfony中创建像线程一样的内容,因此管理员无需等待完成.然后告诉他检查结果?

How can I do in Symfony to create like a thread, so the admin is not waiting to finish. And when it's finished tell him to check the result?

任务是否可以解决此问题?

Are the tasks the answer for this problem?

谢谢!

推荐答案

不是特定于symfony的,但是您需要启动一些后台进程.

Not symfony-specific, but you'll want to kick off some background process.

我不熟悉symfony的命令行脚本工具,但我认为您可以使用一些东西.

I'm not familiar with symfony's tooling for command-line scripts, but I think there is stuff you can use.

然后在您的控制器中,您想要一个大致类似的东西(假设您在unixy主机上):

Then in your controller, you want something roughly like (assuming you're on a unixy host):

public function executeYourBackgroundTask(){

    // first, you might want to create some kind of entry in a table to keep track of jobs.
    // Imagine you've got a table to keep track of this stuff
    $job = new Backgroundjob();
    $job->user_id = $this->getUser()->getId();
    $job->starttime = time();
    $job->someArgument = $someArgument; //anything the job script needs for input.
    $job->save();


    $jobId = $job->getId();

    //start a job in the background.
    exec('php /path/to/your/background/script.php ' . $jobId .' &');

    //your view should just tell the user "Your job is being processed, you'll be notified when it is done"
}

您的后台进程(在/path/to/your/background/script.php中)应采用传递的jobId,获取作业记录,并使用任何存储的输入来运行作业.完成抓取数据并将其填充到数据库中后,它应该在表中设置一个结束时间(将作业标记为完成),然后执行您想做的任何事情来通知用户(发送电子邮件或插入一些内容)排成一条消息表等)

Your background process (in /path/to/your/background/script.php) should take the passed jobId, grab the job record, and use any stored inputs to run the job. When it's done grabbing data and stuffing it into the database, it should do set an endtime in the table (which marks the job as complete), and then do whatever you want to do to notify the user (send an email, or insert some kind of row into a messages table, etc)

这篇关于管理中的长脚本,Symfony中的任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆