From c6220cb920cffc15dc3095de51f9618e7a9f6f37 Mon Sep 17 00:00:00 2001 From: whyour Date: Sun, 29 Jan 2023 18:07:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=AE=9A=E6=97=B6=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E6=9F=A5=E8=AF=A2=E6=97=A5=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/services/cron.ts | 82 ++++------------------------------ src/pages/crontab/logModal.tsx | 2 +- 2 files changed, 10 insertions(+), 74 deletions(-) diff --git a/back/services/cron.ts b/back/services/cron.ts index 7dab67b2..6bc36aa8 100644 --- a/back/services/cron.ts +++ b/back/services/cron.ts @@ -18,7 +18,7 @@ import { TASK_PREFIX, QL_PREFIX } from '../config/const'; @Service() export default class CronService { - constructor(@Inject('logger') private logger: winston.Logger) {} + constructor(@Inject('logger') private logger: winston.Logger) { } private isSixCron(cron: Crontab) { const { schedule } = cron; @@ -427,96 +427,32 @@ export default class CronService { const logFileExist = doc.log_path && (await fileExist(absolutePath)); if (logFileExist) { return getFileContentByName(`${absolutePath}`); - } - const [, commandStr, url] = doc.command.split(/ +/); - let logPath = this.getKey(commandStr); - const isQlCommand = doc.command.startsWith('ql '); - const key = - (url && ['repo', 'raw'].includes(commandStr) && this.getKey(url)) || - logPath; - if (isQlCommand) { - logPath = 'update'; - } - let logDir = `${config.logPath}${logPath}`; - if (existsSync(logDir)) { - let files = await promises.readdir(logDir); - if (isQlCommand) { - files = files.filter((x) => x.includes(key)); - } - return getFileContentByName(`${logDir}/${files[files.length - 1]}`); } else { - return ''; + return '任务未运行或运行失败,请尝试手动运行'; } } public async logs(id: number) { const doc = await this.getDb({ id }); - if (!doc) { + if (!doc || !doc.log_path) { return []; } - if (doc.log_path) { - const relativeDir = path.dirname(`${doc.log_path}`); - const dir = path.resolve(config.logPath, relativeDir); - if (existsSync(dir)) { - let files = await promises.readdir(dir); - return files - .map((x) => ({ - filename: x, - directory: relativeDir.replace(config.logPath, ''), - time: fs.statSync(`${dir}/${x}`).mtime.getTime(), - })) - .sort((a, b) => b.time - a.time); - } - } - - const [, commandStr, url] = doc.command.split(/ +/); - let logPath = this.getKey(commandStr); - const isQlCommand = doc.command.startsWith('ql '); - const key = - (url && ['repo', 'raw'].includes(commandStr) && this.getKey(url)) || - logPath; - if (isQlCommand) { - logPath = 'update'; - } - let logDir = `${config.logPath}${logPath}`; - if (existsSync(logDir)) { - let files = await promises.readdir(logDir); - if (isQlCommand) { - files = files.filter((x) => x.includes(key)); - } + const relativeDir = path.dirname(`${doc.log_path}`); + const dir = path.resolve(config.logPath, relativeDir); + if (existsSync(dir)) { + let files = await promises.readdir(dir); return files .map((x) => ({ filename: x, - directory: logPath, - time: fs.statSync(`${logDir}/${x}`).mtime.getTime(), + directory: relativeDir.replace(config.logPath, ''), + time: fs.statSync(`${dir}/${x}`).mtime.getTime(), })) .sort((a, b) => b.time - a.time); } else { return []; } - } - private getKey(command: string): string { - const start = - command.lastIndexOf('/') !== -1 ? command.lastIndexOf('/') + 1 : 0; - const end = - command.lastIndexOf('.') !== -1 - ? command.lastIndexOf('.') - : command.length; - - const tmpStr = command.substring(0, start - 1); - let index = 0; - if (tmpStr.lastIndexOf('/') !== -1 && tmpStr.startsWith('http')) { - index = tmpStr.lastIndexOf('/'); - } else if (tmpStr.lastIndexOf(':') !== -1 && tmpStr.startsWith('git@')) { - index = tmpStr.lastIndexOf(':'); - } - if (index) { - return `${tmpStr.substring(index + 1)}_${command.substring(start, end)}`; - } else { - return command.substring(start, end); - } } private make_command(tab: Crontab) { diff --git a/src/pages/crontab/logModal.tsx b/src/pages/crontab/logModal.tsx index 3ef6d67a..08ab0d16 100644 --- a/src/pages/crontab/logModal.tsx +++ b/src/pages/crontab/logModal.tsx @@ -48,7 +48,7 @@ const CronLogModal = ({ ) { const log = data as string; setValue(log || '暂无日志'); - const hasNext = log && !logEnded(log) && !log.includes('重启面板'); + const hasNext = log && !logEnded(log) && !log.includes('重启面板') && !log.includes('任务未运行或运行失败,请尝试手动运行'); setExecuting(hasNext); setTimeout(() => { document