refactor(command): 重构命令行任务处理逻辑

- 使用模型替换 Db::name() 方法,提高代码可读性和可维护性
- 优化 Dmtask 和 Opiptask 命令的执行逻辑
- 更新异常捕获方式,使用更具体的异常类
- 调整命名空间和引入的类,以适应新的代码结构
This commit is contained in:
coolxitech 2024-11-09 20:23:57 +08:00
parent da0499142b
commit 638ab7cf3f
2 changed files with 16 additions and 12 deletions

View File

@ -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++;
}

View File

@ -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();