From 3b389259c11460391d237e81b51dc8a2adc5ba17 Mon Sep 17 00:00:00 2001 From: whyour Date: Tue, 6 Dec 2022 00:22:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=B0=83=E8=AF=95=E8=84=9A?= =?UTF-8?q?=E6=9C=AC=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/services/schedule.ts | 17 +++++++++++------ back/services/script.ts | 10 ++-------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/back/services/schedule.ts b/back/services/schedule.ts index 7bab03d6..cfaa28b5 100644 --- a/back/services/schedule.ts +++ b/back/services/schedule.ts @@ -42,15 +42,21 @@ export default class ScheduleService { constructor(@Inject('logger') private logger: winston.Logger) {} - async runTask(command: string, callbacks: TaskCallbacks = {}) { + async runTask( + command: string, + callbacks: TaskCallbacks = {}, + completionTime: 'start' | 'end' = 'end', + ) { return new Promise(async (resolve, reject) => { - const startTime = dayjs(); - await callbacks.onBefore?.(startTime); - - const cp = spawn(command, { shell: '/bin/bash' }); try { + const startTime = dayjs(); + await callbacks.onBefore?.(startTime); + + const cp = spawn(command, { shell: '/bin/bash' }); + // TODO: callbacks.onStart?.(cp, startTime); + completionTime === 'start' && resolve(cp.pid); cp.stdout.on('data', async (data) => { await callbacks.onLog?.(data.toString()); @@ -100,7 +106,6 @@ export default class ScheduleService { ); await callbacks.onError?.(JSON.stringify(error)); } - resolve(cp.pid); }); } diff --git a/back/services/script.ts b/back/services/script.ts index e486b7a5..295a2fac 100644 --- a/back/services/script.ts +++ b/back/services/script.ts @@ -43,9 +43,10 @@ export default class ScriptService { public async runScript(filePath: string) { const relativePath = path.relative(config.scriptPath, filePath); const command = `task -l ${relativePath} now`; - const pid = this.scheduleService.runTask( + const pid = await this.scheduleService.runTask( command, this.taskCallbacks(filePath), + 'start', ); return { code: 200, data: pid }; @@ -61,13 +62,6 @@ export default class ScriptService { await killTask(pid); } catch (error) {} - this.sockService.sendMessage({ - type: 'manuallyRunScript', - message: `${str}\n## 执行结束... ${new Date() - .toLocaleString('zh', { hour12: false }) - .replace(' 24:', ' 00:')}${LOG_END_SYMBOL}`, - }); - return { code: 200 }; } }