From c84908d7fa4a18598db2831fcb7f63a08da746a8 Mon Sep 17 00:00:00 2001 From: whyour Date: Thu, 4 May 2023 10:25:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=BF=90=E8=A1=8C=E5=91=BD?= =?UTF-8?q?=E4=BB=A4api=E4=BB=BB=E5=8A=A1=E6=97=A5=E5=BF=97=E8=B7=AF?= =?UTF-8?q?=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/api/system.ts | 33 ++++++++++++++++++--------------- back/schedule/health.ts | 2 +- back/services/system.ts | 10 ++++++++-- shell/check.sh | 1 + shell/task.sh | 5 +++++ src/pages/error/index.tsx | 4 +--- 6 files changed, 34 insertions(+), 21 deletions(-) diff --git a/back/api/system.ts b/back/api/system.ts index 7d1f0711..57df89da 100644 --- a/back/api/system.ts +++ b/back/api/system.ts @@ -164,24 +164,27 @@ export default (app: Router) => { try { const systemService = Container.get(SystemService); const uniqPath = await getUniqPath(req.body.command); - const logTime = dayjs().format('YYYY-MM-DD-HH-mm-ss'); + const logTime = dayjs().format('YYYY-MM-DD-HH-mm-ss-SSS'); const logPath = `${uniqPath}/${logTime}.log`; res.setHeader('Content-type', 'application/octet-stream'); - await systemService.run(req.body, { - onEnd: async (cp, endTime, diff) => { - res.end(); + await systemService.run( + { ...req.body, logPath }, + { + onEnd: async (cp, endTime, diff) => { + res.end(); + }, + onError: async (message: string) => { + res.write(`\n${message}`); + const absolutePath = await handleLogPath(logPath); + fs.appendFileSync(absolutePath, `\n${message}`); + }, + onLog: async (message: string) => { + res.write(`\n${message}`); + const absolutePath = await handleLogPath(logPath); + fs.appendFileSync(absolutePath, `\n${message}`); + }, }, - onError: async (message: string) => { - res.write(`\n${message}`); - const absolutePath = await handleLogPath(logPath); - fs.appendFileSync(absolutePath, `\n${message}`); - }, - onLog: async (message: string) => { - res.write(`\n${message}`); - const absolutePath = await handleLogPath(logPath); - fs.appendFileSync(absolutePath, `\n${message}`); - }, - }); + ); } catch (e) { return next(e); } diff --git a/back/schedule/health.ts b/back/schedule/health.ts index f9b3731e..5c1651ce 100644 --- a/back/schedule/health.ts +++ b/back/schedule/health.ts @@ -13,7 +13,7 @@ const check = async ( const res = await promiseExec( `curl -sf http://localhost:${config.port}/api/system`, ); - console.log(res); + if (res.includes('200')) { return callback(null, { status: 1 }); } diff --git a/back/services/system.ts b/back/services/system.ts index 30e5c69a..9a34bf53 100644 --- a/back/services/system.ts +++ b/back/services/system.ts @@ -177,11 +177,17 @@ export default class SystemService { } } - public async run({ command }: { command: string }, callback: TaskCallbacks) { + public async run( + { command, logPath }: { command: string; logPath: string }, + callback: TaskCallbacks, + ) { if (!command.startsWith(TASK_COMMAND)) { command = `${TASK_COMMAND} ${command}`; } - this.scheduleService.runTask(`real_time=true ${command}`, callback); + this.scheduleService.runTask( + `real_log_path=${logPath} real_time=true ${command}`, + callback, + ); } public async stop({ command }: { command: string }) { diff --git a/shell/check.sh b/shell/check.sh index 97fe1312..eb7421ae 100755 --- a/shell/check.sh +++ b/shell/check.sh @@ -82,6 +82,7 @@ start_public() { main() { echo -e "=====> 开始检测" + rm -rf ~/.pm2 npm i -g pnpm patch_version pnpm add -g pm2 tsx diff --git a/shell/task.sh b/shell/task.sh index a96627c2..de51cb7a 100755 --- a/shell/task.sh +++ b/shell/task.sh @@ -65,6 +65,11 @@ handle_log_path() { [[ $log_dir_tmp_path ]] && log_dir_tmp="${log_dir_tmp_path}_${log_dir_tmp}" log_dir="${log_dir_tmp%.*}${suffix}" log_path="$log_dir/$log_time.log" + + if [[ $real_log_path != "${log_path}" ]]; then + log_path="$real_log_path" + fi + cmd=">> $dir_log/$log_path 2>&1" make_dir "$dir_log/$log_dir" if [[ "$show_log" == "true" ]]; then diff --git a/src/pages/error/index.tsx b/src/pages/error/index.tsx index 3e4c5475..9c899f22 100644 --- a/src/pages/error/index.tsx +++ b/src/pages/error/index.tsx @@ -13,8 +13,6 @@ const Error = () => { const [data, setData] = useState('暂无日志'); const retryTimes = useRef(1); - console.log(retryTimes.current); - const getLog = (needLoading: boolean = true) => { needLoading && setLoading(true); request @@ -24,9 +22,9 @@ const Error = () => { return reloadUser(); } if (retryTimes.current > 3) { + setData(error?.details); return; } - setData(error.details); retryTimes.current += 1; setTimeout(() => { reloadUser();