如何在Lumen 6中将链接的作业分配到队列中? [英] How to dispatch chained jobs to a queue in Lumen 6?
问题描述
在Laravel中,可以指定在成功执行主作业后应按顺序运行的排队作业的列表.如果序列中的一个作业失败,则其余的作业将不会运行.Laravel文档显示,这是通过在可调度作业上使用withChain方法来完成的,如以下示例所示:
In Laravel, it is possible to specify a list of queued jobs that should be run in sequence after the primary job has executed successfully. If one job in the sequence fails, the rest of the jobs will not be run. The Laravel documentation shows that this is accomplished by using the withChain method on a dispatchable job, as in this example:
ProcessPodcast::withChain([
new OptimizePodcast,
new ReleasePodcast
])->dispatch();
这在Laravel中对我来说效果很好,但是我正在使用Lumen(Laravel的轻量级子集).
根据流明关于队列的文档,就像框架的许多其他部分一样,Lumen的排队作业的功能与Laravel的排队作业相同.因此,要了解有关Lumen中排队作业的更多信息,请查看完整的Laravel排队文档."
This works well for me in Laravel, however I am using Lumen (a lightweight subset of Laravel).
According to Lumen's documentation on queues, "Like many other parts of the framework, Lumen's queued jobs function identically to Laravel's queued jobs. So, to learn more about queuing jobs in Lumen, please review the full Laravel queue documentation."
Lumen文档确实提到了与Laravel的一些细微差别,包括在将工作分配到队列的方式上的差别.它说明了可以使用分派功能或Queue门面来分派Lumen中的作业:
The Lumen docs do mention some minor differences with Laravel, including a difference in the way that jobs are dispatched to a queue. It explains that jobs in Lumen can be dispatched using either the dispatch function or the Queue facade:
dispatch(new ExampleJob);
Queue::push(new ExampleJob);
以此为背景,有没有办法在流明市派遣连锁工作?我已经搜寻Google数天了,与我的问题最接近的匹配项是这两个链接:
With that as background, is there any way to dispatch chained jobs in Lumen? I have scoured Google for days, and the closest matches to my issue are these two links:
- 此Stack Overflow帖子,它准确地描述了我的问题,但未提供任何解决方案
- 此Laracast主题,表明
$ this-> dispatch((new FillBruteFec($ import))-> chain(new FillRaiFec()));
的语法对我也不起作用.
- This Stack Overflow post, which exactly describes my issue but does not offer any solutions
- This Laracast thread, which suggests a syntax of
$this->dispatch( (new FillBruteFec($import))->chain(new FillRaiFec()) );
that also doesn't work for me.
上面的Stack Overflow链接说明,Laravel语法不起作用的原因是Lumen缺少 Illuminate \ Foundation \ Bus \ Dispatchable
特性.
The Stack Overflow link above explains that the reason the Laravel syntax won't work is that Lumen is missing the Illuminate\Foundation\Bus\Dispatchable
trait.
Further complicating matters is that I need to pass a different set of parameters to each of my jobs - something that is apparently challenging to do even in the full Laravel framework.
这是我目前在我的Lumen应用程序中提交我的工作的方式(不链接):
Here is how I am currently submitting my jobs in my Lumen app (without chaining):
Queue::push(new CreateUser($username,$password));
Queue::push(new SetForwarding($username,$forwardTo));
Queue::push(new EnableIncomingEmail($username));
Queue::push(new EnableOutgoingEmail($username));
Queue::push(new EnableImap($username));
我想要链接这些文件的主要原因是要确保在修改该用户的任何后续作业之前成功创建了用户记录.
The main reason I want to chain these is to make sure the user record gets created successfully before any of the subsequent jobs that modify that user.
那么,考虑到所有这些……怎么样?在Lumen连锁工作是否有可能?如果是这样,我将使用什么语法?
So, with all of that in mind... how about it? Is it at all possible to chain jobs in Lumen? If so, what syntax would I use?
推荐答案
illuminate/bus/Queueable
特性,用于链方法,应使用一系列Job实例进行调用,以构成一个工作链
illuminate/bus/Queueable
trait which is used in App\Jobs\Job class provides a chain method that should be invoked with an array of Job instances to make a job chain.
您可以通过编写以下命令来运行一系列作业:
You can run a chain of jobs by writing:
$this->dispatch(
(new CreateUser($username,$password))
->chain([
new SetForwarding($username,$forwardTo),
new EnableIncomingEmail($username),
new EnableOutgoingEmail($username),
new EnableImap($username)
])
);
这篇关于如何在Lumen 6中将链接的作业分配到队列中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!