Anyon 5 лет назад
Родитель
Сommit
639483edd9

+ 0 - 1
app/admin/controller/api/Queue.php

@@ -3,7 +3,6 @@
 
 namespace app\admin\controller\api;
 
-
 use think\admin\Controller;
 use think\admin\service\QueueService;
 

+ 5 - 5
app/wechat/command/Fans.php

@@ -52,11 +52,11 @@ class Fans extends Command
     {
         $message = '';
         foreach ($this->module as $m) {
-            if (method_exists($this, $fun = "_{$m}")) {
-                $message .= $this->$fun();
+            if (method_exists($this, $method = "_{$m}")) {
+                $message .= $this->$method();
             }
         }
-        $this->endQueueMessage(3, $message);
+        $this->queueProgressState(3, $message);
     }
 
     /**
@@ -80,8 +80,8 @@ class Fans extends Command
                 if (is_array($list = WechatService::WeChatUser()->getBatchUserInfo($openids)) && !empty($list['user_info_list'])) {
                     foreach ($list['user_info_list'] as $user) {
                         $string = str_pad(++$done, strlen($result['total']), '0', STR_PAD_LEFT);
-                        $this->setQueuePropress(2, "{$user['openid']} {$user['nickname']}", $done * 100 / $result['total']);
-                        $this->output->writeln("({$string}/{$result['total']}) -> {$user['openid']} {$user['nickname']}");
+                        $message = "({$string}/{$result['total']}) -> {$user['openid']} {$user['nickname']}";
+                        $this->queueProgressMessage(2, $message, $done * 100 / $result['total']);
                         FansService::instance()->set($user, $appid);
                     }
                 }

+ 4 - 8
app/wechat/command/Tests.php

@@ -3,8 +3,7 @@
 
 namespace app\wechat\command;
 
-use think\admin\service\QueueService;
-use think\console\Command;
+use think\admin\Command;
 use think\console\Input;
 use think\console\Output;
 
@@ -17,13 +16,10 @@ class Tests extends Command
 
     protected function execute(Input $input, Output $output)
     {
-        $max = 100;
+        $max = 10000;
         for ($i = 0; $i < $max; $i++) {
-            echo $i . PHP_EOL;
-            if (defined('WorkQueueCode')) {
-                QueueService::instance()->progress(WorkQueueCode, 2, "已经完成了 $i 的计算", $i / $max * 100);
-            }
-            sleep(1);
+            $this->queueProgressMessage(2, "已经完成了 $i 的计算", $i / $max * 100);
+            usleep(5000);
         }
     }
 

+ 4 - 4
app/wechat/controller/Fans.php

@@ -72,12 +72,12 @@ class Fans extends Controller
     public function sync()
     {
         try {
-            $code = sysqueue('同步微信用户数据', "xadmin:fansall", 1, [], 0);
+            $code = sysqueue('同步微信用户所有数据', "xadmin:fansall", 1, [], 0);
             $this->success('创建任务成功,请等待完成!', $code);
         } catch (Exception $exception) {
-            $queue = $exception->getData();
-            if (isset($queue['code'])) {
-                $this->success('任务已经存在,无需再次创建!', $queue['code']);
+            $code = $exception->getData();
+            if (is_string($code) && stripos($code, 'Q') === 0) {
+                $this->success('任务已经存在,无需再次创建!', $code);
             } else {
                 $this->error($exception->getMessage());
             }

+ 4 - 4
composer.lock

@@ -909,12 +909,12 @@
             "source": {
                 "type": "git",
                 "url": "https://github.com/zoujingli/ThinkLibrary.git",
-                "reference": "54d332d3a8bd5771dc8f84d052338b1191f260b7"
+                "reference": "6b6a6ff35710f534980ac8f14ee2bfd7531eaa2f"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/zoujingli/ThinkLibrary/zipball/54d332d3a8bd5771dc8f84d052338b1191f260b7",
-                "reference": "54d332d3a8bd5771dc8f84d052338b1191f260b7",
+                "url": "https://api.github.com/repos/zoujingli/ThinkLibrary/zipball/6b6a6ff35710f534980ac8f14ee2bfd7531eaa2f",
+                "reference": "6b6a6ff35710f534980ac8f14ee2bfd7531eaa2f",
                 "shasum": "",
                 "mirrors": [
                     {
@@ -958,7 +958,7 @@
             ],
             "description": "ThinkPHP v6.0 Development Library",
             "homepage": "http://framework.thinkadmin.top",
-            "time": "2020-03-22T14:10:23+00:00"
+            "time": "2020-03-23T03:44:18+00:00"
         },
         {
             "name": "zoujingli/wechat-developer",

+ 30 - 27
public/static/admin.js

@@ -188,7 +188,7 @@ $(function () {
             }
             $.ajax({
                 data: data || {}, type: method || 'GET', url: $.menu.parseUri(url), beforeSend: function (xhr) {
-                    if (typeof Pace === 'object') Pace.restart();
+                    if (typeof Pace === 'object' && loading !== false) Pace.restart();
                     if (typeof headers === 'object') for (var i in headers) xhr.setRequestHeader(i, headers[i]);
                 }, error: function (XMLHttpRequest) {
                     if (XMLHttpRequest.responseText.indexOf('exception') > -1) layer.open({
@@ -746,46 +746,49 @@ $(function () {
         });
     });
     $.loadQueue = function (code) {
-        //自定页
         layer.open({
-            type: 1, title: false, area: [500, 300], anim: 2, shadeClose: false, content: '' +
-                '<div class="padding-30" data-queue-code-loading="' + code + '" style="width:500px;height:300px">' +
-                '   <div class="margin-top-15" data-message-state></div>' +
-                '   <div class="margin-top-15" data-message-title></div>' +
+            type: 1, title: false, area: [500, 270], anim: 2, shadeClose: false, content: '' +
+                '<div class="padding-30" data-queue-load="' + code + '" style="width:500px;height:270px">' +
+                '   <div class="margin-top-15 layui-elip nowrap" data-message-title></div>' +
                 '   <div class="margin-top-15 layui-progress layui-progress-big" lay-showPercent="yes">' +
                 '       <div class="layui-progress-bar" lay-percent="0.00%"></div>' +
                 '   </div>' +
                 '   <textarea class="margin-top-15 layui-textarea transition color-text" disabled style="resize:none;min-height:190px"></textarea>' +
                 '</div>'
         });
-        (function loadprocess(code, $box) {
-            $box = $('[data-queue-code-loading=' + code + ']');
-            if ($box.length < 1) return false;
+        (function loadprocess(code, that) {
+            that = this, this.$box = $('[data-queue-load=' + code + ']');
+            if (that.$box.length < 1) return false;
+            this.setState = function (status, message) {
+                if (status === 1) {
+                    that.$title.html('<b class="color-text">' + message + '</b>');
+                    that.$percent.addClass('layui-bg-blue').removeClass('layui-bg-green layui-bg-red');
+                } else if (status === 2) {
+                    that.$title.html('<b class="color-blue">正在处理:</b>' + message);
+                    that.$percent.addClass('layui-bg-blue').removeClass('layui-bg-green layui-bg-red');
+                } else if (status === 3) {
+                    that.$title.html('<b class="color-green">' + message + '</b>');
+                    that.$percent.addClass('layui-bg-green').removeClass('layui-bg-blue layui-bg-red');
+                } else if (status === 4) {
+                    that.$title.html('<b class="color-red">' + message + '</b>');
+                    that.$percent.addClass('layui-bg-red').removeClass('layui-bg-blue layui-bg-green');
+                }
+            };
+            this.$area = that.$box.find('textarea'), this.$title = that.$box.find('[data-message-title]'), this.$percent = that.$box.find('.layui-progress div');
             $.form.load(window.ROOT_URL + '?s=admin/api.queue/progress', {code: code}, 'post', function (ret) {
                 if (ret.code) {
                     (function (lines) {
                         this.lines = [];
                         for (this.i in lines) {
-                            this.lines.push('[ ' + lines[this.i].progress + '% ] ' + lines[this.i].message);
+                            this.line = lines[this.i], this.percent = '[ ' + this.line.progress + '% ] ';
+                            this.lines.push(this.line.message.indexOf('>>>') > -1 ? this.line.message : this.percent + this.line.message);
                         }
-                        this.$textarea = $box.find('textarea').val(this.lines.join("\n"));
-                        this.$textarea.animate({scrollTop: this.$textarea[0].scrollHeight + 'px'}, 100)
+                        that.$area.val(this.lines.join("\n")), that.$area.animate({scrollTop: that.$area[0].scrollHeight + 'px'}, 50);
                     })(ret.data.history);
-                    $box.find('.layui-progress div').attr('lay-percent', ret.data.progress + '%');
-                    $box.find('[data-message-title]').html(ret.data.message);
-                    layui.element.render();
-                    this.status = parseInt(ret.data.status);
-                    if (this.status === 1) {
-                        $box.find('[data-message-state]').html('处理状态:<b>任务创建成功</b>');
-                    } else if (this.status === 2) {
-                        $box.find('[data-message-state]').html('处理状态:<b class="color-blue">任务正在处理</b>');
-                    } else if (this.status === 3) {
-                        $box.find('[data-message-state]').html('处理状态:<b class="color-green">任务处理完成</b>');
-                        return false;
-                    } else if (this.status === 4) {
-                        $box.find('[data-message-state]').html('处理状态:<b class="color-red">任务处理失败</b>');
-                        return false;
-                    }
+                    that.$percent.attr('lay-percent', (ret.data.progress || '0.00') + '%'), layui.element.render();
+                    if (ret.data.status > 0) that.setState(parseInt(ret.data.status), ret.data.message);
+                    else return that.setState(4, '获取任务详情失败!'), false;
+                    if (parseInt(ret.data.status) === 3 || parseInt(ret.data.status) === 4) return false;
                     return setTimeout(function () {
                         loadprocess(code);
                     }, 200), false;

+ 4 - 4
vendor/composer/installed.json

@@ -935,12 +935,12 @@
         "source": {
             "type": "git",
             "url": "https://github.com/zoujingli/ThinkLibrary.git",
-            "reference": "54d332d3a8bd5771dc8f84d052338b1191f260b7"
+            "reference": "6b6a6ff35710f534980ac8f14ee2bfd7531eaa2f"
         },
         "dist": {
             "type": "zip",
-            "url": "https://api.github.com/repos/zoujingli/ThinkLibrary/zipball/54d332d3a8bd5771dc8f84d052338b1191f260b7",
-            "reference": "54d332d3a8bd5771dc8f84d052338b1191f260b7",
+            "url": "https://api.github.com/repos/zoujingli/ThinkLibrary/zipball/6b6a6ff35710f534980ac8f14ee2bfd7531eaa2f",
+            "reference": "6b6a6ff35710f534980ac8f14ee2bfd7531eaa2f",
             "shasum": "",
             "mirrors": [
                 {
@@ -956,7 +956,7 @@
             "ext-json": "*",
             "topthink/framework": "^6.0"
         },
-        "time": "2020-03-22T14:10:23+00:00",
+        "time": "2020-03-23T03:44:18+00:00",
         "type": "library",
         "extra": {
             "think": {

+ 1 - 1
vendor/services.php

@@ -1,5 +1,5 @@
 <?php 
-// This file is automatically generated at:2020-03-22 22:15:13
+// This file is automatically generated at:2020-03-23 11:55:14
 declare (strict_types = 1);
 return array (
   0 => 'think\\app\\Service',

+ 4 - 2
vendor/zoujingli/think-library/src/Command.php

@@ -56,10 +56,12 @@ class Command extends ThinkCommand
      * @param null|integer $progress 进度数值
      * @return Command
      */
-    protected function setQueuePropress($status = null, $message = null, $progress = null)
+    protected function queueProgressMessage($status = null, $message = null, $progress = null)
     {
         if (defined('WorkQueueCode')) {
             $this->queue->progress(WorkQueueCode, $status, $message, $progress);
+        } elseif (is_string($message)) {
+            $this->output->writeln($message);
         }
         return $this;
     }
@@ -71,7 +73,7 @@ class Command extends ThinkCommand
      * @return Command
      * @throws Exception
      */
-    protected function endQueueMessage($status, $message)
+    protected function queueProgressState($status, $message)
     {
         if (defined('WorkQueueCode')) {
             throw new Exception($message, $status);

+ 1 - 1
vendor/zoujingli/think-library/src/Exception.php

@@ -32,7 +32,7 @@ class Exception extends \Exception
      * Exception constructor.
      * @param string $message
      * @param integer $code
-     * @param array|mixed $data
+     * @param mixed $data
      */
     public function __construct($message = "", $code = 0, $data = [])
     {

+ 1 - 1
vendor/zoujingli/think-library/src/command/queue/ListenQueue.php

@@ -64,7 +64,7 @@ class ListenQueue extends Queue
                     $this->output->error("Execution failed -> [{$vo['code']}] {$vo['title']},{$exception->getMessage()}");
                 }
             });
-            sleep(1);
+            usleep(500000);
         }
     }
 

