- 7
- 0
- 1
- 打赏
位置: 首页 >文章
composer require --prefer-dist yiisoft/yii2-queue
common/config/main.php
'bootstrap' => ['log','queue'],
'components' => [
'log' => [
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'redis' => require(__DIR__ . '/redis.php'),
'queue' => [
'class' => \yii\queue\redis\Queue::class,
'as log' => \yii\queue\LogBehavior::class,//错误日志 默认为 console/runtime/logs/app.log
'redis' => 'redis', // 连接组件或它的配置
'channel' => 'queue', // queue channel key
],
],
namespace app\common\jobs;
use yii\queue\JobInterface;
use yii\base\BaseObject;
class OrderCancelJob extends BaseObject implements JobInterface
{
public $orderId;
/**
* @param Queue $queue which pushed and is handling the job
*/
public function execute($queue)
{
$t = \Yii::$app->db->beginTransaction();
try {
$order_id = $this->orderId;
//根据订单编号处理业务逻辑
$t->commit();
} catch (\Exception $exception) {
$t->rollBack();
}
}
}
yii queue/listen
//直接使用
Yii::$app->queue->push(new OrderCancelJob(['orderId' => 'XXX']));
//5分钟之后执行
Yii::$app->queue->delay(5*60)->push(new OrderCancelJob(['orderId' => 'XXX']));
可以使用supervisor或systemd 来启动多进程worker
supervisor具体方法前面文章已详细讲解
https://ormcc.com/article/20
以上就是具体实现方法
Tags: yii queue supervisor 对列 延迟消息
转载:欢迎来到本站,转载请注明文章出处https://www.ormcc.com ,欢迎加入技术讨论群599606903
上一篇:搭建本地版ChatGPT
下一篇:输入框交互效果(一)
ormcc
一个爱捣鼓的程序员
IP访问121946次,运行1481天
微信支付宝