laravel 如何使用队列设置高、低和中优先级电子邮件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42325247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:21:46  来源:igfitidea点击:

How to set high, low and medium priority email using queue?

laravellaravel-5.3laravel-5.4

提问by Pankaj

I am studying Job dispatching from here

我正在从这里学习工作派遣

Problem

问题

I read that each connection has queue parameter which tell the queue name. Question is: How can I set the priority to send low or medium or high priority email?

我读到每个连接都有队列参数,它告诉队列名称。问题是:如何设置发送低、中或高优先级电子邮件的优先级?

Cmd prompt

命令提示符

I am using command: php artisan queue:listento process job.

我正在使用命令:php artisan queue:listento process job。

What I tried?

我尝试了什么?

php artisan queue:work --queue=high,default

but this code never works if connection's queue parameter has value is not high

但是如果连接的队列参数的值不高,则此代码永远不会起作用

Default Queue Driver

默认队列驱动程序

'default' => env('QUEUE_DRIVER', 'database'),

Queue Connections

队列连接

'connections' => [

    'Register' => [
        'driver'        =>  'database',
        'table'         =>  'tbljobs',
        'queue'         =>  'low',
        'retry_after'   =>  5,
    ],

    'ForgotPassword' => [
        'driver'        =>  'database',
        'table'         =>  'tbljobs',
        'queue'         =>  'low',
        'retry_after'   =>  5,
    ],

],

.env

.env

QUEUE_DRIVER=Register

Controller Code for Register email

注册电子邮件的控制器代码

$job = (new SendActivationEmail($Data))
        ->onConnection('Register');
dispatch($job);

Controller Code for Reset Password

重置密码的控制器代码

$job = (new SendResetPasswordEmail($this->tokens->create($user), $user))
        ->onConnection('ForgotPassword');
dispatch($job);

采纳答案by Dwight

Take note of the Connections Vs. Queuesnote in Laravel's queue documentation, which applies to all queue drivers apart from SQS as far as I'm aware.

注意Connections Vs。Laravel 的队列文档中的队列注释,据我所知,它适用于除 SQS 之外的所有队列驱动程序。

Before getting started with Laravel queues, it is important to understand the distinction between "connections" and "queues". In your config/queue.php configuration file, there is a connections configuration option. This option defines a particular connection to a backend service such as Amazon SQS, Beanstalk, or Redis. However, any given queue connection may have multiple "queues" which may be thought of as different stacks or piles of queued jobs.

Note that each connection configuration example in the queue configuration file contains a queue attribute. This is the default queue that jobs will be dispatched to when they are sent to a given connection. In other words, if you dispatch a job without explicitly defining which queue it should be dispatched to, the job will be placed on the queue that is defined in the queue attribute of the connection configuration:

在开始使用 Laravel 队列之前,了解“连接”和“队列”之间的区别很重要。在您的 config/queue.php 配置文件中,有一个连接配置选项。此选项定义与后端服务(例如 Amazon SQS、Beanstalk 或 Redis)的特定连接。然而,任何给定的队列连接都可能有多个“队列”,这些“队列”可能被认为是不同的堆栈或排队作业的堆。

请注意,队列配置文件中的每个连接配置示例都包含一个队列属性。这是作业在发送到给定连接时将被分派到的默认队列。换句话说,如果您在没有明确定义应将其分派到哪个队列的情况下分派作业,则该作业将被放置在连接配置的 queue 属性中定义的队列中:

In effect you will register one queue connection in your config/queues.phpfile and the defaultparameter will simply be the queue that jobs are dispatched to by default, if another queue is not provided.

实际上,您将在config/queues.php文件中注册一个队列连接,并且default如果未提供另一个队列,则该参数将只是默认情况下将作业分派到的队列。

Vitaly's answer above would be the correct approach to the problem (consolidate to a single connection with a default queue) then adjust your jobs to get sent to different queues if required. This is some important (I think) context to how how queue configuration works.

上面 Vitaly 的回答将是解决问题的正确方法(合并为具有默认队列的单个连接),然后根据需要调整您的作业以发送到不同的队列。这是队列配置如何工作的一些重要(我认为)上下文。

回答by Vitaliy Ryaboy

