定时服务区分系统、订阅、脚本任务

This commit is contained in:
whyour
2024-08-23 23:06:50 +08:00
parent 8b8eae211b
commit 4e5ad6d5f3
8 changed files with 132 additions and 45 deletions
+18 -9
View File
@@ -17,6 +17,7 @@ export interface ScheduleTaskType {
command: string;
name?: string;
schedule?: string;
runOrigin: 'subscription' | 'system' | 'script';
}
export interface TaskCallbacks {
@@ -40,7 +41,11 @@ export default class ScheduleService {
private intervalSchedule = new ToadScheduler();
private maxBuffer = 200 * 1024 * 1024;
private taskLimitMap = {
system: 'runWithSystemLimit' as const,
script: 'runWithScriptLimit' as const,
subscription: 'runWithSubscriptionLimit' as const,
};
constructor(@Inject('logger') private logger: winston.Logger) {}
@@ -52,15 +57,17 @@ export default class ScheduleService {
name?: string;
command?: string;
id: string;
runOrigin: 'subscription' | 'system' | 'script';
},
completionTime: 'start' | 'end' = 'end',
) {
return taskLimit.runWithCronLimit(params, () => {
const { runOrigin, ...others } = params;
return taskLimit[this.taskLimitMap[runOrigin]](others, () => {
return new Promise(async (resolve, reject) => {
taskLimit.removeQueuedCron(params.id);
this.logger.info(
`[panel][开始执行任务] 参数 ${JSON.stringify({
...params,
...others,
command,
})}`,
);
@@ -103,7 +110,7 @@ export default class ScheduleService {
endTime,
endTime.diff(startTime, 'seconds'),
);
resolve({ ...params, pid: cp.pid, code });
resolve({ ...others, pid: cp.pid, code });
});
} catch (error) {
this.logger.error(
@@ -118,7 +125,7 @@ export default class ScheduleService {
}
async createCronTask(
{ id = 0, command, name, schedule = '' }: ScheduleTaskType,
{ id = 0, command, name, schedule = '', runOrigin }: ScheduleTaskType,
callbacks?: TaskCallbacks,
runImmediately = false,
) {
@@ -139,6 +146,7 @@ export default class ScheduleService {
schedule,
command,
id: _id,
runOrigin,
});
}),
);
@@ -149,6 +157,7 @@ export default class ScheduleService {
schedule,
command,
id: _id,
runOrigin,
});
}
}
@@ -157,14 +166,13 @@ export default class ScheduleService {
const _id = this.formatId(id);
this.logger.info('[panel][取消定时任务], 任务名: %s', name);
if (this.scheduleStacks.has(_id)) {
taskLimit.removeQueuedCron(_id);
this.scheduleStacks.get(_id)?.cancel();
this.scheduleStacks.delete(_id);
}
}
async createIntervalTask(
{ id = 0, command, name = '' }: ScheduleTaskType,
{ id = 0, command, name = '', runOrigin }: ScheduleTaskType,
schedule: SimpleIntervalSchedule,
runImmediately = true,
callbacks?: TaskCallbacks,
@@ -183,6 +191,7 @@ export default class ScheduleService {
name,
command,
id: _id,
runOrigin,
});
},
(err) => {
@@ -207,6 +216,7 @@ export default class ScheduleService {
name,
command,
id: _id,
runOrigin,
});
}
}
@@ -214,7 +224,6 @@ export default class ScheduleService {
async cancelIntervalTask({ id = 0, name }: ScheduleTaskType) {
const _id = this.formatId(id);
this.logger.info('[取消interval任务], 任务ID: %s, 任务名: %s', _id, name);
taskLimit.removeQueuedCron(_id);
this.intervalSchedule.removeById(_id);
}