修复 6 位 cron 不以 task 开头运行失败

This commit is contained in:
whyour 2023-07-25 22:18:35 +08:00
parent 590dbba19b
commit 6832cb4e22
2 changed files with 12 additions and 2 deletions

View File

@ -3,6 +3,9 @@ import { AddCronRequest, AddCronResponse } from '../protos/cron';
import nodeSchedule from 'node-schedule'; import nodeSchedule from 'node-schedule';
import { scheduleStacks } from './data'; import { scheduleStacks } from './data';
import { runCron } from '../shared/runCron'; import { runCron } from '../shared/runCron';
import { QL_PREFIX, TASK_PREFIX } from '../config/const';
import Logger from '../loaders/logger';
import dayjs from 'dayjs';
const addCron = ( const addCron = (
call: ServerUnaryCall<AddCronRequest, AddCronResponse>, call: ServerUnaryCall<AddCronRequest, AddCronResponse>,
@ -13,10 +16,17 @@ const addCron = (
if (scheduleStacks.has(id)) { if (scheduleStacks.has(id)) {
scheduleStacks.get(id)?.cancel(); scheduleStacks.get(id)?.cancel();
} }
let cmdStr = command;
if (!cmdStr.startsWith(TASK_PREFIX) && !cmdStr.startsWith(QL_PREFIX)) {
cmdStr = `${TASK_PREFIX}${cmdStr}`;
}
scheduleStacks.set( scheduleStacks.set(
id, id,
nodeSchedule.scheduleJob(id, schedule, async () => { nodeSchedule.scheduleJob(id, schedule, async () => {
runCron(`ID=${id} ${command}`) Logger.silly(`当前时间: ${dayjs().format('YYYY-MM-DD HH:mm:ss')},运行命令: ${cmdStr}`);
runCron(`ID=${id} ${cmdStr}`)
}), }),
); );
} }

View File

@ -24,7 +24,7 @@ export default class CronService {
private isSixCron(cron: Crontab) { private isSixCron(cron: Crontab) {
const { schedule } = cron; const { schedule } = cron;
if (schedule?.split(/ +/).length === 6) { if (Number(schedule?.split(/ +/).length) > 5) {
return true; return true;
} }
return false; return false;