Your Name 7 months ago
parent
commit
e341ac4afc
3 changed files with 180 additions and 10 deletions
  1. 55
    9
      application/api/controller/Cms.php
  2. 124
    0
      application/common/jobs/TagHandle.php
  3. 1
    1
      config/queue.php

+ 55
- 9
application/api/controller/Cms.php View File

14
 // +----------------------------------------------------------------------
14
 // +----------------------------------------------------------------------
15
 namespace app\api\controller;
15
 namespace app\api\controller;
16
 
16
 
17
-use app\common\controller\Api;
18
-use app\common\library\Ems as Emslib;
19
 use app\member\controller\MemberApi;
17
 use app\member\controller\MemberApi;
20
-use app\member\model\Member;
21
-use think\facade\Hook;
22
-use think\facade\Validate;
23
 use think\Db;
18
 use think\Db;
24
 
19
 
25
 /**
20
 /**
30
 class Cms extends MemberApi
25
 class Cms extends MemberApi
31
 {
26
 {
32
 
27
 
33
-    protected $noNeedLogin = ['login', 'register','getProduct','getProductSmall','createTask','tagHandle','taskHandle','getAreaJson'];
28
+    protected $noNeedLogin = ['login', 'register','getProduct','getProductSmall','createTask','tagHandle','taskHandle','getAreaJson','getTagListHandle'];
34
     protected $noNeedRight = [];
29
     protected $noNeedRight = [];
35
 
30
 
36
     //初始化
31
     //初始化
345
             ->order('add_time asc')->paginate(1);
340
             ->order('add_time asc')->paginate(1);
346
         $list = $list->toArray();
341
         $list = $list->toArray();
347
         $data = $list['data'];
342
         $data = $list['data'];
343
+
344
+        if(empty($data)){
345
+            $this->error('fail',['ko'=>'不存在数据']);
346
+        }
347
+
348
         foreach ($data as $index => $row) {
348
         foreach ($data as $index => $row) {
349
 
349
 
350
             //获取标签
350
             //获取标签
385
                        'run_time' => time(),
385
                        'run_time' => time(),
386
                        'status' => 1,
386
                        'status' => 1,
387
                    ]);
387
                    ]);
388
-                   $this->error('fail');
388
+                   $this->error('fail',['is_run'=>4]);
389
                }
389
                }
390
 
390
 
391
                //是否只有一个页面 (远程)
391
                //是否只有一个页面 (远程)
408
                        'run_time' => time(),
408
                        'run_time' => time(),
409
                        'status' => 1,
409
                        'status' => 1,
410
                    ]);
410
                    ]);
411
-                   $this->error('fail');
411
+                   $this->error('fail',['is_run'=>3]);
412
                }
412
                }
413
 
413
 
414
                //创建标签
414
                //创建标签
519
         $data = $list['data'];
519
         $data = $list['data'];
520
         //dump($data);die;
520
         //dump($data);die;
521
 
521
 
522
+        if(empty($data)){
523
+            $this->error('fail',['ko'=>'不存在数据']);
524
+        }
522
         /*
525
         /*
523
          * 跨库 无法保证远程和本地的事务一致性
526
          * 跨库 无法保证远程和本地的事务一致性
524
          * 这种机制 类似 支付
527
          * 这种机制 类似 支付
786
             $one = Db::connect('db1')->table('ey_seo_number')->where([
789
             $one = Db::connect('db1')->table('ey_seo_number')->where([
787
                     'type' => 1,
790
                     'type' => 1,
788
                     'aid' => $row['aid'],
791
                     'aid' => $row['aid'],
789
-                ])->where('parentid','!=',38)->find();
792
+                ])->where('parentid','<>',38)->find();
790
             $number = Db::connect('db1')->table('ey_seo_number')->where([
793
             $number = Db::connect('db1')->table('ey_seo_number')->where([
791
                     'type' => 1,
794
                     'type' => 1,
792
                     'aid' => $one['parentid'],
795
                     'aid' => $one['parentid'],
860
 
863
 
861
     }
864
     }
862
 
865
 
866
+    /*
867
+     * 批量执行标签处理
868
+     * 加入队列 http://console.zx2049.com/api/cms/getTagListHandle
869
+     * 启动队列 php think queue:listen --queue tagHandle
870
+     * 监听队列 进程守护管理器 (附加参数参考: https://www.kancloud.cn/ken678/yzncms/2383907)
871
+     * redis查看数据器 默认重置次数1
872
+     * workman实时推送消息
873
+     * 添加自定义命令的方式来执行队列
874
+     */
875
+    public function getTagListHandle()
876
+    {
877
+
878
+
879
+/*        $a = new \app\api\queue\jobs\TagHandle();
880
+        var_dump($a);
881
+        return json([]);*/
882
+
883
+
884
+
885
+        $area = Db::connect('db1')
886
+            ->table('ey_citysite')
887
+            ->where(['level'=>1,'is_open'=>1,'status'=>1])
888
+            ->select();
889
+        //dump($area);
890
+        $area[] = [
891
+            'id' => 10000,
892
+            'name' => '主站',
893
+        ];
894
+
895
+        foreach ($area as $k=>$v){
896
+            //http://console.zx2049.com/api/cms/tagHandle?taskid=10&diqu=1
897
+            $jobData  = ['ts' => time(),'url'=>'http://console.zx2049.com/api/cms/tagHandle','taskid'=>10, 'area_id' => $v['id'], 'area_name' => $v['name']];//当前任务所需的业务数据
898
+            $isPushed = \think\Queue::push('app\common\jobs\TagHandle', $jobData,'tagHandle');//没有@使用默认方法fire,此方法为立即执行
899
+            if ($isPushed !== false) {
900
+                echo "ok";
901
+            } else {
902
+                echo 'warn';
903
+            }
904
+            echo "<br/>";
905
+        }
906
+
907
+    }
908
+
863
 
