From 2cf6b420032135b9750b59c97a3e47085084016e Mon Sep 17 00:00:00 2001 From: Anyon Date: Mon, 11 May 2020 16:41:39 +0800 Subject: [PATCH] ComposerUpdate --- app/admin/controller/api/Plugs.php | 2 +- composer.lock | 8 +- vendor/composer/autoload_classmap.php | 11 +- vendor/composer/autoload_static.php | 11 +- vendor/composer/installed.json | 8 +- vendor/services.php | 2 +- .../zoujingli/think-library/src/Library.php | 13 +- .../think-library/src/command/Database.php | 104 +++++++ .../think-library/src/command/Queue.php | 269 ++++++++++++++++++ .../think-library/src/command/Version.php | 6 +- .../src/command/database/Optimize.php | 59 ---- .../src/command/database/Repair.php | 58 ---- .../src/command/queue/CleanQueue.php | 77 ----- .../src/command/queue/ListenQueue.php | 101 ------- .../src/command/queue/QueryQueue.php | 51 ---- .../src/command/queue/StartQueue.php | 63 ---- .../src/command/queue/StateQueue.php | 51 ---- .../src/command/queue/StopQueue.php | 53 ---- .../src/command/queue/WorkQueue.php | 141 --------- .../src/service/QueueService.php | 2 +- 20 files changed, 395 insertions(+), 695 deletions(-) create mode 100644 vendor/zoujingli/think-library/src/command/Database.php create mode 100644 vendor/zoujingli/think-library/src/command/Queue.php delete mode 100644 vendor/zoujingli/think-library/src/command/database/Optimize.php delete mode 100644 vendor/zoujingli/think-library/src/command/database/Repair.php delete mode 100644 vendor/zoujingli/think-library/src/command/queue/CleanQueue.php delete mode 100644 vendor/zoujingli/think-library/src/command/queue/ListenQueue.php delete mode 100644 vendor/zoujingli/think-library/src/command/queue/QueryQueue.php delete mode 100644 vendor/zoujingli/think-library/src/command/queue/StartQueue.php delete mode 100644 vendor/zoujingli/think-library/src/command/queue/StateQueue.php delete mode 100644 vendor/zoujingli/think-library/src/command/queue/StopQueue.php delete mode 100644 vendor/zoujingli/think-library/src/command/queue/WorkQueue.php diff --git a/app/admin/controller/api/Plugs.php b/app/admin/controller/api/Plugs.php index f1bf718fb..2b557e3f5 100644 --- a/app/admin/controller/api/Plugs.php +++ b/app/admin/controller/api/Plugs.php @@ -107,7 +107,7 @@ class Plugs extends Controller public function optimize() { if (AdminService::instance()->isSuper()) { - $this->_queue('优化数据库所有数据表', 'xadmin:dbOptimize', 0, [], 0, 0); + $this->_queue('优化数据库所有数据表', 'xadmin:database optimize', 0, [], 0, 0); } else { $this->error('只有超级管理员才能操作!'); } diff --git a/composer.lock b/composer.lock index 287c7b6f4..db394a969 100644 --- a/composer.lock +++ b/composer.lock @@ -909,12 +909,12 @@ "source": { "type": "git", "url": "https://github.com/zoujingli/ThinkLibrary.git", - "reference": "43dcb252b35e954ea14577de75919022471a426a" + "reference": "069ed2ae0ce1c144ea7349938b2a017433b8561d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/zoujingli/ThinkLibrary/zipball/43dcb252b35e954ea14577de75919022471a426a", - "reference": "43dcb252b35e954ea14577de75919022471a426a", + "url": "https://api.github.com/repos/zoujingli/ThinkLibrary/zipball/069ed2ae0ce1c144ea7349938b2a017433b8561d", + "reference": "069ed2ae0ce1c144ea7349938b2a017433b8561d", "shasum": "", "mirrors": [ { @@ -958,7 +958,7 @@ ], "description": "ThinkPHP v6.0 Development Library", "homepage": "http://framework.thinkadmin.top", - "time": "2020-05-11T02:31:16+00:00" + "time": "2020-05-11T08:36:34+00:00" }, { "name": "zoujingli/wechat-developer", diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index 9264c7e2f..55a7f541a 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -255,17 +255,10 @@ return array( 'think\\admin\\Queue' => $vendorDir . '/zoujingli/think-library/src/Queue.php', 'think\\admin\\Service' => $vendorDir . '/zoujingli/think-library/src/Service.php', 'think\\admin\\Storage' => $vendorDir . '/zoujingli/think-library/src/Storage.php', + 'think\\admin\\command\\Database' => $vendorDir . '/zoujingli/think-library/src/command/Database.php', 'think\\admin\\command\\Install' => $vendorDir . '/zoujingli/think-library/src/command/Install.php', + 'think\\admin\\command\\Queue' => $vendorDir . '/zoujingli/think-library/src/command/Queue.php', 'think\\admin\\command\\Version' => $vendorDir . '/zoujingli/think-library/src/command/Version.php', - 'think\\admin\\command\\database\\Optimize' => $vendorDir . '/zoujingli/think-library/src/command/database/Optimize.php', - 'think\\admin\\command\\database\\Repair' => $vendorDir . '/zoujingli/think-library/src/command/database/Repair.php', - 'think\\admin\\command\\queue\\CleanQueue' => $vendorDir . '/zoujingli/think-library/src/command/queue/CleanQueue.php', - 'think\\admin\\command\\queue\\ListenQueue' => $vendorDir . '/zoujingli/think-library/src/command/queue/ListenQueue.php', - 'think\\admin\\command\\queue\\QueryQueue' => $vendorDir . '/zoujingli/think-library/src/command/queue/QueryQueue.php', - 'think\\admin\\command\\queue\\StartQueue' => $vendorDir . '/zoujingli/think-library/src/command/queue/StartQueue.php', - 'think\\admin\\command\\queue\\StateQueue' => $vendorDir . '/zoujingli/think-library/src/command/queue/StateQueue.php', - 'think\\admin\\command\\queue\\StopQueue' => $vendorDir . '/zoujingli/think-library/src/command/queue/StopQueue.php', - 'think\\admin\\command\\queue\\WorkQueue' => $vendorDir . '/zoujingli/think-library/src/command/queue/WorkQueue.php', 'think\\admin\\extend\\CodeExtend' => $vendorDir . '/zoujingli/think-library/src/extend/CodeExtend.php', 'think\\admin\\extend\\DataExtend' => $vendorDir . '/zoujingli/think-library/src/extend/DataExtend.php', 'think\\admin\\extend\\ExcelExtend' => $vendorDir . '/zoujingli/think-library/src/extend/ExcelExtend.php', diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index 0b0423f0e..7e3d564fd 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -388,17 +388,10 @@ class ComposerStaticInitf41e9df38a61a147f539b835fbd021f0 'think\\admin\\Queue' => __DIR__ . '/..' . '/zoujingli/think-library/src/Queue.php', 'think\\admin\\Service' => __DIR__ . '/..' . '/zoujingli/think-library/src/Service.php', 'think\\admin\\Storage' => __DIR__ . '/..' . '/zoujingli/think-library/src/Storage.php', + 'think\\admin\\command\\Database' => __DIR__ . '/..' . '/zoujingli/think-library/src/command/Database.php', 'think\\admin\\command\\Install' => __DIR__ . '/..' . '/zoujingli/think-library/src/command/Install.php', + 'think\\admin\\command\\Queue' => __DIR__ . '/..' . '/zoujingli/think-library/src/command/Queue.php', 'think\\admin\\command\\Version' => __DIR__ . '/..' . '/zoujingli/think-library/src/command/Version.php', - 'think\\admin\\command\\database\\Optimize' => __DIR__ . '/..' . '/zoujingli/think-library/src/command/database/Optimize.php', - 'think\\admin\\command\\database\\Repair' => __DIR__ . '/..' . '/zoujingli/think-library/src/command/database/Repair.php', - 'think\\admin\\command\\queue\\CleanQueue' => __DIR__ . '/..' . '/zoujingli/think-library/src/command/queue/CleanQueue.php', - 'think\\admin\\command\\queue\\ListenQueue' => __DIR__ . '/..' . '/zoujingli/think-library/src/command/queue/ListenQueue.php', - 'think\\admin\\command\\queue\\QueryQueue' => __DIR__ . '/..' . '/zoujingli/think-library/src/command/queue/QueryQueue.php', - 'think\\admin\\command\\queue\\StartQueue' => __DIR__ . '/..' . '/zoujingli/think-library/src/command/queue/StartQueue.php', - 'think\\admin\\command\\queue\\StateQueue' => __DIR__ . '/..' . '/zoujingli/think-library/src/command/queue/StateQueue.php', - 'think\\admin\\command\\queue\\StopQueue' => __DIR__ . '/..' . '/zoujingli/think-library/src/command/queue/StopQueue.php', - 'think\\admin\\command\\queue\\WorkQueue' => __DIR__ . '/..' . '/zoujingli/think-library/src/command/queue/WorkQueue.php', 'think\\admin\\extend\\CodeExtend' => __DIR__ . '/..' . '/zoujingli/think-library/src/extend/CodeExtend.php', 'think\\admin\\extend\\DataExtend' => __DIR__ . '/..' . '/zoujingli/think-library/src/extend/DataExtend.php', 'think\\admin\\extend\\ExcelExtend' => __DIR__ . '/..' . '/zoujingli/think-library/src/extend/ExcelExtend.php', diff --git a/vendor/composer/installed.json b/vendor/composer/installed.json index de3a31dda..9dca4f1ef 100644 --- a/vendor/composer/installed.json +++ b/vendor/composer/installed.json @@ -935,12 +935,12 @@ "source": { "type": "git", "url": "https://github.com/zoujingli/ThinkLibrary.git", - "reference": "43dcb252b35e954ea14577de75919022471a426a" + "reference": "069ed2ae0ce1c144ea7349938b2a017433b8561d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/zoujingli/ThinkLibrary/zipball/43dcb252b35e954ea14577de75919022471a426a", - "reference": "43dcb252b35e954ea14577de75919022471a426a", + "url": "https://api.github.com/repos/zoujingli/ThinkLibrary/zipball/069ed2ae0ce1c144ea7349938b2a017433b8561d", + "reference": "069ed2ae0ce1c144ea7349938b2a017433b8561d", "shasum": "", "mirrors": [ { @@ -956,7 +956,7 @@ "ext-json": "*", "topthink/framework": "^6.0" }, - "time": "2020-05-11T02:31:16+00:00", + "time": "2020-05-11T08:36:34+00:00", "type": "library", "extra": { "think": { diff --git a/vendor/services.php b/vendor/services.php index aca7beac2..e11ad0c49 100644 --- a/vendor/services.php +++ b/vendor/services.php @@ -1,5 +1,5 @@ 'think\\app\\Service', diff --git a/vendor/zoujingli/think-library/src/Library.php b/vendor/zoujingli/think-library/src/Library.php index e83ec4927..473ac085f 100644 --- a/vendor/zoujingli/think-library/src/Library.php +++ b/vendor/zoujingli/think-library/src/Library.php @@ -84,19 +84,10 @@ class Library extends Service SystemService::instance()->bindRuntime(); // 注册系统任务指令 $this->commands([ + 'think\admin\command\Queue', 'think\admin\command\Install', 'think\admin\command\Version', - // 系统异步任务指令 - 'think\admin\command\queue\CleanQueue', - 'think\admin\command\queue\WorkQueue', - 'think\admin\command\queue\StopQueue', - 'think\admin\command\queue\StateQueue', - 'think\admin\command\queue\StartQueue', - 'think\admin\command\queue\QueryQueue', - 'think\admin\command\queue\ListenQueue', - // 数据库表优化指令 - 'think\admin\command\database\Optimize', - 'think\admin\command\database\Repair', + 'think\admin\command\Database', ]); } } \ No newline at end of file diff --git a/vendor/zoujingli/think-library/src/command/Database.php b/vendor/zoujingli/think-library/src/command/Database.php new file mode 100644 index 000000000..7486bbd76 --- /dev/null +++ b/vendor/zoujingli/think-library/src/command/Database.php @@ -0,0 +1,104 @@ +setName('xadmin:database'); + $this->addArgument('action', Argument::OPTIONAL, 'repair|optimize', 'optimize'); + $this->setDescription('Optimize Or Repair Database for ThinkAdmin'); + } + + /** + * @param Input $input + * @param Output $output + * @return mixed + */ + public function execute(Input $input, Output $output) + { + $action = $input->getArgument('action'); + if (in_array($action, ['repair', 'optimize'])) { + return $this->${"_{$action}"}(); + } + $this->output->error("Wrong operation, currently allow repair|optimize"); + } + + /** + * 修复数据表 + * @throws \think\admin\Exception + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + protected function _repair() + { + $this->setQueueProgress("正在获取需要修复的数据表", 0); + $tables = $this->getTables(); + [$total, $used] = [count($tables), 0]; + $this->setQueueProgress("总共需要修复 {$total} 张数据表", 0); + foreach ($tables as $table) { + $stridx = str_pad(++$used, strlen("{$total}"), '0', STR_PAD_LEFT) . "/{$total}"; + $this->setQueueProgress("[{$stridx}] 正在修复数据表 {$table}", $used / $total * 100); + $this->app->db->query("REPAIR TABLE `{$table}`"); + } + } + + /** + * 优化所有数据表 + * @throws \think\admin\Exception + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + protected function _optimize() + { + $this->setQueueProgress("正在获取需要优化的数据表", 0); + $tables = $this->getTables(); + [$total, $used] = [count($tables), 0]; + $this->setQueueProgress("总共需要优化 {$total} 张数据表", 0); + foreach ($tables as $table) { + $stridx = str_pad(++$used, strlen("{$total}"), '0', STR_PAD_LEFT) . "/{$total}"; + $this->setQueueProgress("[{$stridx}] 正在优化数据表 {$table}", $used / $total * 100); + $this->app->db->query("OPTIMIZE TABLE `{$table}`"); + } + } + + /** + * 获取数据库的数据表 + * @return array + */ + protected function getTables() + { + $tables = []; + foreach ($this->app->db->query("show tables") as $item) { + $tables = array_merge($tables, array_values($item)); + } + return $tables; + } + +} \ No newline at end of file diff --git a/vendor/zoujingli/think-library/src/command/Queue.php b/vendor/zoujingli/think-library/src/command/Queue.php new file mode 100644 index 000000000..5289b8077 --- /dev/null +++ b/vendor/zoujingli/think-library/src/command/Queue.php @@ -0,0 +1,269 @@ +setName('xadmin:queue'); + $this->addArgument('action', Argument::OPTIONAL, 'stop|start|status|query|listen|clean|dorun', 'listen'); + $this->addArgument('code', Argument::OPTIONAL, 'Taskcode'); + $this->addOption('daemon', 'd', Option::VALUE_NONE, 'Run the queue listen in daemon mode'); + $this->setDescription('Asynchronous Command Queue for ThinkAdmin'); + } + + /** + * 执行指令内容 + * @param Input $input + * @param Output $output + * @return mixed + */ + public function execute(Input $input, Output $output) + { + $action = $this->input->hasOption('daemon') ? 'start' : $input->getArgument('action'); + if (method_exists($this, $method = "{$action}Action")) return $this->$method(); + $this->output->error("Wrong operation, currently allow stop|start|status|query|listen|clean|dorun"); + } + + /** + * 停止所有任务 + */ + protected function stopAction() + { + $keyword = $this->process->think('xadmin:queue'); + if (count($result = $this->process->query($keyword)) < 1) { + $this->output->warning("There is no task process to finish"); + } else foreach ($result as $item) { + $this->process->close($item['pid']); + $this->output->info("Sending end process {$item['pid']} signal succeeded"); + } + } + + /** + * 启动后台任务 + */ + protected function startAction() + { + $this->app->db->name($this->table)->count(); + $command = $this->process->think("xadmin:queue listen"); + if (count($result = $this->process->query($command)) > 0) { + $this->output->info("Listening main process {$result['0']['pid']} has started"); + } else { + [$this->process->create($command), sleep(1)]; + if (count($result = $this->process->query($command)) > 0) { + $this->output->info("Listening main process {$result['0']['pid']} started successfully"); + } else { + $this->output->error('Failed to create listening main process'); + } + } + } + + /** + * 查询所有任务 + */ + protected function queryAction() + { + $result = $this->process->query($this->process->think("xadmin:queue")); + if (count($result) > 0) foreach ($result as $item) { + $this->output->writeln("{$item['pid']}\t{$item['cmd']}"); + } else { + $this->output->writeln('No related task process found'); + } + } + + /** + * 清理所有任务 + * @throws \think\admin\Exception + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + protected function cleanAction() + { + $map = [['exec_time', '<', time() - 7 * 24 * 3600]]; + $count1 = $this->app->db->name($this->table)->where($map)->delete(); + $this->setQueueProgress("清理 {$count1} 条历史任务成功"); + // 重置超60分钟无响应的记录 + $map = [['exec_time', '<', time() - 3600], ['status', '=', '2']]; + $count2 = $this->app->db->name($this->table)->where($map)->update([ + 'status' => '4', 'exec_desc' => '任务执行超时,已自动标识为失败!', + ]); + $this->setQueueProgress("处理 {$count2} 条超时间任务成功", 100); + } + + /** + * 查询兼听状态 + */ + protected function statusAction() + { + $command = $this->process->think('xadmin:queue listen'); + if (count($result = $this->process->query($command)) > 0) { + $this->output->info("Listening for main process {$result[0]['pid']} running"); + } else { + $this->output->warning("The Listening main process is not running"); + } + } + + /** + * 立即监听任务 + */ + protected function listenAction() + { + set_time_limit(0); + $this->app->db->name($this->table)->count(); + if ($this->process->iswin()) { + $this->setProcessTitle("ThinkAdmin {$this->process->version()} Queue Listen"); + } + $this->output->writeln("\tYou can exit with `CTRL-C`"); + $this->output->writeln('============== LISTENING =============='); + while (true) { + $where = [['status', '=', '1'], ['exec_time', '<=', time()]]; + $this->app->db->name($this->table)->where($where)->order('exec_time asc')->chunk(100, function (Collection $result) { + foreach ($result->toArray() as $vo) try { + $command = $this->process->think("xadmin:queue dorun {$vo['code']} -"); + if (count($this->process->query($command)) > 0) { + $this->output->writeln("Already in progress -> [{$vo['code']}] {$vo['title']}"); + } else { + $this->process->create($command); + $this->output->writeln("Created new process -> [{$vo['code']}] {$vo['title']}"); + } + } catch (\Exception $exception) { + $this->app->db->name($this->table)->where(['code' => $vo['code']])->update([ + 'status' => '4', 'outer_time' => time(), 'exec_desc' => $exception->getMessage(), + ]); + $this->output->error("Execution failed -> [{$vo['code']}] {$vo['title']},{$exception->getMessage()}"); + } + }); + usleep(500000); + } + } + + /** + * 执行任务内容 + * @throws \think\db\exception\DbException + */ + protected function dorunAction() + { + set_time_limit(0); + $this->code = trim($this->input->getArgument('code')); + if (empty($this->code)) { + $this->output->error('Task number needs to be specified for task execution'); + } else try { + $this->queue->initialize($this->code); + if (empty($this->queue->record) || intval($this->queue->record['status']) !== 1) { + // 这里不做任何处理(该任务可能在其它地方已经在执行) + $this->output->warning($message = "The or status of task {$this->code} is abnormal"); + } else { + // 锁定任务状态,防止任务再次被执行 + $this->app->db->name($this->table)->strict(false)->where(['code' => $this->code])->update([ + 'enter_time' => microtime(true), 'attempts' => $this->app->db->raw('attempts+1'), + 'outer_time' => '0', 'exec_pid' => getmypid(), 'exec_desc' => '', 'status' => '2', + ]); + $this->queue->progress(2, '>>> 任务处理开始 <<<', 0); + // 设置进程标题 + if ($this->process->iswin()) { + $this->setProcessTitle("ThinkAdmin {$this->process->version()} Queue - {$this->queue->title}"); + } + // 执行任务内容 + defined('WorkQueueCall') or define('WorkQueueCall', true); + defined('WorkQueueCode') or define('WorkQueueCode', $this->code); + if (class_exists($command = $this->queue->record['command'])) { + // 自定义任务,支持返回消息(支持异常结束,异常码可选择 3|4 设置任务状态) + $class = $this->app->make($command, [], true); + if ($class instanceof \think\admin\Queue) { + $this->update(3, $class->initialize($this->queue)->execute($this->queue->data)); + } elseif ($class instanceof \think\admin\service\QueueService) { + $this->update(3, $class->initialize($this->queue->code)->execute($this->queue->data)); + } else { + throw new \think\admin\Exception("自定义 {$command} 未继承 Queue 或 QueueService"); + } + } else { + // 自定义指令,不支持返回消息(支持异常结束,异常码可选择 3|4 设置任务状态) + $attr = explode(' ', trim(preg_replace('|\s+|', ' ', $this->queue->record['command']))); + $this->update(3, $this->app->console->call(array_shift($attr), $attr)->fetch(), false); + } + } + } catch (\Exception|\Error $exception) { + $code = $exception->getCode(); + if (intval($code) !== 3) $code = 4; + $this->update($code, $exception->getMessage()); + } + } + + /** + * 修改当前任务状态 + * @param integer $status 任务状态 + * @param string $message 消息内容 + * @param boolean $issplit 是否分隔 + * @throws \think\db\exception\DbException + */ + protected function update($status, $message, $issplit = true) + { + // 更新当前任务 + $info = trim(is_string($message) ? $message : ''); + $desc = $issplit ? explode("\n", $info) : [$message]; + $this->app->db->name($this->table)->strict(false)->where(['code' => $this->code])->update([ + 'status' => $status, 'outer_time' => microtime(true), 'exec_pid' => getmypid(), 'exec_desc' => $desc[0], + ]); + $this->output->writeln(is_string($message) ? $message : ''); + // 任务进度标记 + if (!empty($desc[0])) { + $this->queue->progress($status, ">>> {$desc[0]} <<<"); + } + if ($status == 3) { + $this->queue->progress($status, '>>> 任务处理完成 <<<', 100); + } elseif ($status == 4) { + $this->queue->progress($status, '>>> 任务处理失败 <<<'); + } + // 注册循环任务 + if (isset($this->queue->record['loops_time']) && $this->queue->record['loops_time'] > 0) { + try { + $this->queue->initialize($this->code)->reset($this->queue->record['loops_time']); + } catch (\Exception|\Error $exception) { + $this->app->log->error("Queue {$this->queue->record['code']} Loops Failed. {$exception->getMessage()}"); + } + } + } +} \ No newline at end of file diff --git a/vendor/zoujingli/think-library/src/command/Version.php b/vendor/zoujingli/think-library/src/command/Version.php index 3842cf160..4ed0ab42a 100644 --- a/vendor/zoujingli/think-library/src/command/Version.php +++ b/vendor/zoujingli/think-library/src/command/Version.php @@ -20,7 +20,7 @@ use think\console\Input; use think\console\Output; /** - * 获取框架版本号 + * 框架版本号指令 * Class Version * @package think\admin\command */ @@ -32,6 +32,10 @@ class Version extends Command $this->setDescription("Query application framework version"); } + /** + * @param Input $input + * @param Output $output + */ protected function execute(Input $input, Output $output) { $output->writeln('ThinkLib ' . $this->process->version()); diff --git a/vendor/zoujingli/think-library/src/command/database/Optimize.php b/vendor/zoujingli/think-library/src/command/database/Optimize.php deleted file mode 100644 index 1c5d98702..000000000 --- a/vendor/zoujingli/think-library/src/command/database/Optimize.php +++ /dev/null @@ -1,59 +0,0 @@ -setName('xadmin:dbOptimize'); - $this->setDescription("Attempt to optimize all data tables"); - } - - /** - * @param Input $input - * @param Output $output - * @throws \think\admin\Exception - * @throws \think\db\exception\DataNotFoundException - * @throws \think\db\exception\DbException - * @throws \think\db\exception\ModelNotFoundException - */ - protected function execute(Input $input, Output $output) - { - $tables = []; - $this->setQueueProgress("正在获取需要优化的数据表", 0); - foreach ($this->app->db->query("show tables") as $item) { - $tables = array_merge($tables, array_values($item)); - } - [$total, $used] = [count($tables), 0]; - $this->setQueueProgress("总共需要优化 {$total} 张数据表", 0); - foreach ($tables as $table) { - $stridx = str_pad(++$used, strlen("{$total}"), '0', STR_PAD_LEFT) . "/{$total}"; - $this->setQueueProgress("[{$stridx}] 正在优化数据表 {$table}", $used / $total * 100); - $this->app->db->query("OPTIMIZE TABLE `{$table}`"); - } - } - -} \ No newline at end of file diff --git a/vendor/zoujingli/think-library/src/command/database/Repair.php b/vendor/zoujingli/think-library/src/command/database/Repair.php deleted file mode 100644 index 98e964a99..000000000 --- a/vendor/zoujingli/think-library/src/command/database/Repair.php +++ /dev/null @@ -1,58 +0,0 @@ -setName('xadmin:dbRepair'); - $this->setDescription("Attempt to repair all data tables"); - } - - /** - * @param Input $input - * @param Output $output - * @throws \think\admin\Exception - * @throws \think\db\exception\DataNotFoundException - * @throws \think\db\exception\DbException - * @throws \think\db\exception\ModelNotFoundException - */ - protected function execute(Input $input, Output $output) - { - $tables = []; - $this->setQueueProgress("正在获取需要修复的数据表", 0); - foreach ($this->app->db->query("show tables") as $item) { - $tables = array_merge($tables, array_values($item)); - } - [$total, $used] = [count($tables), 0]; - $this->setQueueProgress("总共需要修复 {$total} 张数据表", 0); - foreach ($tables as $table) { - $stridx = str_pad(++$used, strlen("{$total}"), '0', STR_PAD_LEFT) . "/{$total}"; - $this->setQueueProgress("[{$stridx}] 正在修复数据表 {$table}", $used / $total * 100); - $this->app->db->query("REPAIR TABLE `{$table}`"); - } - } -} \ No newline at end of file diff --git a/vendor/zoujingli/think-library/src/command/queue/CleanQueue.php b/vendor/zoujingli/think-library/src/command/queue/CleanQueue.php deleted file mode 100644 index cad7454f6..000000000 --- a/vendor/zoujingli/think-library/src/command/queue/CleanQueue.php +++ /dev/null @@ -1,77 +0,0 @@ -setName('xtask:clean')->setDescription('Clean up historical task records'); - $this->addArgument('time', Argument::OPTIONAL, 'BeforeTime', 7 * 24 * 3600); - } - - /** - * 清理历史任务 - * @param Input $input - * @param Output $output - * @throws \think\admin\Exception - * @throws \think\db\exception\DataNotFoundException - * @throws \think\db\exception\DbException - * @throws \think\db\exception\ModelNotFoundException - */ - protected function execute(Input $input, Output $output) - { - $this->time = $input->getArgument('time'); - if (empty($this->time) || !is_numeric($this->time) || $this->time <= 0) { - $this->setQueueError("参数错误,需要传入任务超时时间"); - } else { - $map = [['exec_time', '<', time() - $this->time]]; - $count1 = $this->app->db->name($this->table)->where($map)->delete(); - $this->setQueueProgress("清理 {$count1} 条历史任务成功", 50); - // 重置超60分钟无响应的记录 - $map = [['exec_time', '<', time() - 3600], ['status', '=', '2']]; - $count2 = $this->app->db->name($this->table)->where($map)->update([ - 'status' => '4', 'exec_desc' => '任务执行超时,已自动标识为失败!', - ]); - $this->setQueueProgress("处理 {$count2} 条超时间任务成功", 100); - } - } -} \ No newline at end of file diff --git a/vendor/zoujingli/think-library/src/command/queue/ListenQueue.php b/vendor/zoujingli/think-library/src/command/queue/ListenQueue.php deleted file mode 100644 index 11b7e5c23..000000000 --- a/vendor/zoujingli/think-library/src/command/queue/ListenQueue.php +++ /dev/null @@ -1,101 +0,0 @@ -setName('xtask:listen')->setDescription('Start task listening main process'); - } - - /** - * 初始化任务监听指令 - * @param Input $input - * @param Output $output - */ - protected function initialize(Input $input, Output $output) - { - parent::initialize($input, $output); - $this->app->db->setLog(new NullLogger()); - } - - /** - * 启动进程守护监听 - * @param Input $input 输入对象 - * @param Output $output 输出对象 - */ - protected function execute(Input $input, Output $output) - { - set_time_limit(0); - $this->app->db->name($this->table)->count(); - if ($this->process->iswin()) { - $this->setProcessTitle("ThinkAdmin {$this->process->version()} Queue Listen"); - } - $output->writeln('============ LISTENING ============'); - while (true) { - $where = [['status', '=', '1'], ['exec_time', '<=', time()]]; - $this->app->db->name($this->table)->where($where)->order('exec_time asc')->chunk(100, function (Collection $result) { - foreach ($result->toArray() as $vo) try { - $command = $this->process->think("xtask:_work {$vo['code']} -"); - if (count($this->process->query($command)) > 0) { - $this->output->writeln("Already in progress -> [{$vo['code']}] {$vo['title']}"); - } else { - $this->process->create($command); - $this->output->writeln("Created new process -> [{$vo['code']}] {$vo['title']}"); - } - } catch (\Exception $exception) { - $this->update($vo['code'], ['status' => '4', 'outer_time' => time(), 'exec_desc' => $exception->getMessage()]); - $this->output->error("Execution failed -> [{$vo['code']}] {$vo['title']},{$exception->getMessage()}"); - } - }); - usleep(500000); - } - } - - /** - * 更新任务数据 - * @param mixed $code 任务编号 - * @param mixed $data 任务数据 - * @return boolean - * @throws \think\db\exception\DbException - */ - protected function update($code, array $data = []) - { - return $this->app->db->name($this->table)->where(['code' => $code])->update($data); - } - -} diff --git a/vendor/zoujingli/think-library/src/command/queue/QueryQueue.php b/vendor/zoujingli/think-library/src/command/queue/QueryQueue.php deleted file mode 100644 index 150670075..000000000 --- a/vendor/zoujingli/think-library/src/command/queue/QueryQueue.php +++ /dev/null @@ -1,51 +0,0 @@ -setName('xtask:query')->setDescription('Query all running task processes'); - } - - /** - * 执行相关进程查询 - * @param Input $input 输入对象 - * @param Output $output 输出对象 - */ - protected function execute(Input $input, Output $output) - { - $result = $this->process->query($this->process->think("xtask:")); - if (count($result) > 0) foreach ($result as $item) { - $output->writeln("{$item['pid']}\t{$item['cmd']}"); - } else { - $output->writeln('No related task process found'); - } - } -} diff --git a/vendor/zoujingli/think-library/src/command/queue/StartQueue.php b/vendor/zoujingli/think-library/src/command/queue/StartQueue.php deleted file mode 100644 index cc46d4523..000000000 --- a/vendor/zoujingli/think-library/src/command/queue/StartQueue.php +++ /dev/null @@ -1,63 +0,0 @@ -setName('xtask:start')->setDescription('Create daemons to listening main process'); - } - - /** - * 执行启动操作 - * @param Input $input 输入对象 - * @param Output $output 输出对象 - */ - protected function execute(Input $input, Output $output) - { - $this->app->db->name($this->table)->count(); - $command = $this->process->think("xtask:listen"); - if (count($result = $this->process->query($command)) > 0) { - $output->info("Listening main process {$result['0']['pid']} has started"); - } else { - [$this->process->create($command), sleep(1)]; - if (count($result = $this->process->query($command)) > 0) { - $output->info("Listening main process {$result['0']['pid']} started successfully"); - } else { - $output->error('Failed to create listening main process'); - } - } - } -} diff --git a/vendor/zoujingli/think-library/src/command/queue/StateQueue.php b/vendor/zoujingli/think-library/src/command/queue/StateQueue.php deleted file mode 100644 index 614ab8d98..000000000 --- a/vendor/zoujingli/think-library/src/command/queue/StateQueue.php +++ /dev/null @@ -1,51 +0,0 @@ -setName('xtask:state')->setDescription('Check listening main process status'); - } - - /** - * 指令执行状态 - * @param Input $input - * @param Output $output - */ - protected function execute(Input $input, Output $output) - { - $command = $this->process->think('xtask:listen'); - if (count($result = $this->process->query($command)) > 0) { - $output->info("Listening for main process {$result[0]['pid']} running"); - } else { - $output->warning("The Listening main process is not running"); - } - } -} diff --git a/vendor/zoujingli/think-library/src/command/queue/StopQueue.php b/vendor/zoujingli/think-library/src/command/queue/StopQueue.php deleted file mode 100644 index 4a97e4cd7..000000000 --- a/vendor/zoujingli/think-library/src/command/queue/StopQueue.php +++ /dev/null @@ -1,53 +0,0 @@ -setName('xtask:stop')->setDescription('Smooth stop of all task processes'); - } - - /** - * 停止所有任务执行 - * @param Input $input - * @param Output $output - */ - protected function execute(Input $input, Output $output) - { - $keyword = $this->process->think('xtask:'); - if (count($result = $this->process->query($keyword)) < 1) { - $output->warning("There is no task process to finish"); - } else foreach ($result as $item) { - $this->process->close($item['pid']); - $output->info("Sending end process {$item['pid']} signal succeeded"); - } - } -} diff --git a/vendor/zoujingli/think-library/src/command/queue/WorkQueue.php b/vendor/zoujingli/think-library/src/command/queue/WorkQueue.php deleted file mode 100644 index 2cb3ec862..000000000 --- a/vendor/zoujingli/think-library/src/command/queue/WorkQueue.php +++ /dev/null @@ -1,141 +0,0 @@ -setName('xtask:_work')->setDescription('Create a process to execute task'); - $this->addArgument('code', Argument::OPTIONAL, 'TaskNumber'); - $this->addArgument('spts', Argument::OPTIONAL, 'Separator'); - } - - /** - * 执行指令的任务 - * @param Input $input 输入对象 - * @param Output $output 输出对象 - * @throws \think\db\exception\DbException - */ - protected function execute(Input $input, Output $output) - { - set_time_limit(0); - $this->code = trim($input->getArgument('code')); - if (empty($this->code)) { - $this->output->error('Task number needs to be specified for task execution'); - } else try { - $this->queue->initialize($this->code); - if (empty($this->queue->record) || intval($this->queue->record['status']) !== 1) { - // 这里不做任何处理(该任务可能在其它地方已经在执行) - $this->output->warning($message = "The or status of task {$this->code} is abnormal"); - } else { - // 锁定任务状态,防止任务再次被执行 - $this->app->db->name($this->table)->strict(false)->where(['code' => $this->code])->update([ - 'enter_time' => microtime(true), 'attempts' => $this->app->db->raw('attempts+1'), - 'outer_time' => '0', 'exec_pid' => getmypid(), 'exec_desc' => '', 'status' => '2', - ]); - $this->queue->progress(2, '>>> 任务处理开始 <<<', 0); - // 设置进程标题 - if ($this->process->iswin()) { - $this->setProcessTitle("ThinkAdmin {$this->process->version()} Queue - {$this->queue->title}"); - } - // 执行任务内容 - defined('WorkQueueCall') or define('WorkQueueCall', true); - defined('WorkQueueCode') or define('WorkQueueCode', $this->code); - if (class_exists($command = $this->queue->record['command'])) { - // 自定义任务,支持返回消息(支持异常结束,异常码可选择 3|4 设置任务状态) - $class = $this->app->make($command, [], true); - if ($class instanceof \think\admin\Queue) { - $this->update(3, $class->initialize($this->queue)->execute($this->queue->data)); - } elseif ($class instanceof \think\admin\service\QueueService) { - $this->update(3, $class->initialize($this->queue->code)->execute($this->queue->data)); - } else { - throw new \think\admin\Exception("自定义 {$command} 未继承 Queue 或 QueueService"); - } - } else { - // 自定义指令,不支持返回消息(支持异常结束,异常码可选择 3|4 设置任务状态) - $attr = explode(' ', trim(preg_replace('|\s+|', ' ', $this->queue->record['command']))); - $this->update(3, $this->app->console->call(array_shift($attr), $attr)->fetch(), false); - } - } - } catch (\Exception|\Error $exception) { - $code = $exception->getCode(); - if (intval($code) !== 3) $code = 4; - $this->update($code, $exception->getMessage()); - } - } - - /** - * 修改当前任务状态 - * @param integer $status 任务状态 - * @param string $message 消息内容 - * @param boolean $issplit 是否分隔 - * @throws \think\db\exception\DbException - */ - protected function update($status, $message, $issplit = true) - { - // 更新当前任务 - $info = trim(is_string($message) ? $message : ''); - $desc = $issplit ? explode("\n", $info) : [$message]; - $this->app->db->name($this->table)->strict(false)->where(['code' => $this->code])->update([ - 'status' => $status, 'outer_time' => microtime(true), 'exec_pid' => getmypid(), 'exec_desc' => $desc[0], - ]); - $this->output->writeln(is_string($message) ? $message : ''); - // 任务进度标记 - if (!empty($desc[0])) { - $this->queue->progress($status, ">>> {$desc[0]} <<<"); - } - if ($status == 3) { - $this->queue->progress($status, '>>> 任务处理完成 <<<', 100); - } elseif ($status == 4) { - $this->queue->progress($status, '>>> 任务处理失败 <<<'); - } - // 注册循环任务 - if (isset($this->queue->record['loops_time']) && $this->queue->record['loops_time'] > 0) { - try { - $this->queue->initialize($this->code)->reset($this->queue->record['loops_time']); - } catch (\Exception|\Error $exception) { - $this->app->log->error("Queue {$this->queue->record['code']} Loops Failed. {$exception->getMessage()}"); - } - } - } - -} diff --git a/vendor/zoujingli/think-library/src/service/QueueService.php b/vendor/zoujingli/think-library/src/service/QueueService.php index febdc3021..8d5e183b2 100644 --- a/vendor/zoujingli/think-library/src/service/QueueService.php +++ b/vendor/zoujingli/think-library/src/service/QueueService.php @@ -105,7 +105,7 @@ class QueueService extends Service */ public function addCleanQueue() { - return $this->register('定时清理系统任务数据', "xtask:clean", 0, [], 0, 3600); + return $this->register('定时清理系统任务数据', "xadmin:queue clean", 0, [], 0, 3600); } /**