[英]laravel - Execute Job After Multiple Jobs End
I am working on a project where user gives me some input, and then I process those inputs.我正在做一个项目,用户给我一些输入,然后我处理这些输入。 This processing takes long time, so I am using queues, and I want to send mail to the user that their processed data is ready on my app.
此处理需要很长时间,因此我使用队列,并且我想向用户发送邮件,告知他们处理的数据已在我的应用程序上准备就绪。 What I want to achieve is, put all these jobs into the queue, then process them, and when a user's all processing ends, to mail that user.
我想要实现的是,将所有这些作业放入队列中,然后处理它们,当用户的所有处理结束时,向该用户发送邮件。
I am using 2 phase queue.我正在使用两阶段队列。 First, I get the input and dispatch a job where the input gets parsed and divided into sub-datasets, and dispatch another job from that job which processes the parsed input data from the first job.
首先,我获取输入并调度一个作业,其中输入被解析并划分为子数据集,然后从该作业调度另一个作业,该作业处理来自第一个作业的解析输入数据。 Why I did this is, the first job takes less time than the second job, and I want to use jobs to only do the processing, without any interruption or extra work with parsing, the first phase is all about parsing and the second part is all about processing.
我这样做的原因是,第一个工作比第二个工作花费的时间少,我想用工作只做处理,没有任何中断或额外的解析工作,第一阶段是关于解析的,第二部分是所有关于处理。 The processing part may fail and the queue will process it again if it fails, so if I do not divide the input into the sub-jobs, a fail near the end of the job may cause all the processing for the job to be done again.
处理部分可能会失败,如果失败,队列会再次处理它,所以如果我不将输入分成子作业,作业接近尾声的失败可能会导致作业的所有处理再次完成.
If I put all those jobs to the same queue, and assign that mailing job to the Queue::after
method, a user may have to wait for all the queue to finish, if I understood correctly.如果我将所有这些作业放入同一个队列,并将该邮件作业分配给
Queue::after
方法,如果我理解正确,用户可能必须等待所有队列完成。 I want to be able to send a mail to the user when all of his inputs have been processed, without waiting any other user's inputs to be processed.我希望能够在处理完用户的所有输入后向用户发送邮件,而无需等待处理任何其他用户的输入。 The documents for Laravel Queue shows an example method:
Laravel Queue 的文档显示了一个示例方法:
Queue::after(function (JobProcessed $event) {
// $event->connectionName
// $event->job
// $event->job->payload()
});
And what I understood from this is this works after every processed job, and I can access the job properties with the $event
object.我从中了解到的是,这在每个处理完的作业之后都有效,并且我可以使用
$event
对象访问作业属性。 This is not going to work with my 2-phase queue structure.这不适用于我的两阶段队列结构。 I know I can use some counters in database to check if all the processing is done, but I want to know if there is a better and more elegant way of achieving this.
我知道我可以使用数据库中的一些计数器来检查所有处理是否已完成,但我想知道是否有更好、更优雅的方法来实现这一点。
Thank you very much.非常感谢。
It has been a few years now, but Laravel 8 may provide a solution to this:几年过去了,但 Laravel 8 可能会为此提供一个解决方案:
https://github.com/laravel/framework/pull/32830 https://github.com/laravel/framework/pull/32830
Jobs can be run in a batch, with callbacks that are invoked when all jobs complete, any jobs fail, or all jobs complete successfuly.作业可以批量运行,并在所有作业完成、任何作业失败或所有作业成功完成时调用回调。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.