909
 
864
 }
910
 }

+ 124
- 0
application/common/jobs/TagHandle.php View File

1
+<?php
2
+// +----------------------------------------------------------------------
3
+// | Yzncms [ 御宅男工作室 ]
4
+// +----------------------------------------------------------------------
5
+// | Copyright (c) 2018 http://yzncms.com All rights reserved.
6
+// +----------------------------------------------------------------------
7
+// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
+// +----------------------------------------------------------------------
9
+// | Author: 御宅男 <530765310@qq.com>
10
+// +----------------------------------------------------------------------
11
+
12
+// +----------------------------------------------------------------------
13
+// | 测试队列,仅供参考
14
+// +----------------------------------------------------------------------
15
+namespace app\common\jobs;
16
+
17
+use think\facade\Log;
18
+use think\queue\Job;
19
+
20
+class TagHandle
21
+{
22
+
23
+    /**
24
+     * fire方法是消息队列默认调用的方法
25
+     * @param Job            $job      当前的任务对象
26
+     * @param array|mixed    $data     发布任务时自定义的数据
27
+     */
28
+    public function fire(Job $job, $data)
29
+    {
30
+        $isJobDone = $this->doHelloJob($data);
31
+        if ($isJobDone) {
32
+            // 如果任务执行成功, 记得删除任务
33
+            $job->delete();
34
+            print("<info>Hello Job has been done and deleted" . "</info>\n");
35
+        } else {
36
+            //超过3次删除任务 如果不删除 并且命令行附带--tries参数,则会执行failed方法
37
+            if ($job->attempts() > 3) {
38
+                $job->delete();
39
+                print("<warn>Hello Job has been retried more than 3 times!" . "</warn>\n");
40
+            } else {
41
+                //5秒后从新放入队列
42
+                $job->release(5);
43
+            }
44
+        }
45
+    }
46
+
47
+    //多任务A 在发布任务时,需要用 push('任务的类名@方法名')
48
+    public function taskA(Job $job, $data)
49
+    {
50
+        $isJobDone = $this->_doTaskA($data);
51
+        if ($isJobDone) {
52
+            $job->delete();
53
+            print("Info: TaskA of Job MultiTask has been done and deleted" . "\n");
54
+        } else {
55
+            if ($job->attempts() > 3) {
56
+                $job->delete();
57
+            }
58
+        }
59
+    }
60
+
61
+    //多任务B 在发布任务时,需要用 push('任务的类名@方法名')
62
+    public function taskB(Job $job, $data)
63
+    {
64
+        $isJobDone = $this->_doTaskA($data);
65
+        if ($isJobDone) {
66
+            $job->delete();
67
+            print("Info: TaskB of Job MultiTask has been done and deleted" . "\n");
68
+        } else {
69
+            if ($job->attempts() > 2) {
70
+                $job->release();
71
+            }
72
+        }
73
+    }
74
+
75
+    /**
76
+     * 根据消息中的数据进行实际的业务处理...
77
+     */
78
+    private function doHelloJob($data)
79
+    {
80
+/*        print("<info>Hello Job Started. job Data is: " . var_export($data, true) . "</info> \n");
81
+        print("<info>Hello Job is Fired at " . date('Y-m-d H:i:s') . "</info> \n");
82
+        print("<info>Hello Job is Done!" . "</info> \n");*/
83
+
84
+        /*
85
+         * "data": {
86
+            "ts": 1713866309,
87
+            "url": "http:\/\/console.zx2049.com\/api\/cms\/tagHandle",
88
+            "taskid": 10,
89
+            "area_id": 1,
90
+            "area_name": "\u5e7f\u4e1c"
91
+          },
92
+         */
93
+
94
+        //测试
95
+        print("<info>执行标签处理: " . $data['area_id'] . "</info> \n");
96
+
97
+        /*//拼装url
98
+        $url = $data['url'].'?taskid='.$data['taskid'].'&diqu='.$data['area_id'];
99
+        //发送执行
100
+        $content = file_get_contents($url);
101
+        echo $content;*/
102
+
103
+        return true;
104
+    }
105
+
106
+    private function _doTaskA($data)
107
+    {
108
+        print("Info: doing TaskA of Job MultiTask " . "\n");
109
+        return true;
110
+    }
111
+
112
+    private function _doTaskB($data)
113
+    {
114
+        print("Info: doing TaskB of Job MultiTask " . "\n");
115
+        return true;
116
+    }
117
+
118
+    //此函数可以省略 通常用于定义通知管理员队列失败
119
+    //任务最终失败回调 命令行的 --tries 参数的值大于0生效
120
+    public function failed($jobData)
121
+    {
122
+        Log::error("Warning: Job failed after max retries. job data is :" . var_export($jobData, true));
123
+    }
124
+}

+ 1
- 1
config/queue.php View File

15
     'default'    => 'default', // 默认的队列名称
15
     'default'    => 'default', // 默认的队列名称
16
     'host'       => '127.0.0.1', // redis 主机ip
16
     'host'       => '127.0.0.1', // redis 主机ip
17
     'port'       => 6379, // redis 端口
17
     'port'       => 6379, // redis 端口
18
-    'password'   => '', // redis 密码
18
+    'password'   => 'Zhengwei178..', // redis 密码
19
     'select'     => 0, // 使用哪一个 db,默认为 db0
19
     'select'     => 0, // 使用哪一个 db,默认为 db0
20
     'timeout'    => 0, // redis连接的超时时间
20
     'timeout'    => 0, // redis连接的超时时间
21
     'persistent' => false, // 是否是长连接
21
     'persistent' => false, // 是否是长连接

Loading…
Cancel
Save