+ 4 - 4
vendor/zoujingli/think-library/src/command/queue/WorkQueue.php

@@ -74,7 +74,7 @@ class WorkQueue extends Queue
                     'enter_time' => microtime(true), 'attempts' => $this->app->db->raw('attempts+1'),
                     'outer_time' => '0', 'exec_pid' => getmypid(), 'exec_desc' => '', 'status' => '2',
                 ]);
-                QueueService::instance()->progress($this->code, 2, '>>> 任务处理开始', 0);
+                QueueService::instance()->progress($this->code, 2, '>>> 任务处理开始 <<<', 0);
                 // 设置进程标题
                 if ($this->process->iswin()) {
                     $this->setProcessTitle("ThinkAdmin {$this->process->version()} Queue - {$this->queue['title']}");
@@ -121,12 +121,12 @@ class WorkQueue extends Queue
         $this->output->writeln(is_string($message) ? $message : '');
         // 任务进度标记
         if (!empty($desc[0])) {
-            QueueService::instance()->progress($this->code, $status, ">>> {$desc[0]}");
+            QueueService::instance()->progress($this->code, $status, ">>> {$desc[0]} <<<");
         }
         if ($status == 3) {
-            QueueService::instance()->progress($this->code, $status, '>>> 任务处理完成', 100);
+            QueueService::instance()->progress($this->code, $status, '>>> 任务处理完成 <<<', 100);
         } elseif ($status == 4) {
-            QueueService::instance()->progress($this->code, $status, '>>> 任务处理失败');
+            QueueService::instance()->progress($this->code, $status, '>>> 任务处理失败 <<<');
         }
         // 注册循环任务
         if (isset($this->queue['loops_time']) && $this->queue['loops_time'] > 0) {

+ 28 - 27
vendor/zoujingli/think-library/src/service/QueueService.php

@@ -15,10 +15,8 @@
 
 namespace think\admin\service;
 
-use think\admin\Exception;
 use think\admin\extend\CodeExtend;
 use think\admin\Service;
-use think\exception\InvalidArgumentException;
 
 /**
  * 任务基础服务
@@ -56,7 +54,7 @@ class QueueService extends Service
      * 数据初始化
      * @param integer $code
      * @return static
-     * @throws \think\Exception
+     * @throws \think\admin\Exception
      * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
@@ -68,7 +66,7 @@ class QueueService extends Service
             $this->queue = $this->app->db->name('SystemQueue')->where(['code' => $this->code])->find();
             if (empty($this->queue)) {
                 $this->app->log->error("Qeueu initialize failed, Queue {$code} not found.");
-                throw new \think\Exception("Qeueu initialize failed, Queue {$code} not found.");
+                throw new \think\admin\Exception("Qeueu initialize failed, Queue {$code} not found.");
             }
             $this->code = $this->queue['code'];
             $this->title = $this->queue['title'];
@@ -102,7 +100,7 @@ class QueueService extends Service
      * 重发异步任务
      * @param integer $wait 等待时间
      * @return $this
-     * @throws \think\Exception
+     * @throws \think\admin\Exception
      * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
@@ -111,7 +109,7 @@ class QueueService extends Service
     {
         if (empty($this->queue)) {
             $this->app->log->error("Qeueu reset failed, Queue {$this->code} data cannot be empty!");
-            throw new \think\Exception("Qeueu reset failed, Queue {$this->code} data cannot be empty!");
+            throw new \think\admin\Exception("Qeueu reset failed, Queue {$this->code} data cannot be empty!");
         }
         $map = ['code' => $this->code];
         $this->app->db->name('SystemQueue')->where($map)->strict(false)->failException(true)->update([
@@ -121,16 +119,16 @@ class QueueService extends Service
     }
 
     /**
-     * 添加清理7天前的记录及超时任务
-     * @throws Exception
-     * @throws \think\Exception
+     * 添加清理定时清理任务
+     * @return $this
+     * @throws \think\admin\Exception
      * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
      */
     public function addCleanQueue()
     {
-        $this->register('清理7天前记录及执行超时的任务', "xtask:clean", 0, [], 0, 3600);
+        return $this->register('清理7天前记录及执行超时的任务', "xtask:clean", 0, [], 0, 3600);
     }
 
     /**
@@ -142,8 +140,7 @@ class QueueService extends Service
      * @param integer $rscript 任务类型(0单例,1多例)
      * @param integer $loops 循环等待时间
      * @return $this
-     * @throws Exception
-     * @throws \think\Exception
+     * @throws \think\admin\Exception
      * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
@@ -152,7 +149,7 @@ class QueueService extends Service
     {
         $map = [['title', '=', $title], ['status', 'in', ['1', '2']]];
         if (empty($rscript) && ($queue = $this->app->db->name('SystemQueue')->where($map)->find())) {
-            throw new Exception(lang('think_library_queue_exist'), 0, $queue);
+            throw new \think\admin\Exception(lang('think_library_queue_exist'), 0, $queue['code']);
         }
         $this->app->db->name('SystemQueue')->strict(false)->failException(true)->insert([
             'code'       => $this->code = CodeExtend::uniqidDate(16, 'Q'),
@@ -166,12 +163,12 @@ class QueueService extends Service
             'outer_time' => '0',
             'loops_time' => $loops,
         ]);
-        $this->progress($this->code, 1, '>>> 任务创建成功', 0.00);
+        $this->progress($this->code, 1, '>>> 任务创建成功 <<<', 0.00);
         return $this->initialize($this->code);
     }
 
     /**
-     * 更新任务进度信息
+     * 设置任务进度信息
      * @param string $code 任务编号
      * @param null|integer $status 任务状态
      * @param null|string $message 进度消息
@@ -180,17 +177,24 @@ class QueueService extends Service
      */
     public function progress($code, $status = null, $message = null, $progress = null)
     {
+        if (is_numeric($status) && intval($status) === 3) {
+            if (!is_numeric($progress)) $progress = '100.00';
+            if (is_null($message)) $message = '>>> 任务已经完成 <<<';
+        }
+        if (is_numeric($status) && intval($status) === 4) {
+            if (!is_numeric($progress)) $progress = '0.00';
+            if (is_null($message)) $message = '>>> 任务执行失败 <<<';
+        }
         $ckey = "queue_{$code}_progress";
-        $data = $this->app->cache->get($ckey, [
-            'code'     => $code,
-            'status'   => $status,
-            'message'  => $message,
-            'progress' => $progress,
-            'history'  => [],
-        ]);
-        if (is_numeric($progress)) {
-            $progress = sprintf("%.2f", $progress);
+        try {
+            $data = $this->app->cache->get($ckey, [
+                'code' => $code, 'status' => $status, 'message' => $message, 'progress' => $progress, 'history' => [],
+            ]);
+        } catch (\Exception|\TypeError $exception) {
+            return $this->progress($code, $status, $message, $progress);
         }
+        if (is_numeric($status)) $data['status'] = intval($status);
+        if (is_numeric($progress)) $progress = sprintf("%.2f", $progress);
         if (is_string($message) && is_null($progress)) {
             $data['message'] = $message;
             $data['history'][] = ['message' => $message, 'progress' => $data['progress']];
@@ -202,9 +206,6 @@ class QueueService extends Service
             $data['progress'] = $progress;
             $data['history'][] = ['message' => $message, 'progress' => $progress];
         }
-        if (is_numeric($status)) {
-            $data['status'] = intval($status);
-        }
         if (is_string($message) || is_numeric($progress)) {
             $this->app->cache->set($ckey, $data);
         }