From c285026339effbbc7e8848d7619c48599f9679e1 Mon Sep 17 00:00:00 2001 From: whyour Date: Mon, 27 Feb 2023 23:25:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=88=A0=E9=99=A4=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/api/cron.ts | 15 +++++++++++++++ back/services/cron.ts | 11 ++++++++++- shell/api.sh | 23 +++++++++++++++++++++++ shell/rmlog.sh | 10 ++++++++-- src/pages/crontab/logModal.tsx | 2 +- 5 files changed, 57 insertions(+), 4 deletions(-) diff --git a/back/api/cron.ts b/back/api/cron.ts index f47d4307..90777f85 100644 --- a/back/api/cron.ts +++ b/back/api/cron.ts @@ -152,6 +152,21 @@ export default (app: Router) => { } }); + route.get( + '/detail', + async (req: Request, res: Response, next: NextFunction) => { + const logger: Logger = Container.get('logger'); + try { + const cronService = Container.get(CronService); + const data = await cronService.find(req.query as any); + return res.send({ code: 200, data }); + } catch (e) { + logger.error('🔥 error: %o', e); + return next(e); + } + }, + ); + route.post( '/', celebrate({ diff --git a/back/services/cron.ts b/back/services/cron.ts index e39f1357..26d71843 100644 --- a/back/services/cron.ts +++ b/back/services/cron.ts @@ -261,6 +261,15 @@ export default class CronService { } } + public async find(params: { log_path: string }): Promise { + try { + const result = await CrontabModel.findOne({ where: { ...params } }); + return result; + } catch (error) { + throw error; + } + } + public async crontabs(params?: { searchValue: string; page: string; @@ -429,7 +438,7 @@ export default class CronService { if (logFileExist) { return getFileContentByName(`${absolutePath}`); } else { - return '任务未运行或运行失败,请尝试手动运行'; + return '任务未运行'; } } diff --git a/shell/api.sh b/shell/api.sh index 7abc94bb..92de8312 100755 --- a/shell/api.sh +++ b/shell/api.sh @@ -199,4 +199,27 @@ notify_api() { fi } +find_cron_api() { + local params=$1 + local currentTimeStamp=$(date +%s) + local api=$( + curl -s --noproxy "*" "http://0.0.0.0:5600/open/crons/detail?$params&t=$currentTimeStamp" \ + -H "Accept: application/json" \ + -H "Authorization: Bearer $token" \ + -H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36" \ + -H "Content-Type: application/json;charset=UTF-8" \ + -H "Origin: http://0.0.0.0:5700" \ + -H "Referer: http://0.0.0.0:5700/crontab" \ + -H "Accept-Language: en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7" \ + --compressed + ) + data=$(echo "$api" | jq -r .data) + if [[ $data == 'null' ]]; then + echo -e "" + else + name=$(echo "$api" | jq -r .data.name) + echo -e "$name" + fi +} + get_token diff --git a/shell/rmlog.sh b/shell/rmlog.sh index bebdaf23..cc9dae3c 100755 --- a/shell/rmlog.sh +++ b/shell/rmlog.sh @@ -6,7 +6,7 @@ dir_shell=$QL_DIR/shell days=$1 -## 删除运行js脚本的旧日志 +## 删除运行脚本的旧日志 remove_js_log() { local log_full_path_list=$(find $dir_log/ -name "*.log") local diff_time @@ -18,7 +18,13 @@ remove_js_log() { else diff_time=$(($(date +%s) - $(date +%s -d "$log_date"))) fi - [[ $diff_time -gt $((${days} * 86400)) ]] && rm -vf $log + if [[ $diff_time -gt $((${days} * 86400)) ]]; then + local log_path=$(echo "$log" | sed "s,${dir_log},,g") + local result=$(find_cron_api "log_path=$log_path") + if [[ $result ]]; then + rm -vf $log + fi + fi fi done } diff --git a/src/pages/crontab/logModal.tsx b/src/pages/crontab/logModal.tsx index c2bd5452..d85100ed 100644 --- a/src/pages/crontab/logModal.tsx +++ b/src/pages/crontab/logModal.tsx @@ -54,7 +54,7 @@ const CronLogModal = ({ log && !logEnded(log) && !log.includes('重启面板') && - !log.includes('任务未运行或运行失败,请尝试手动运行'), + !log.includes('任务未运行'), ); setExecuting(hasNext); autoScroll();