简体   繁体   English

Laravel 邮件应该队列与作业应该队列

[英]Laravel Mail ShouldQueue vs Job ShouldQueue

Okay, so I get the idea of ShouldQueue and that it lets the application run more smooth.好的,所以我得到了 ShouldQueue 的想法,它可以让应用程序运行得更流畅。

But what I don't get, is the difference between using a Mailable with ShouldQueue vs using a Job with ShouldQueue.但我不明白的是,使用带有ShouldQueue 的Mailable 与使用带有ShouldQueue 的Job 之间的区别。

If I use Mailable with ShouldQueue and send the mail as I normally would, the mail is queued and should then be send by a worker.如果我使用带有 ShouldQueue 的 Mailable 并像往常一样发送邮件,邮件会排队,然后应该由工作人员发送。

If I use Job with ShouldQueue it does the same thing but with 1 more class (It feels a bit redundant).如果我将 Job 与 ShouldQueue 一起使用,它会做同样的事情,但会增加 1 个类(感觉有点多余)。 So in my use case it looks like using Job is a waste of time?所以在我的用例中,使用 Job 似乎是在浪费时间?

Can anyone please tell me what benefits I would get with using Job over Mailable?谁能告诉我使用 Job over Mailable 有什么好处? :) :)

When you dispatch a Job with the ShouldQueue contract it will be added to a queue (if not specified the default queue).当您使用ShouldQueue合同调度 Job 时,它将被添加到队列中(如果未指定default队列)。 During this process execution, when you create a Mailable with the same contract, it will be enqueued to the same queue or to another.在这个流程执行过程中,当你创建一个具有相同合约的Mailable时,它会被排入同一个队列或另一个队列。

As in most of these cases, the answer to your question is it depends .在大多数情况下,您的问题的答案是它取决于

For example:例如:

  • If you have to dispatch a job with a specific logic that requires a lot of time to be executed and, when this task is over, you have to notify tons of users, than the answer is enqueue both the job and the Mailable and in 2 different queues.如果您必须使用需要大量时间执行的特定逻辑来调度作业,并且当此任务结束时,您必须通知大量用户,那么答案是将作业和 Mailable 放入队列,然后在 2不同的队列。
  • If you have to notify just a user, enque the job and send the Mailable directly.如果您只需要通知用户,请对作业进行排队并直接发送 Mailable。
  • If you have to notify a user for a password reset and you have just a few requests for a password reset, send the Mailable directly.如果您必须通知用户重置密码并且您只有几个密码重置请求,请直接发送 Mailable。
  • ..... .....

The benefits, IMHO depend on what you have to do.恕我直言,好处取决于您必须做什么。 Usually, more tasks you have to complete, more queues are necessary.通常,您必须完成的任务越多,就需要更多的队列。 Anyway it's always better to use the queues in order to return a simple, fast response to the User and handle the rest separately.无论如何,最好使用队列来向用户返回简单、快速的响应并单独处理其余部分。

For example, if a user registration succeed, but the mailable throws an error, it will create a record on the failed_job_table without telling the User that his / her registration failed.例如,如果用户注册成功,但是mailable 抛出错误,它会在failed_job_table上创建一个记录,而不告诉用户他/她的注册失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM