From 638ab7cf3ffbcd842a1f0570e0199aa41fc62ace Mon Sep 17 00:00:00 2001 From: coolxitech Date: Sat, 9 Nov 2024 20:23:57 +0800 Subject: [PATCH] =?UTF-8?q?refactor(command):=20=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E8=A1=8C=E4=BB=BB=E5=8A=A1=E5=A4=84=E7=90=86?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 使用模型替换 Db::name() 方法,提高代码可读性和可维护性 - 优化 Dmtask 和 Opiptask 命令的执行逻辑 - 更新异常捕获方式,使用更具体的异常类 - 调整命名空间和引入的类,以适应新的代码结构 --- app/command/Dmtask.php | 22 ++++++++++++++-------- app/command/Opiptask.php | 6 ++---- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/app/command/Dmtask.php b/app/command/Dmtask.php index 9748898..5f075c3 100644 --- a/app/command/Dmtask.php +++ b/app/command/Dmtask.php @@ -4,15 +4,21 @@ declare (strict_types=1); namespace app\command; +use Co; use Exception; +use Swoole\ExitException; use think\console\Command; use think\console\Input; use think\console\input\Argument; use think\console\input\Option; use think\console\Output; use think\facade\Db; +use app\model\Config as ConfigModel; +use app\model\Dmtask as DmtaskModel; use think\facade\Config; use app\lib\TaskRunner; +use function Co\run; +use function go; class Dmtask extends Command { @@ -25,7 +31,7 @@ class Dmtask extends Command protected function execute(Input $input, Output $output) { - $res = Db::name('config')->cache('configs', 0)->column('value', 'key'); + $res = ConfigModel::cache('configs', 0)->column('value', 'key'); Config::set($res, 'sys'); config_set('run_error', ''); @@ -45,8 +51,8 @@ class Dmtask extends Command private function runtask() { - \Co::set(['hook_flags' => SWOOLE_HOOK_ALL]); - \Co\run(function () { + Co::set(['hook_flags' => SWOOLE_HOOK_ALL]); + run(function () { $date = date("Ymd"); $count = config_get('run_count', null, true) ?? 0; while (true) { @@ -56,20 +62,20 @@ class Dmtask extends Command $date = date("Ymd"); } - $rows = Db::name('dmtask')->where('checknexttime', '<=', time())->where('active', 1)->order('id', 'ASC')->select(); + $rows = DmtaskModel::where('checknexttime', '<=', time())->where('active', 1)->order('id', 'ASC')->select(); foreach ($rows as $row) { - \go(function () use ($row) { + go(function () use ($row) { try { (new TaskRunner())->execute($row); - } catch (\Swoole\ExitException $e) { + } catch (ExitException $e) { echo $e->getStatus()."\n"; } catch (Exception $e) { echo $e->__toString()."\n"; } }); - Db::name('dmtask')->where('id', $row['id'])->update([ + DmtaskModel::where('id', $row['id'])->update([ 'checktime' => time(), - 'checknexttime' => time() + $row['frequency'] + 'checknexttime' => time() + $row['frequency'], ]); $count++; } diff --git a/app/command/Opiptask.php b/app/command/Opiptask.php index dd62cc8..dc7886e 100644 --- a/app/command/Opiptask.php +++ b/app/command/Opiptask.php @@ -7,12 +7,10 @@ namespace app\command; use Exception; use think\console\Command; use think\console\Input; -use think\console\input\Argument; -use think\console\input\Option; use think\console\Output; -use think\facade\Db; use think\facade\Config; use app\lib\OptimizeService; +use app\model\Config as ConfigModel; class Opiptask extends Command { @@ -25,7 +23,7 @@ class Opiptask extends Command protected function execute(Input $input, Output $output) { - $res = Db::name('config')->cache('configs', 0)->column('value', 'key'); + $res = ConfigModel::cache('configs', 0)->column('value', 'key'); Config::set($res, 'sys'); (new OptimizeService())->execute();