think-queue 消息队列
chenvle
2022-11-04 PM
2045℃
3条
composer require topthink/think-queue
<?php
return [
'default' => 'redis',
'connections' => [
'sync' => [
'type' => 'sync',
],
'database' => [
'type' => 'database',
'queue' => 'default',
'table' => 'jobs',
],
'redis' => [
'type' => 'redis',
'queue' => 'default',
'host' => '127.0.0.1',
'port' => 6379,
'password' => '',
'select' => 0,
'timeout' => 0,
'persistent' => false,
],
],
'failed' => [
'type' => 'none',
'table' => 'failed_jobs',
],
];
- 创建消费者类
- 编辑消费者类
<?php
namespace app\admin\job;
use think\queue\Job;
class Task
{
public function fire(Job $job, $data)
{
$rt = $this->doJob($data);
if($rt){
$job->delete();
return true;
}
// 重试三次失败 todo...
if($job->attempts() == 3){
$job->delete();
return false;
}
//执行失败10S后重试
$job->release(10);
}
public function doJob($data)
{
//执行任务
return true;
}
}
public function redis()
{
$jobHandlerClassName = 'app\admin\job\Task';
$jobQueueName = 'task';
$orderData = ['order_sn'=>time()];
//Queue::push();//立即执行
//这儿的10是指10秒后执行队列任务
$isPushed = Queue::later('10', $jobHandlerClassName, $orderData, $jobQueueName);
if($isPushed !== false){
echo '队列添加成功';
}else{
echo '插入失败了';
}
}
php think queue:listen --queue task
或
php think queue:work --queue task
- 拓展:Supervisor安装
- 备注
$isPushed = Queue::later('10', $jobHandlerClassName, $orderData, $jobQueueName); 中如果$jobQueueName 不传参数,则队列监听可不指定队列任务(php think queue:listen),如传了则必须要指定任务才会执行!!!#避坑
《声生不息家年华葫芦家庭录像带》大陆综艺高清在线免费观看:https://www.jgz518.com/xingkong/14959.html
你的才华让人瞩目,期待你的更多文章。 http://www.55baobei.com/6ocxQQBPTb.html
看的我热血沸腾啊