From b160f914ad49160a0f58e3fc221e1ac202e50db8 Mon Sep 17 00:00:00 2001 From: whyour Date: Wed, 1 Jun 2022 00:25:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=AE=A2=E9=98=85=E6=89=A7?= =?UTF-8?q?=E8=A1=8C=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/services/schedule.ts | 3 +++ back/services/subscription.ts | 15 +++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/back/services/schedule.ts b/back/services/schedule.ts index 79b2f279..b11a2b28 100644 --- a/back/services/schedule.ts +++ b/back/services/schedule.ts @@ -18,6 +18,7 @@ interface ScheduleTaskType { } export interface TaskCallbacks { + onBefore?: (startTime: dayjs.Dayjs) => Promise; onStart?: ( cp: ChildProcessWithoutNullStreams, startTime: dayjs.Dayjs, @@ -45,6 +46,8 @@ export default class ScheduleService { return new Promise(async (resolve, reject) => { try { const startTime = dayjs(); + await callbacks.onBefore?.(startTime); + const cp = spawn(command, { shell: '/bin/bash' }); // TODO: diff --git a/back/services/subscription.ts b/back/services/subscription.ts index 4718a519..8916d0de 100644 --- a/back/services/subscription.ts +++ b/back/services/subscription.ts @@ -192,14 +192,13 @@ export default class SubscriptionService { private taskCallbacks(doc: Subscription): TaskCallbacks { return { - onStart: async (cp: ChildProcessWithoutNullStreams, startTime) => { + onBefore: async (startTime) => { const logTime = startTime.format('YYYY-MM-DD-HH-mm-ss'); const logPath = `${doc.alias}/${logTime}.log`; await SubscriptionModel.update( { status: SubscriptionStatus.running, log_path: logPath, - pid: cp.pid, }, { where: { id: doc.id } }, ); @@ -220,9 +219,17 @@ export default class SubscriptionService { (error.stderr && error.stderr.toString()) || JSON.stringify(error); } if (beforeStr) { - fs.appendFileSync(absolutePath, `${beforeStr}\n\n`); + fs.appendFileSync(absolutePath, `${beforeStr}\n`); } }, + onStart: async (cp: ChildProcessWithoutNullStreams, startTime) => { + await SubscriptionModel.update( + { + pid: cp.pid, + }, + { where: { id: doc.id } }, + ); + }, onEnd: async (cp, endTime, diff) => { const sub = await this.getDb({ id: doc.id }); const absolutePath = await this.handleLogPath(sub.log_path as string); @@ -231,7 +238,7 @@ export default class SubscriptionService { let afterStr = ''; try { if (sub.sub_after) { - fs.appendFileSync(absolutePath, `\n\n## 执行after命令...\n`); + fs.appendFileSync(absolutePath, `\n\n## 执行after命令...\n\n`); afterStr = await this.promiseExec(sub.sub_after); } } catch (error: any) {