使用composer安装队列包
composer require topthink/think-queue
安装完毕系统配置redis相关配置
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>
// +----------------------------------------------------------------------
return [
'default' => 'redis',
'connections' => [
'sync' => [
'type' => 'sync',
],
'database' => [
'type' => 'database',
'queue' => 'default',
'table' => 'jobs',
'connection' => null,
],
'redis' => [
'type' => 'redis',
'queue' => 'default',
'host' => '127.0.0.1',
'port' => 6379,
'password' => '',
'select' => 1,
'timeout' => 0,
'persistent' => false,
],
],
'failed' => [
'type' => 'none',
'table' => 'failed_jobs',
],
];
编写入队代码
<?php
namespace app\controller;
use app\BaseController;
use think\facade\Db;
use think\facade\Queue;
/**
* todo 提醒进入队列
* @author zhuyanbin
* @time 2023年9月23日15:21:48
**/
class Notify extends BaseController {
/**
* 将客户资料加入到redis队列
* php /www/wwwroot/oyjst.sdoumei.com/public/cli.php notify/nextContact
**/
public function nextContact() {
$client = Db::table('fa_client')
->where('status','in',['已加微信','已加企微'])
->select();
//dump($client);
foreach ($client as $k => $v) {
$jobHandlerClassName = 'app\job\Notify@nextContact';
$jobQueueName = "nextContact";
$isPushed = Queue::push($jobHandlerClassName, $v, $jobQueueName);
dump($isPushed);
exit();
}
}
}
消费队列代码
<?php
namespace app\job;
use app\BaseController;
use think\facade\Db;
use think\queue\Job;
class Notify extends BaseController {
/**
* 队列消费
* php think queue:work --queue nextContact
**/
public function nextContact(Job $job, $data) {
//dump($data);
$job->delete();
}
}
消费队列
php think queue:work --queue nextContact
监听消费
php think queue:listen --queue nextContact