'connections' => [

    'Register' => [ //<this name is connection name
        'driver'        =>  'database',
        'table'         =>  'tbljobs',
        'queue'         =>  'low',  //<this name is default queue name then you register a queue using this connection
        'retry_after'   =>  5,
    ],
],

I suggest you modify your code in the following way:

我建议您按以下方式修改代码:

'connections' => [
    'Register' => [
        'driver'        =>  'database',
        'table'         =>  'tbljobs',
        'queue'         =>  'default',
        'retry_after'   =>  5,
    ],
],

High priority job - Controller Code for Register email

高优先级工作 - 注册电子邮件的控制器代码

$job = (new SendActivationEmail($Data))
        ->onConnection('Register')
        ->onQueue("high");
dispatch($job);

Medium priority job - Controller Code for Reset Password

中优先级作业 - 重置密码的控制器代码

$job = (new SendResetPasswordEmail($this->tokens->create($user), $user))
        ->onConnection('Register')
        ->onQueue("medium");
dispatch($job);

Low priority job

低优先级工作

dispatch((new LowPriorityJob())->onQueue("low"));

Default priority job

默认优先作业

dispatch((new DefaultPriorityJob()));

->onConnection('Register') //this line is usefull if you specify you default connection is Register in .env QUEUE_DRIVER=Register

->onConnection('Register') //如果你指定默认连接是 Register in .env QUEUE_DRIVER=Register,这行很有用

Run your jobs

运行你的工作

this command run your jobs stored in default connection. In your case Register

此命令运行存储在默认连接中的作业。在你的情况下注册

php artisan queue:work --queue=high,medium,low,default

this command run your jobs stored in customConnectionName connection

此命令运行存储在 customConnectionName 连接中的作业

php artisan queue:work customConnectionName --queue=high,medium,low,default

回答by avip

You should have two queues defined in your config/queue.phpfile. Say, one with the name "high" and the other "low".

您应该在config/queue.php文件中定义了两个队列。比如说,一个名称为“高”,另一个名称为“低”。

Then, you can then dispatch jobs to them as needed like this:

然后,您可以根据需要将作业分派给他们,如下所示:

$job = (new SendResetPasswordEmail($this->tokens->create($user), $user))
        ->onConnection('ForgotPassword');
dispatch($job)->onQueue('high'));

Note: ->onQueue('high')

笔记: ->onQueue('high')

Finally, you would run: php artisan queue:work --queue=high,low

最后,您将运行: php artisan queue:work --queue=high,low

This will start a worker that will process all jobs on "high" queue before moving on to jobs on "low".

这将启动一个工作程序,该工作程序将处理“高”队列中的所有作业,然后再处理“低”队列中的作业。

回答by Heartbeat

Try this

尝试这个

'connections' => [

'Register-low' => [
    'driver'        =>  'database',
    'table'         =>  'tbljobs',
    'queue'         =>  'low',
    'retry_after'   =>  5,
],
'Register-high' => [
    'driver'        =>  'database',
    'table'         =>  'tbljobs',
    'queue'         =>  'high',
    'retry_after'   =>  5,
],

'ForgotPassword' => [
    'driver'        =>  'database',
    'table'         =>  'tbljobs',
    'queue'         =>  'low',
    'retry_after'   =>  5,
],

],

AND then

进而

php artisan queue:listen --queue=Register-high,Register-low

php artisan queue:listen --queue=注册高,注册低

回答by meow2x

Queue priorities is explained here https://laravel.com/docs/5.7/queues#queue-priorities. You simply need to "pass a comma-delimited list of queue names to the work command."

此处解释了队列优先级https://laravel.com/docs/5.7/queues#queue-priorities。您只需要“将逗号分隔的队列名称列表传递给工作命令”。

As the documentation says:

正如文档所说:

php artisan queue:work --queue=high,low

php artisan queue:work --queue=high,low

So dispatch((new Job)->onQueue('high'))will be given higher priority than dispatch((new Job)->onQueue('low')).

因此dispatch((new Job)->onQueue('high'))将获得比 更高的优先级dispatch((new Job)->onQueue('low'))

Or you could use your custom queue names:

或者您可以使用自定义队列名称:

php artisan queue:work --queue=first,second.

php artisan queue:work --queue=first,second.