金沙娱场城app

 
 注册大米会员

qq登录

只需一步,快速开始

查看: 784|回复: 0

大米cms7.x使用异步队列(数据库)实现,当然您也可以用redis-金沙娱场城app

[复制链接]

549

主题

827

帖子

8632

积分

超级版主

rank: 8rank: 8

积分
8632

授权用户商城金牌vip

发表于 2025-6-19 15:30:57 | 显示全部楼层 |
本帖最后由 追影 于 2025-6-26 09:15 编辑

大米cms7.x使用异步队列(数据库版)实现实例:
使用场景:有时我们需要点击某个按钮立刻响应, 但实际需要执行一个大的任务(耗时未知或很长)才会回调.这就需要用到异步队列

1:安装依赖:
  1. composer require topthink/think-queue
复制代码

2:配置队列为使用数据库


3:创建数据库迁移文件
  1. php think queue:table
  2. php think queue:failed-table
  3. php think migrate:run
复制代码
4:创建队列任务(队列里要做的任务)


  1. namespace app\job;

  2. use app\base\service\foodai;
  3. use think\facade\config;
  4. use think\facade\log;
  5. use think\queue\job;
  6. use app\base\model\article as articlemodel;
  7. use think\facade\db;

  8. class pythonstudy
  9. {

  10.     // 最大尝试次数(默认 3 次)
  11.     public $tries = 2;

  12.     // 最大异常次数(可选)
  13.     public $maxexceptions = 2;
  14.     public function fire(job $job, $article) {
  15.         // 处理任务逻辑
  16.         try {
  17.             //处理任务
  18.             $this->do_upload($article);
  19.             // 任务成功处理后删除
  20.             $job->delete();
  21.         } catch (\exception $e) {
  22.             // 任务失败处理
  23.             $job->fail();
  24.         }
  25.     }

  26.     public function failed($data) {
  27.         // 任务失败回调
  28.         echo "job failed: " . json_encode($data) . "\n";
  29.         log::error('python学习任务失败', $data);
  30.     }

  31.     private function shouldupload($article)
  32.     {
  33.         return $article->title
  34.             && $article->aid
  35.             && $article->ai_pictures
  36.             && !$article->upload_ai
  37.             && !$article->public_price
  38.             && !$article->isflash;
  39.     }



  40.     private function do_upload($article_arr){
  41.         try {
  42.         $article = (object)$article_arr;//习惯用object
  43.         if (!$this->shouldupload($article)) {
  44.             echo '验证参数不通过';
  45.             return;
  46.         }
  47.         $str_pics = $article->ai_pictures;
  48.         $company_code = $article->company_code?:$this->request->param('company_code');
  49.         if(!$company_code) {echo('未传递company_code');return;}
  50.         \payment\common::changedbbycompany($company_code);
  51.         //上传数据集
  52.         $baidu_service = new foodai();
  53.         $r = $baidu_service->multi_study($str_pics, $article_arr,$company_code);
  54.         if($r) {
  55.             $article_model = new articlemodel();
  56.             $article_model->where('aid', '=', $article->aid)->save(['upload_ai' => 1]);
  57.             echo 'python do upload success';
  58.         }else{
  59.             echo 'python do upload fail';
  60.         }

  61.         return true;
  62.         }catch (\exception $exception){
  63.             echo $exception->getfile().$exception->getmessage();
  64.         }
  65.         return false;
  66.     }


  67. }
复制代码

(5)写一个事件促发异步队列

  1. namespace app\base\event;

  2. use app\base\service\baidu as baiduservice;
  3. use think\facade\log;
  4. use think\facade\queue;

  5. class pythonai
  6. {
  7.     public function handle($article)
  8.     {
  9.         queue::push(\app\job\pythonstudy::class,$article);
  10.     }
  11. }
复制代码

(6)注册事件与触发该事件


注册全局事件 app/event.php
  1. // 事件定义文件
  2. return [
  3.     'bind'      => [
  4.     ],
  5.     'listen'    => [
  6.         'appinit'  => [],
  7.         'httprun'  => [],
  8.         'httpend'  => [],
  9.         'loglevel' => [],
  10.         'logwrite' => [],
  11.         'aistudy' => [
  12.             \app\base\event\pythonai::class,
  13.         ],

  14.     ],

  15.     'subscribe' => [
  16.     ],
  17. ];
复制代码
触发事件:用内置的event函数即可
  1.     private function do_event($article){
  2.         $article->company_code = $this->get_company_code();
  3.         $article_arr = $article->toarray();
  4.         //var_dump($article_arr);
  5.         //触发事件
  6.         event('aistudy',$article_arr);
  7.     }
复制代码



(7)最后 计划任务 让队列跑起来
  1. # 以守护进程方式运行
  2. php think queue:work
复制代码


顺带说下centos守护进程方式运行supervisord配置如下
  1. [program:think-queue]
  2. command=php /wwwroot/hall/xunyuan/think queue:work
  3. directory=/wwwroot/hall/xunyuan
  4. autostart=true
  5. autorestart=true
  6. startretries=3
  7. user=nginx
  8. numprocs=1
  9. redirect_stderr=true
  10. stdout_logfile=/var/log/supervisor/think-queue.log
复制代码




您需要登录后才可以回帖 登录 | 注册大米会员

本版积分规则

快速回复 返回列表
网站地图