yii2-queue

9次阅读

共计 3354 个字符,预计需要花费 9 分钟才能阅读完成。

Worker starting control

[](https://github.com/yiisoft/yi…

Supervisor 是 Linux 的进程监视器。
它会自动启动您的控制台进程。
安装在 Ubuntu 上,你需要运行命令:

sudo apt-get install supervisor

Supervisor 配置文件通常可用 /etc/supervisor/conf.d。你可以创建任意数量的配置文件。

配置示例:

[program:yii-queue-worker]
process_name=%(program_name)s_%(process_num)02d
command=/usr/bin/php /var/www/my_project/yii queue/listen --verbose=1 --color=0
autostart=true
autorestart=true
user=www-data
numprocs=4
redirect_stderr=true
stdout_logfile=/var/www/my_project/log/yii-queue-worker.log

在这种情况下,Supervisor 会启动 4 个 queue/listen worker。输出将写入相应日志文件。

有关 Supervisor 配置和使用的更多信息,请参阅文档。

以守护进程模式启动的 Worker 使用 queue/listen 命令支持 File、Db、Redis、RabbitMQ、Beanstalk、Gearman 驱动。有关其他参数,请参阅驱动程序指南。

[](https://github.com/yiisoft/yi…

Systemd is an init system used in Linux to bootstrap the user space. To configure workers startup using systemd, create a config file named yii-queue@.service in /etc/systemd/system with the following contents:

[Unit]
Description=Yii Queue Worker %I
After=network.target
# the following two lines only apply if your queue backend is mysql
# replace this with the service that powers your backend
After=mysql.service
Requires=mysql.service

[Service]
User=www-data
Group=www-data
ExecStart=/usr/bin/php /var/www/my_project/yii queue/listen --verbose
Restart=on-failure

[Install]
WantedBy=multi-user.target

You need to reload systemd in order for it to re-read configuration:

systemctl daemon-reload

Set of commands to control workers:

To start two workers

systemctl start yii-queue@1 yii-queue@2

To get status of running workers

systemctl status “yii-queue@*”

To stop the worker

systemctl stop yii-queue@2

To stop all running workers

systemctl stop “yii-queue@*”

To start two workers at system boot

systemctl enable yii-queue@1 yii-queue@2

To learn all features of systemd, check its documentation.

[](https://github.com/yiisoft/yi…

您可以用 cron 开始 worker。需要使用 queue/run 命令。只要队列包含作业,它就能进行执行。

配置示例:

* * * * * /usr/bin/php /var/www/my_project/yii queue/run

在这种情况下,cron 将每分钟启动一次命令。

queue/run 命令支持 File、Db、Redis、Beanstalk、Gearman 驱动。有关其他选项,请参阅驱动程序指南。

错误与重复执行

在作业处理期间可以抛出异常。当请求的服务和外部资源不可用时,由于代码编写的比较糟糕而导致的内部错误。
在第二种情况下,可以在一段时间后重新尝试这个作业。

有几种方法可以做到这一点。

[](https://github.com/yiisoft/yi…

第一个方法由组件选项实现:

‘components’ => [

'queue' => \[
    'class' => \\yii\\queue\\<driver>\\Queue::class,
    'ttr' => 5 \* 60, // Max time for anything job handling 
    'attempts' => 3, // Max number of attempts
\],

],

ttr 选项设置了在队列中保留工作的时间。如果一份作业在这段时间没有执行,它将返回队列进行重试。
attempts 选项设置了最大的尝试次数。如果尝试已经结束,作业作还没有完成,它将从队列中移除。

这种将全局设置队列中的所有作业,如果您需要为多个作业进行不同的设置可以使用,第二种方法。

[](https://github.com/yiisoft/yi…

Separate control of retry is implemented by RetryableJobInterface 接口。示例:

class SomeJob extends BaseObject implements RetryableJobInterface
{

public function execute($queue)
{//...}

public function getTtr()
{return 15 \* 60;}

public function canRetry($attempt, $error)
{return ($attempt < 5) && ($error instanceof TemporaryException);
}

}

getTtr()canRetry() 方法比组件配置有更高的优先级。

[](https://github.com/yiisoft/yi…

第三种方法设置 TTR 和需要重试失败的作业包括使用 Queue::EVENT_BEFORE_PUSHQueue::EVENT_AFTER_ERROR 事件。

Queue::EVENT_BEFORE_PUSH 事件可用于设置 TTR:

Yii::$app\->queue\->on(Queue::EVENT\_BEFORE\_PUSH, function (PushEvent $event) {

if ($event\->job instanceof SomeJob) {$event\->ttr = 300;}

});

并且 Queue::EVENT_AFTER_ERROR 事件可用于设置新的尝试:

Yii::$app\->queue\->on(Queue::EVENT\_AFTER\_ERROR, function (ExecEvent $event) {

if ($event\->job instanceof SomeJob) {$event\->retry = ($event\->attempt < 5) && ($event\->error instanceof TemporaryException);
}

});

事件处理程序在 RetryableJobInterface 方法之后执行,因此具有最高优先级。

[](https://github.com/yiisoft/yi…

完全支持 Beanstalk, DB, File 和 Redis 驱动程序的重试工具。Sync 驱动不会重试失败的工作 Gearman 不支持重试。RabbitMQ 基本版支持,但重试编号无法得到。

正文完
 0