如何使用队列设置高、低和中优先级电子邮件?
How to set high, low and medium priority email using queue?
问题
我读到每个连接都有队列参数,它告诉队列名称。问题是:如何设置发送低优先级或中优先级或高优先级电子邮件的优先级?
命令提示符
我正在使用命令:php artisan queue:listen 来处理作业。
我尝试了什么?
php artisan queue:work --queue=high,default
但如果连接的队列参数值不高,则此代码永远不会工作
默认队列驱动程序
'default' => env('QUEUE_DRIVER', 'database'),
队列连接数
'connections' => [
'Register' => [
'driver' => 'database',
'table' => 'tbljobs',
'queue' => 'low',
'retry_after' => 5,
],
'ForgotPassword' => [
'driver' => 'database',
'table' => 'tbljobs',
'queue' => 'low',
'retry_after' => 5,
],
],
.env
QUEUE_DRIVER=Register
注册邮箱的控制器代码
$job = (new SendActivationEmail($Data))
->onConnection('Register');
dispatch($job);
重置密码的控制器代码
$job = (new SendResetPasswordEmail($this->tokens->create($user), $user))
->onConnection('ForgotPassword');
dispatch($job);
您应该在 config/queue.php
文件中定义两个队列。例如,一个名称为 "high",另一个名称为 "low".
然后,您可以根据需要向他们分配作业,如下所示:
$job = (new SendResetPasswordEmail($this->tokens->create($user), $user))
->onConnection('ForgotPassword');
dispatch($job)->onQueue('high'));
注:->onQueue('high')
最后,你会 运行:
php artisan queue:work --queue=high,low
这将启动一个工作程序,该工作程序将处理 "high" 队列中的所有作业,然后再继续处理 "low" 上的作业。
试试这个
'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,
],
],
然后
php artisan queue:listen --queue=Register-high,Register-low
'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,
],
],
我建议您按以下方式修改您的代码:
'connections' => [
'Register' => [
'driver' => 'database',
'table' => 'tbljobs',
'queue' => 'default',
'retry_after' => 5,
],
],
高优先级作业 - 注册电子邮件的控制器代码
$job = (new SendActivationEmail($Data))
->onConnection('Register')
->onQueue("high");
dispatch($job);
中优先级作业 - 重置密码的控制器代码
$job = (new SendResetPasswordEmail($this->tokens->create($user), $user))
->onConnection('Register')
->onQueue("medium");
dispatch($job);
低优先级作业
dispatch((new LowPriorityJob())->onQueue("low"));
默认优先级作业
dispatch((new DefaultPriorityJob()));
->onConnection('Register') //this line is usefull if you specify you default connection is Register in .env QUEUE_DRIVER=Register
运行 你的工作
此命令 运行 您的作业存储在默认连接中。在你的情况下注册
php artisan queue:work --queue=high,medium,low,default
此命令运行您的作业存储在 customConnectionName 连接中
php artisan queue:work customConnectionName --queue=high,medium,low,default
记下 连接与。队列 Laravel's queue documentation 中的注释,据我所知,它适用于除 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:
实际上,您将在 config/queues.php
文件中注册一个队列连接,如果未提供另一个队列,default
参数将只是默认将作业分派到的队列。
Vitaly 上面的回答将是解决问题的正确方法(合并到具有默认队列的单个连接)然后调整您的作业以在需要时发送到不同的队列。这是关于队列配置如何工作的一些重要的(我认为)上下文。
此处解释了队列优先级 https://laravel.com/docs/5.7/queues#queue-priorities。
您只需要 "pass a comma-delimited list of queue names to the work command."
如文档所述:
php artisan queue:work --queue=high,low
因此 dispatch((new Job)->onQueue('high'))
将比 dispatch((new Job)->onQueue('low'))
获得更高的优先级。
或者您可以使用自定义队列名称:
php artisan queue:work --queue=first,second
.
问题
我读到每个连接都有队列参数,它告诉队列名称。问题是:如何设置发送低优先级或中优先级或高优先级电子邮件的优先级?
命令提示符
我正在使用命令:php artisan queue:listen 来处理作业。
我尝试了什么?
php artisan queue:work --queue=high,default
但如果连接的队列参数值不高,则此代码永远不会工作
默认队列驱动程序
'default' => env('QUEUE_DRIVER', 'database'),
队列连接数
'connections' => [
'Register' => [
'driver' => 'database',
'table' => 'tbljobs',
'queue' => 'low',
'retry_after' => 5,
],
'ForgotPassword' => [
'driver' => 'database',
'table' => 'tbljobs',
'queue' => 'low',
'retry_after' => 5,
],
],
.env
QUEUE_DRIVER=Register
注册邮箱的控制器代码
$job = (new SendActivationEmail($Data))
->onConnection('Register');
dispatch($job);
重置密码的控制器代码
$job = (new SendResetPasswordEmail($this->tokens->create($user), $user))
->onConnection('ForgotPassword');
dispatch($job);
您应该在 config/queue.php
文件中定义两个队列。例如,一个名称为 "high",另一个名称为 "low".
然后,您可以根据需要向他们分配作业,如下所示:
$job = (new SendResetPasswordEmail($this->tokens->create($user), $user))
->onConnection('ForgotPassword');
dispatch($job)->onQueue('high'));
注:->onQueue('high')
最后,你会 运行:
php artisan queue:work --queue=high,low
这将启动一个工作程序,该工作程序将处理 "high" 队列中的所有作业,然后再继续处理 "low" 上的作业。
试试这个
'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,
],
],
然后
php artisan queue:listen --queue=Register-high,Register-low
'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,
],
],
我建议您按以下方式修改您的代码:
'connections' => [
'Register' => [
'driver' => 'database',
'table' => 'tbljobs',
'queue' => 'default',
'retry_after' => 5,
],
],
高优先级作业 - 注册电子邮件的控制器代码
$job = (new SendActivationEmail($Data))
->onConnection('Register')
->onQueue("high");
dispatch($job);
中优先级作业 - 重置密码的控制器代码
$job = (new SendResetPasswordEmail($this->tokens->create($user), $user))
->onConnection('Register')
->onQueue("medium");
dispatch($job);
低优先级作业
dispatch((new LowPriorityJob())->onQueue("low"));
默认优先级作业
dispatch((new DefaultPriorityJob()));
->onConnection('Register') //this line is usefull if you specify you default connection is Register in .env QUEUE_DRIVER=Register
运行 你的工作
此命令 运行 您的作业存储在默认连接中。在你的情况下注册
php artisan queue:work --queue=high,medium,low,default
此命令运行您的作业存储在 customConnectionName 连接中
php artisan queue:work customConnectionName --queue=high,medium,low,default
记下 连接与。队列 Laravel's queue documentation 中的注释,据我所知,它适用于除 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:
实际上,您将在 config/queues.php
文件中注册一个队列连接,如果未提供另一个队列,default
参数将只是默认将作业分派到的队列。
Vitaly 上面的回答将是解决问题的正确方法(合并到具有默认队列的单个连接)然后调整您的作业以在需要时发送到不同的队列。这是关于队列配置如何工作的一些重要的(我认为)上下文。
此处解释了队列优先级 https://laravel.com/docs/5.7/queues#queue-priorities。 您只需要 "pass a comma-delimited list of queue names to the work command."
如文档所述:
php artisan queue:work --queue=high,low
因此 dispatch((new Job)->onQueue('high'))
将比 dispatch((new Job)->onQueue('low'))
获得更高的优先级。
或者您可以使用自定义队列名称:
php artisan queue:work --queue=first,second
.