From 6f13cc6e6ff1d77514ab43ea20d865e05f2870d1 Mon Sep 17 00:00:00 2001 From: whyour Date: Sun, 22 May 2022 22:14:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=AE=A2=E9=98=85before/afte?= =?UTF-8?q?r=E6=89=A7=E8=A1=8C=E6=97=B6=E6=9C=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/services/schedule.ts | 4 -- back/services/subscription.ts | 71 +++++++++++++++++------------------ 2 files changed, 34 insertions(+), 41 deletions(-) diff --git a/back/services/schedule.ts b/back/services/schedule.ts index 1dd147c2..4627bb23 100644 --- a/back/services/schedule.ts +++ b/back/services/schedule.ts @@ -29,8 +29,6 @@ export interface TaskCallbacks { ) => Promise; onLog?: (message: string) => Promise; onError?: (message: string) => Promise; - onBefore?: () => Promise; - onAfter?: () => Promise; } @Service() @@ -46,7 +44,6 @@ export default class ScheduleService { async runTask(command: string, callbacks: TaskCallbacks = {}) { return new Promise(async (resolve, reject) => { try { - await callbacks.onBefore?.(); const startTime = dayjs(); const cp = spawn(command, { shell: '/bin/bash' }); @@ -91,7 +88,6 @@ export default class ScheduleService { endTime, endTime.diff(startTime, 'seconds'), ); - await callbacks.onAfter?.(); resolve(null); }); } catch (error) { diff --git a/back/services/subscription.ts b/back/services/subscription.ts index fbdde7ed..eb6d54e0 100644 --- a/back/services/subscription.ts +++ b/back/services/subscription.ts @@ -175,17 +175,32 @@ export default class SubscriptionService { private taskCallbacks(doc: Subscription): TaskCallbacks { return { onStart: async (cp: ChildProcessWithoutNullStreams, startTime) => { + // 执行sub_before + let beforeStr = ''; + try { + if (doc.sub_before) { + beforeStr = execSync(doc.sub_before).toString(); + } + } catch (error) { + beforeStr = JSON.stringify(error); + } + if (beforeStr) { + beforeStr += '\n'; + } + const logTime = startTime.format('YYYY-MM-DD-HH-mm-ss'); const logPath = `${doc.alias}/${logTime}.log`; await this.handleLogPath( logPath as string, - `## 开始执行... ${startTime.format('YYYY-MM-DD HH:mm:ss')}\n`, + `${beforeStr}## 开始执行... ${startTime.format( + 'YYYY-MM-DD HH:mm:ss', + )}\n`, ); await SubscriptionModel.update( { status: SubscriptionStatus.running, - pid: cp.pid, log_path: logPath, + pid: cp.pid, }, { where: { id: doc.id } }, ); @@ -194,7 +209,7 @@ export default class SubscriptionService { const sub = await this.getDb({ id: doc.id }); await SubscriptionModel.update( { status: SubscriptionStatus.idle, pid: undefined }, - { where: { id: doc.id } }, + { where: { id: sub.id } }, ); const absolutePath = await this.handleLogPath(sub.log_path as string); fs.appendFileSync( @@ -203,6 +218,22 @@ export default class SubscriptionService { 'YYYY-MM-DD HH:mm:ss', )} 耗时 ${diff} 秒`, ); + + // 执行 sub_after + let afterStr = ''; + try { + if (sub.sub_after) { + afterStr = execSync(sub.sub_after).toString(); + } + } catch (error) { + afterStr = JSON.stringify(error); + } + if (afterStr) { + afterStr = `\n\n${afterStr}`; + const absolutePath = await this.handleLogPath(sub.log_path as string); + fs.appendFileSync(absolutePath, afterStr); + } + this.sockService.sendMessage({ type: 'runSubscriptionEnd', message: '订阅执行完成', @@ -219,40 +250,6 @@ export default class SubscriptionService { const absolutePath = await this.handleLogPath(sub.log_path as string); fs.appendFileSync(absolutePath, `\n${message}`); }, - onBefore: async () => { - return new Promise((resolve, reject) => { - if (doc.sub_before) { - exec(doc.sub_before, async (err, stdout, stderr) => { - const absolutePath = await this.handleLogPath( - doc.log_path as string, - ); - fs.appendFileSync( - absolutePath, - stdout || stderr || `${JSON.stringify(err || {})}`, - ); - }); - } else { - resolve(); - } - }); - }, - onAfter: async () => { - return new Promise((resolve, reject) => { - if (doc.sub_after) { - exec(doc.sub_after, async (err, stdout, stderr) => { - const absolutePath = await this.handleLogPath( - doc.log_path as string, - ); - fs.appendFileSync( - absolutePath, - stdout || stderr || `${JSON.stringify(err || {})}`, - ); - }); - } else { - resolve(); - } - }); - }, }; }