diff --git a/back/services/cron.ts b/back/services/cron.ts index ca2908e7..a4da2c2e 100644 --- a/back/services/cron.ts +++ b/back/services/cron.ts @@ -5,7 +5,7 @@ import { Crontab, CrontabModel, CrontabStatus } from '../data/cron'; import { exec, execSync } from 'child_process'; import fs from 'fs'; import cron_parser from 'cron-parser'; -import { getFileContentByName, fileExist, killTask } from '../config/util'; +import { getFileContentByName, fileExist, killTask, getUniqPath } from '../config/util'; import { promises, existsSync } from 'fs'; import { Op, where, col as colFn, FindOptions, fn } from 'sequelize'; import path from 'path'; @@ -13,10 +13,11 @@ import { TASK_PREFIX, QL_PREFIX } from '../config/const'; import cronClient from '../schedule/client'; import taskLimit from '../shared/pLimit'; import { spawn } from 'cross-spawn'; +import dayjs from 'dayjs'; @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; @@ -391,8 +392,14 @@ export default class CronService { } let { id, command, log_path } = cron; - const absolutePath = path.resolve(config.logPath, `${log_path}`); - const logFileExist = log_path && (await fileExist(absolutePath)); + const uniqPath = await getUniqPath(command); + const logTime = dayjs().format('YYYY-MM-DD-HH-mm-ss-SSS'); + const logDirPath = path.resolve(config.logPath, `${uniqPath}`); + if (!log_path?.includes(uniqPath)) { + fs.mkdirSync(logDirPath, { recursive: true }); + } + const logPath = `${uniqPath}/${logTime}.log`; + const absolutePath = path.resolve(config.logPath, `${logPath}`); this.logger.silly('Running job'); this.logger.silly('ID: ' + id); @@ -412,21 +419,17 @@ export default class CronService { cmdStr = `${cmdStr} now`; } - const cp = spawn(`ID=${id} ${cmdStr}`, { shell: '/bin/bash' }); + const cp = spawn(`real_log_path=${logPath} ID=${id} ${cmdStr}`, { shell: '/bin/bash' }); await CrontabModel.update( - { status: CrontabStatus.running, pid: cp.pid }, + { status: CrontabStatus.running, pid: cp.pid, log_path: logPath }, { where: { id } }, ); cp.stderr.on('data', (data) => { - if (logFileExist) { - fs.appendFileSync(`${absolutePath}`, `${data.toString()}`); - } + fs.appendFileSync(`${absolutePath}`, `${data.toString()}`); }); cp.on('error', (err) => { - if (logFileExist) { - fs.appendFileSync(`${absolutePath}`, `${JSON.stringify(err)}`); - } + fs.appendFileSync(`${absolutePath}`, `${JSON.stringify(err)}`); }); cp.on('exit', async (code, signal) => { diff --git a/src/layouts/index.less b/src/layouts/index.less index 8569d568..ab1ddc44 100644 --- a/src/layouts/index.less +++ b/src/layouts/index.less @@ -93,10 +93,12 @@ body { } .ant-tooltip { - max-width: 500px !important; + max-width: 300px !important; .ant-tooltip-inner { word-break: break-all !important; + max-height: 300px !important; + overflow-y: auto !important; } } diff --git a/src/pages/crontab/index.tsx b/src/pages/crontab/index.tsx index e0a62c7c..a204c549 100644 --- a/src/pages/crontab/index.tsx +++ b/src/pages/crontab/index.tsx @@ -69,7 +69,7 @@ const Crontab = () => { dataIndex: 'name', key: 'name', fixed: 'left', - width: 140, + width: 120, render: (text: string, record: any) => ( <> { title: intl.get('命令/脚本'), dataIndex: 'command', key: 'command', - width: 220, + width: 240, render: (text, record) => { return ( { render: (text: string, record: any) => { return (
- +
{text}