djeux / yii2-queue
dev-master
2018-10-10 19:16 UTC
Requires
- pda/pheanstalk: ^3.0
- symfony/process: 3.1
- yiisoft/yii2: >=2.0.13
- yiisoft/yii2-redis: ~2.0.0
Requires (Dev)
- phpunit/phpunit: 5.5.*
This package is auto-updated.
Last update: 2024-09-11 13:50:45 UTC
README
##为yii2提供的通用队列组件
目前仅支持beanstalkd驱动,但可以轻松扩展以支持其他类型
##安装
composer require djeux/yii2-queue
##配置 在你的 config/main.php 中(因为你需要设置队列以在控制台和Web中工作)
'components' => [ 'queue' => [ 'class' => 'djeux\queue\BeanstalkdQueueManager', // in this example we use beanstalkd driver 'host' => '127.0.0.1', 'worker' => [ 'listen' => ['default', /* here you define what tubes should the worker listen to */] ], ], ]
##使用 在你的代码中,你可以通过以下两种方式推送任务
- 通过传递一个实现了 'Queueable' 接口的对象
Yii::$app->queue->push(new MyJob("text to push"), '', 'default'); class MyJob implements Queueable { protected $pushedText; public function __construct($text) { $this->pushedText = $text; } /** * @param $job BaseJob */ public function handle($job) { echo $this->pushedText; $job->delete(); } }
- 通过传递一个由 '@' 分隔的类和方法字符串
Yii::$app->queue->push('app\components\queue\MyJob@myMethod', 'text to push', 'default'); class MyJob { public function myMethod($job, $data) { echo $data; $job->delete(); } }
工作进程
你可以设置工作进程通过supervisord运行
有两种主要方法
php yii queue/manager
运行一个管理器,该管理器检查配置的工作进程并为每个列出的队列启动一个进程
php yii queue/worker <queue>
运行一个守护进程,该守护进程处理队列任务
故障排除
如果你有使用数据库连接的任务,并且它们在守护进程模式下运行,服务器最终会断开连接。@see wait_timeout
为了应对这种情况,\djeux\queue\helpers\TimeoutTrait 中有一个特质,你可以调用它来保持任务处理并重新连接,如果连接断开
对于swiftmailer也是如此,如果你在发送电子邮件。SMTP最终会断开连接
421 Timeout waiting for data from client.
为了应对这种情况,你可以使用
try { \Yii::$app->mailer->send($message); } catch (\Exception $e) { if (strpos(strtolower($e->getMessage()), '421 timeout') !== false) { if (($mailer = \Yii::$app->mailer) instanceof Mailer) { /* @var $mailer Mailer */ $mailer->getSwiftMailer()->getTransport()->stop(); $this->send($job, $data); } } }
你可以添加“beforeJob”和“afterJob”等方法,这些方法将在任务处理前后被调用。
##备注. 包灵感来自Laravel队列系统的实现。
对于任何建议、修复和负面评论,请写入问题。