thinkphp下的Job队列处理

需要think-queue扩展,没有的请composer安装一下

bash 复制代码
    "require": {
        "php": ">=7.2.5",
        "topthink/framework": "^6.1",
        "topthink/think-orm": "^2.0",
        "topthink/think-multi-app": "^1.0",
        "topthink/think-filesystem": "^2.0",
        "firebase/php-jwt": "^6.2",
        "overtrue/pinyin": "~4.0",
        "hg/apidoc": "^5.0",
        "overtrue/wechat": "^3.3",
        "qiniu/php-sdk": "^7.4",
        "aliyuncs/oss-sdk-php": "^2.4",
        "qcloud/cos-sdk-v5": "^2.3",
        "guzzlehttp/guzzle": "^6.0",
        "psr/container": "1.1.1",
        "phpmailer/phpmailer": "^6.5",
        "overtrue/easy-sms": "^2.1",
        "upyun/sdk": "^3.5",
        "aws/aws-sdk-php": "^3.250",
        "fastknife/ajcaptcha": "^1.2",
        "topthink/think-worker": "^3.0",
        "chinayin/ip2region": "^2.0",
        "topthink/think-throttle": "^1.3",
        "topthink/think-queue": "^3.0",
        "alibabacloud/ocr-api-20210707": "1.0.1",
        "alibabacloud/darabonba-stream": "^0.0.1",
        "phpoffice/phpexcel": "^1.8",
        "alibabacloud/facebody-20191230": "1.0.2",
        "elasticsearch/elasticsearch": "v7.10.0",
        "endroid/qrcode": "^4.3",
        "predis/predis": "^2.2",
        "ext-openssl": "*",
        "godruoyi/php-snowflake": "^2.2",
      "ext-sodium": "*",
      "ext-bcmath": "*",
        "ext-json": "*",
        "jpush/jpush":  "*",
      "ext-ftp": "*",
        "alibabacloud/ocr-20191230": "^1.0"
    }

1,在用到的地方进行队列插入

bash 复制代码
CdrJob::dispatch([$cdr], [], $queueName);

如果需要延迟,可以这样

bash 复制代码
Openai1Job::dispatchSece(60 , [$cdr], [], 'Openai1Job');

2,Job下获取队列处理,例如

bash 复制代码
namespace app\job;

use app\job\basic\BaseJobs;
use app\job\basic\QueueTrait;
use think\facade\Log;

/**
 * OpenAI处理
 * Class Openai1Job
 * @package app\job
 */
class Openai1Job extends BaseJobs
{

    use QueueTrait;

    public function doJob($cdr)
    {
        try {
            //AI分析处理
            $services = app()->make(\app\common\service\api\OpenaiService::class);
            return $services::OpenaiConsumer($cdr);
        } catch (\Throwable $e) {
            Log::error('Openai1Job,AI分析失败原因:' . $e->getMessage());
            return false;
        }
    }
}

3,添加守护进程,可通过宝塔安装进程守护管理器管理

bash 复制代码
php think queue:listen --queue Openai1Job
相关推荐
Asthenia041244 分钟前
Spring AOP 和 Aware:在Bean实例化后-调用BeanPostProcessor开始工作!在初始化方法执行之前!
后端
Asthenia04122 小时前
什么是消除直接左递归 - 编译原理解析
后端
Asthenia04122 小时前
什么是自上而下分析 - 编译原理剖析
后端
Asthenia04122 小时前
什么是语法分析 - 编译原理基础
后端
Asthenia04122 小时前
理解词法分析与LEX:编译器的守门人
后端
uhakadotcom2 小时前
视频直播与视频点播:基础知识与应用场景
后端·面试·架构
Asthenia04123 小时前
Spring扩展点与工具类获取容器Bean-基于ApplicationContextAware实现非IOC容器中调用IOC的Bean
后端
bobz9654 小时前
ovs patch port 对比 veth pair
后端
Asthenia04124 小时前
Java受检异常与非受检异常分析
后端