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

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);
}
+1 -1
View File
@@ -44,7 +44,7 @@ export default class ScriptService {
const pid = await this.scheduleService.runTask(
`real_time=true ${command}`,
this.taskCallbacks(filePath),
{ command, id: relativePath.replace(/ /g, '-') },
{ command, id: relativePath.replace(/ /g, '-'), runOrigin: 'script' },
'start',
);
+3 -2
View File
@@ -88,7 +88,7 @@ export default class SubscriptionService {
this.scheduleService.cancelCronTask(doc as any);
needCreate &&
(await this.scheduleService.createCronTask(
doc as any,
{ ...doc, runOrigin: 'subscription' } as any,
this.taskCallbacks(doc),
runImmediately,
));
@@ -97,7 +97,7 @@ export default class SubscriptionService {
const { type, value } = doc.interval_schedule;
needCreate &&
(await this.scheduleService.createIntervalTask(
doc as any,
{ ...doc, runOrigin: 'subscription' } as any,
{ [type]: value } as SimpleIntervalSchedule,
runImmediately,
this.taskCallbacks(doc),
@@ -329,6 +329,7 @@ export default class SubscriptionService {
schedule: subscription.schedule,
command,
id: String(subscription.id),
runOrigin: 'subscription',
});
}
+12 -4
View File
@@ -92,17 +92,22 @@ export default class SystemService {
info: { ...oDoc.info, ...info },
});
const cron = {
id: result.id || NaN,
id: result.id as number,
name: '删除日志',
command: `ql rmlog ${info.logRemoveFrequency}`,
runOrigin: 'system' as const,
};
if (oDoc.info?.logRemoveFrequency) {
await this.scheduleService.cancelIntervalTask(cron);
}
if (info.logRemoveFrequency && info.logRemoveFrequency > 0) {
this.scheduleService.createIntervalTask(cron, {
days: info.logRemoveFrequency,
});
this.scheduleService.createIntervalTask(
cron,
{
days: info.logRemoveFrequency,
},
true,
);
}
return { code: 200, data: info };
}
@@ -179,6 +184,7 @@ export default class SystemService {
{
command,
id: 'update-node-mirror',
runOrigin: 'system',
},
);
}
@@ -254,6 +260,7 @@ export default class SystemService {
{
command,
id: 'update-linux-mirror',
runOrigin: 'system',
},
);
}
@@ -366,6 +373,7 @@ export default class SystemService {
this.scheduleService.runTask(`real_time=true ${command}`, callback, {
command,
id: command.replace(/ /g, '-'),
runOrigin: 'system',
});
}