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)02dcommand=/usr/bin/php /var/www/my_project/yii queue/listen --verbose=1 --color=0autostart=trueautorestart=trueuser=www-datanumprocs=4redirect_stderr=truestdout_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 %IAfter=network.target# the following two lines only apply if your queue backend is mysql# replace this with the service that powers your backendAfter=mysql.serviceRequires=mysql.service[Service]User=www-dataGroup=www-dataExecStart=/usr/bin/php /var/www/my_project/yii queue/listen --verboseRestart=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_PUSH
与 Queue::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 基本版支持,但重试编号无法得到。