From f338537ea02eff1943e48370e7c0bdb2dcc6ddae Mon Sep 17 00:00:00 2001 From: whyour Date: Sat, 28 May 2022 19:56:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=B3=BB=E7=BB=9F=E5=90=AF?= =?UTF-8?q?=E5=8A=A8=E6=89=A7=E8=A1=8C=E8=AE=A2=E9=98=85=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/loaders/initTask.ts | 2 +- back/services/schedule.ts | 15 ++++++++++++++- back/services/subscription.ts | 30 ++++++++++++++++++------------ 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/back/loaders/initTask.ts b/back/loaders/initTask.ts index 9e486f64..f02c2c00 100644 --- a/back/loaders/initTask.ts +++ b/back/loaders/initTask.ts @@ -26,6 +26,6 @@ export default async () => { // 运行所有订阅 const subs = await subscriptionService.list(); for (const sub of subs) { - await subscriptionService.handleTask(sub); + await subscriptionService.handleTask(sub, true, true, true); } }; diff --git a/back/services/schedule.ts b/back/services/schedule.ts index 4627bb23..79b2f279 100644 --- a/back/services/schedule.ts +++ b/back/services/schedule.ts @@ -106,6 +106,7 @@ export default class ScheduleService { async createCronTask( { id = 0, command, name, schedule = '' }: ScheduleTaskType, callbacks?: TaskCallbacks, + runImmediately = false, ) { const _id = this.formatId(id); this.logger.info( @@ -122,6 +123,10 @@ export default class ScheduleService { await this.runTask(command, callbacks); }), ); + + if (runImmediately) { + await this.runTask(command, callbacks); + } } async cancelCronTask({ id = 0, name }: ScheduleTaskType) { @@ -160,9 +165,17 @@ export default class ScheduleService { }, ); - const job = new LongIntervalJob({ ...schedule, runImmediately }, task, _id); + const job = new LongIntervalJob( + { ...schedule, runImmediately: false }, + task, + _id, + ); this.intervalSchedule.addIntervalJob(job); + + if (runImmediately) { + await this.runTask(command, callbacks); + } } async cancelIntervalTask({ id = 0, name }: ScheduleTaskType) { diff --git a/back/services/subscription.ts b/back/services/subscription.ts index 4a968adb..d93a6007 100644 --- a/back/services/subscription.ts +++ b/back/services/subscription.ts @@ -124,7 +124,12 @@ export default class SubscriptionService { return { url, host }; } - public handleTask(doc: Subscription, needCreate = true, needAddKey = true) { + public async handleTask( + doc: Subscription, + needCreate = true, + needAddKey = true, + runImmediately = false, + ) { const { url, host } = this.formatUrl(doc); if (doc.type === 'private-repo' && doc.pull_type === 'ssh-key') { if (needAddKey) { @@ -143,20 +148,21 @@ export default class SubscriptionService { if (doc.schedule_type === 'crontab') { this.scheduleService.cancelCronTask(doc as any); needCreate && - this.scheduleService.createCronTask( + (await this.scheduleService.createCronTask( doc as any, this.taskCallbacks(doc), - ); + runImmediately, + )); } else { this.scheduleService.cancelIntervalTask(doc as any); const { type, value } = doc.interval_schedule as any; needCreate && - this.scheduleService.createIntervalTask( + (await this.scheduleService.createIntervalTask( doc as any, { [type]: value } as SimpleIntervalSchedule, - true, + runImmediately, this.taskCallbacks(doc), - ); + )); } } @@ -270,7 +276,7 @@ export default class SubscriptionService { public async create(payload: Subscription): Promise { const tab = new Subscription(payload); const doc = await this.insert(tab); - this.handleTask(doc); + await this.handleTask(doc); return doc; } @@ -280,7 +286,7 @@ export default class SubscriptionService { public async update(payload: Subscription): Promise { const newDoc = await this.updateDb(payload); - this.handleTask(newDoc); + await this.handleTask(newDoc); return newDoc; } @@ -323,7 +329,7 @@ export default class SubscriptionService { public async remove(ids: number[]) { const docs = await SubscriptionModel.findAll({ where: { id: ids } }); for (const doc of docs) { - this.handleTask(doc, false, false); + await this.handleTask(doc, false, false); } await SubscriptionModel.destroy({ where: { id: ids } }); } @@ -354,7 +360,7 @@ export default class SubscriptionService { this.logger.silly(error); } } - this.handleTask(doc, false); + await this.handleTask(doc, false); const command = this.formatCommand(doc); const err = await this.killTask(command); const absolutePath = await this.handleLogPath(doc.log_path as string); @@ -425,7 +431,7 @@ export default class SubscriptionService { public async disabled(ids: number[]) { const docs = await SubscriptionModel.findAll({ where: { id: ids } }); for (const doc of docs) { - this.handleTask(doc, false); + await this.handleTask(doc, false); } await SubscriptionModel.update({ is_disabled: 1 }, { where: { id: ids } }); } @@ -433,7 +439,7 @@ export default class SubscriptionService { public async enabled(ids: number[]) { const docs = await SubscriptionModel.findAll({ where: { id: ids } }); for (const doc of docs) { - this.handleTask(doc); + await this.handleTask(doc); } await SubscriptionModel.update({ is_disabled: 0 }, { where: { id: ids } }); }