添加系统更新操作和设置删除日志频率

This commit is contained in:
hanhh
2021-10-12 00:27:42 +08:00
parent 9455ca64a2
commit b1077443a3
19 changed files with 531 additions and 18 deletions
+61
View File
@@ -0,0 +1,61 @@
import { Service, Inject } from 'typedi';
import winston from 'winston';
import nodeSchedule from 'node-schedule';
import { Crontab } from '../data/cron';
import { exec } from 'child_process';
@Service()
export default class ScheduleService {
private scheduleStacks = new Map<string, nodeSchedule.Job>();
constructor(@Inject('logger') private logger: winston.Logger) {}
async generateSchedule({ _id = '', command, name, schedule }: Crontab) {
this.logger.info(
'[创建定时任务],任务ID: %scron: %s,任务名: %s,任务方法: %s',
_id,
schedule,
name,
);
this.scheduleStacks.set(
_id,
nodeSchedule.scheduleJob(_id, schedule, async () => {
try {
exec(command, async (error, stdout, stderr) => {
if (error) {
await this.logger.info(
'执行任务`%s`失败,时间:%s, 错误信息:%j',
name,
new Date().toLocaleString(),
error,
);
}
if (stderr) {
await this.logger.info(
'执行任务`%s`失败,时间:%s, 错误信息:%j',
name,
new Date().toLocaleString(),
stderr,
);
}
});
} catch (error) {
await this.logger.info(
'执行任务`%s`失败,时间:%s, 错误信息:%j',
name,
new Date().toLocaleString(),
error,
);
} finally {
}
}),
);
}
async cancelSchedule(id: string, jobName: string) {
this.logger.info('[取消定时任务],任务名:%s', jobName);
this.scheduleStacks.has(id) && this.scheduleStacks.get(id)?.cancel();
}
}