yii2-queue 学习笔记

参考https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/README.md
一.基于redis

1.安装redis ,并进行相关配置

2.yii2 项目中使用composer 安装 yii2-redis

3.yii2 项目中使用compsoer 安装yii2-queue

4.将redis和queue配置写入文件例如在config中

$redis = require(__DIR__ . '/redis.php');
$queue = require(__DIR__ . '/queue.php');

  

web.php

'bootstrap' => ['log', 'queue'],
.....
'components' => [
'redis' => $redis,
'queue' => $queue,
....
]

  


queue.php

<?php

return [
'class' => 'yii\queue\redis\Queue',
'redis' => 'redis', // Redis connection component or its config
'channel' => 'queue', // Queue channel key
];

  

redis.php

<?php

return [
'class' => 'yii\redis\Connection',
'hostname' => '192.168.221.134',
'port' => 6379,
'database' => 0,
'password' => '123456'
];

  

5.新建任务类供具体代码调用,例如在components中新建task目录,并新建如下类

TesttaskJob.php
<?php

namespace app\components\task;

use Yii;
use yii\base\Object;
use app\models\MyTest;

class TesttaskJob extends Object implements \yii\queue\Job
{

public $name;
public $content;

public function execute($queue)
{
$mytest = new MyTest([
'name' => $this->name,
'content' => $this->content,
'created' => time(),
]);

$mytest->save();
}

}

  

调用部分

use app\components\task\TesttaskJob;

...
$job = new TesttaskJob([
'name' => 'zhangs',
'content' => 'ahjasdasdasd',
]);
$res = Yii::$app->queue->delay(2 * 60)->push($job);
...

  

6.服务器端 新建定时任务 后台执行 yii queue/listen ,执行了数个后台任务后可停止

yii queue/listen 5 //表示每隔5s监听一次队列
yii queue/run 运行队列任务直到空,一般用于定时cron
yii queue/info 查看队列状态

转载于:https://www.cnblogs.com/bestOrnothing/p/yii2_queue.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值