增加任务重复运行提醒

This commit is contained in:
whyour
2024-08-23 09:37:26 +08:00
parent f4cb3eacf8
commit 8b8eae211b
7 changed files with 88 additions and 17 deletions
+16 -3
View File
@@ -42,7 +42,7 @@ export default class ScheduleService {
private maxBuffer = 200 * 1024 * 1024;
constructor(@Inject('logger') private logger: winston.Logger) { }
constructor(@Inject('logger') private logger: winston.Logger) {}
async runTask(
command: string,
@@ -51,12 +51,19 @@ export default class ScheduleService {
schedule?: string;
name?: string;
command?: string;
id: string;
},
completionTime: 'start' | 'end' = 'end',
) {
return taskLimit.runWithCronLimit(() => {
return taskLimit.runWithCronLimit(params, () => {
return new Promise(async (resolve, reject) => {
this.logger.info(`[panel][开始执行任务] 参数 ${JSON.stringify({ ...params, command })}`);
taskLimit.removeQueuedCron(params.id);
this.logger.info(
`[panel][开始执行任务] 参数 ${JSON.stringify({
...params,
command,
})}`,
);
try {
const startTime = dayjs();
@@ -131,6 +138,7 @@ export default class ScheduleService {
name,
schedule,
command,
id: _id,
});
}),
);
@@ -140,6 +148,7 @@ export default class ScheduleService {
name,
schedule,
command,
id: _id,
});
}
}
@@ -148,6 +157,7 @@ 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);
}
@@ -172,6 +182,7 @@ export default class ScheduleService {
this.runTask(command, callbacks, {
name,
command,
id: _id,
});
},
(err) => {
@@ -195,6 +206,7 @@ export default class ScheduleService {
this.runTask(command, callbacks, {
name,
command,
id: _id,
});
}
}
@@ -202,6 +214,7 @@ 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);
}
+3 -1
View File
@@ -7,6 +7,7 @@ import ScheduleService, { TaskCallbacks } from './schedule';
import config from '../config';
import { TASK_COMMAND } from '../config/const';
import { getFileContentByName, getPid, killTask, rmPath } from '../config/util';
import taskLimit from '../shared/pLimit';
@Service()
export default class ScriptService {
@@ -43,7 +44,7 @@ export default class ScriptService {
const pid = await this.scheduleService.runTask(
`real_time=true ${command}`,
this.taskCallbacks(filePath),
{ command },
{ command, id: relativePath.replace(/ /g, '-') },
'start',
);
@@ -53,6 +54,7 @@ export default class ScriptService {
public async stopScript(filePath: string, pid: number) {
if (!pid) {
const relativePath = path.relative(config.scriptPath, filePath);
taskLimit.removeQueuedCron(relativePath.replace(/ /g, '-'));
pid = (await getPid(`${TASK_COMMAND} ${relativePath} now`)) as number;
}
try {
+3
View File
@@ -29,6 +29,7 @@ import { LOG_END_SYMBOL } from '../config/const';
import { formatCommand, formatUrl } from '../config/subscription';
import { CrontabModel } from '../data/cron';
import CrontabService from './cron';
import taskLimit from '../shared/pLimit';
@Service()
export default class SubscriptionService {
@@ -301,6 +302,7 @@ export default class SubscriptionService {
for (const doc of docs) {
if (doc.pid) {
try {
taskLimit.removeQueuedCron(String(doc.id));
await killTask(doc.pid);
} catch (error) {
this.logger.error(error);
@@ -326,6 +328,7 @@ export default class SubscriptionService {
name: subscription.name,
schedule: subscription.schedule,
command,
id: String(subscription.id),
});
}
+4
View File
@@ -178,6 +178,7 @@ export default class SystemService {
},
{
command,
id: 'update-node-mirror',
},
);
}
@@ -252,6 +253,7 @@ export default class SystemService {
},
{
command,
id: 'update-linux-mirror',
},
);
}
@@ -363,6 +365,7 @@ export default class SystemService {
}
this.scheduleService.runTask(`real_time=true ${command}`, callback, {
command,
id: command.replace(/ /g, '-'),
});
}
@@ -371,6 +374,7 @@ export default class SystemService {
return { code: 400, message: '参数错误' };
}
taskLimit.removeQueuedCron(command.replace(/ /g, '-'));
if (pid) {
await killTask(pid);
return { code: 200 };