题
在管理面板中,当管理员选择一个动作,将负载从API数据,并将其保存到DB。这可能需要2秒到3分钟,这取决于他是什么进口
我如何能在Symfony的做创建像一个线程,因此管理员没有等来完成。而当它的完成告诉他检查结果?
是任务这个问题的答案?
谢谢!
解决方案
不symfony的,但你要揭开序幕,一些后台进程。
我不熟悉用symfony的工装命令行脚本,但我觉得有东西,你可以使用。
然后在你的控制器,你想要的东西大致如下(假设你是一个unixy主机上):
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过去了,抓住作业记录,并使用任何存储的投入运行作业。当它完成抓取数据,并将其压入到数据库中,它应该做的设置在表的结束时间(这标志着作业作为完成),然后做任何你想做的事情通知用户(发送电子邮件,或插入一些种行的成消息表,等等)
不隶属于 StackOverflow