From 8ad325686d2ad4faef77d6fb78ad52578523071d Mon Sep 17 00:00:00 2001 From: whyour Date: Mon, 18 Apr 2022 22:29:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=AE=9A=E6=97=B6=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E6=97=A5=E5=BF=97=E8=AE=BE=E7=BD=AE=EF=BC=8C=E4=B8=BB?= =?UTF-8?q?=E6=8C=81=E8=AE=BE=E7=BD=AE=E5=A4=A7=E4=BA=8E24=E5=A4=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/services/schedule.ts | 74 ++++++++++++++++++++++----------------- back/services/system.ts | 2 ++ 2 files changed, 43 insertions(+), 33 deletions(-) diff --git a/back/services/schedule.ts b/back/services/schedule.ts index a1b752aa..94a05ff9 100644 --- a/back/services/schedule.ts +++ b/back/services/schedule.ts @@ -5,8 +5,8 @@ import { Crontab } from '../data/cron'; import { exec } from 'child_process'; import { ToadScheduler, - SimpleIntervalJob, - Task, + LongIntervalJob, + AsyncTask, SimpleIntervalSchedule, } from 'toad-scheduler'; @@ -87,43 +87,51 @@ export default class ScheduleService { name, command, ); - const task = new Task(name, async () => { - try { - exec( - command, - { maxBuffer: this.maxBuffer }, - async (error, stdout, stderr) => { - if (error) { - await this.logger.info( - '执行任务%s失败,时间:%s, 错误信息:%j', - command, - new Date().toLocaleString(), - error, - ); - } + const task = new AsyncTask( + name, + async () => { + return new Promise(async (resolve, reject) => { + try { + exec( + command, + { maxBuffer: this.maxBuffer }, + async (error, stdout, stderr) => { + if (error) { + await this.logger.info( + '执行任务%s失败,时间:%s, 错误信息:%j', + command, + new Date().toLocaleString(), + error, + ); + } - if (stderr) { - await this.logger.info( - '执行任务%s失败,时间:%s, 错误信息:%j', - command, - new Date().toLocaleString(), - stderr, - ); - } - }, - ); - } catch (error) { - await this.logger.info( + if (stderr) { + await this.logger.info( + '执行任务%s失败,时间:%s, 错误信息:%j', + command, + new Date().toLocaleString(), + stderr, + ); + } + resolve(); + }, + ); + } catch (error) { + reject(error); + } + }); + }, + (err) => { + this.logger.info( '执行任务%s失败,时间:%s, 错误信息:%j', command, new Date().toLocaleString(), - error, + err, ); - } finally { - } - }); + }, + ); - const job = new SimpleIntervalJob({ ...schedule }, task, _id); + const job = new LongIntervalJob({ ...schedule }, task, _id); this.intervalSchedule.addIntervalJob(job); } diff --git a/back/services/system.ts b/back/services/system.ts index ca8e5106..8d84e738 100644 --- a/back/services/system.ts +++ b/back/services/system.ts @@ -57,7 +57,9 @@ export default class SystemService { } public async updateLogRemoveFrequency(frequency: number) { + const oDoc = await this.getLogRemoveFrequency(); const result = await this.updateAuthDb({ + ...oDoc, type: AuthDataType.removeLogFrequency, info: { frequency }, });