ComposerUpdate

This commit is contained in:
Anyon 2020-03-23 12:03:48 +08:00
parent 55101d845e
commit 639483edd9
13 changed files with 91 additions and 90 deletions

View File

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

View File

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

View File

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

View File

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

8
composer.lock generated
View File

@ -909,12 +909,12 @@
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/zoujingli/ThinkLibrary.git", "url": "https://github.com/zoujingli/ThinkLibrary.git",
"reference": "54d332d3a8bd5771dc8f84d052338b1191f260b7" "reference": "6b6a6ff35710f534980ac8f14ee2bfd7531eaa2f"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/zoujingli/ThinkLibrary/zipball/54d332d3a8bd5771dc8f84d052338b1191f260b7", "url": "https://api.github.com/repos/zoujingli/ThinkLibrary/zipball/6b6a6ff35710f534980ac8f14ee2bfd7531eaa2f",
"reference": "54d332d3a8bd5771dc8f84d052338b1191f260b7", "reference": "6b6a6ff35710f534980ac8f14ee2bfd7531eaa2f",
"shasum": "", "shasum": "",
"mirrors": [ "mirrors": [
{ {
@ -958,7 +958,7 @@
], ],
"description": "ThinkPHP v6.0 Development Library", "description": "ThinkPHP v6.0 Development Library",
"homepage": "http://framework.thinkadmin.top", "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", "name": "zoujingli/wechat-developer",

View File

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

View File

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

2
vendor/services.php vendored
View File

@ -1,5 +1,5 @@
<?php <?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); declare (strict_types = 1);
return array ( return array (
0 => 'think\\app\\Service', 0 => 'think\\app\\Service',

View File

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

View File

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

View File

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

View File

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

View File

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