yii2-队列使用
安装yii2-queue
composer require yiisoft/yii2-queue
安装yii2-redis
composer require yiisoft/yii2-redis
控制台配置文件 console.php 和 web.php
'redis' => [
'class' => 'yii\redis\Connection',
'hostname' => '127.0.0.1',
'port' => 6379,
'database' => 0,
],
'queue' => [
'class' => 'yii\queue\redis\Queue',
'redis' => 'redis', // 连接组件或它的配置
'channel' => 'queue',
],
队列文件 common\queues\TestJob.php
<?php
namespace app\common\queues;
use app\modules\admin\models\Admin;
use yii\base\BaseObject;
class TestJob extends BaseObject implements \yii\queue\JobInterface
{
public $username;
public function execute($queue)
{
$this->addAdmin();
}
public function addAdmin(){
$admin = new Admin();
$admin->username = $this->username;
$admin->password = '123456';
$admin->save();
}
}
控制器 TestController.php
public function actionIndex()
{
$id = Yii::$app->queue->push(new TestJob(['username'=>'useranme']));
}
开启控制台队列监听
php yii queue/listen
访问测试
http://www.yii2.com/test/index
yii2操作queue方法
$id = Yii::$app->queue->push(new Job()); //推送作业到队列
Yii::$app->queue->isWaiting($id); //判断作业是否等待中
Yii::$app->queue->isReserved($id); //判断作用是否执行中
Yii::$app->queue->isDone($id);//作业是否执行完
控制台queue命令
php yii queue/info //查看队列信息
php yii queue/run //运行队列
php yii queue/listen //监听队列
php yii queue/clear //清空队列
标签:php,队列,yii,redis,queue,使用,yii2
From: https://www.cnblogs.com/hu308830232/p/18105469