mirror of
https://github.com/whyour/qinglong.git
synced 2026-12-01 09:45:36 +08:00
feat: harden cron execution recovery and expose scheduling diagnostics
Reconcile scheduled and system cron triggers, isolate concurrent executions and retain verifiable exit statistics across restarts. Add paged task history, log and script browsing, dashboard timing details and regression coverage.
This commit is contained in:
1 parent
3b6a873520
commit
8a9e70e90b
166 files changed
+18942
-1800
No files matched your search
+59
-28
@@ -3,6 +3,8 @@ import { AddCronRequest, AddCronResponse } from '../protos/cron';
|
||||
import { createCronJob, CronJob } from '../shared/cronScheduler';
|
||||
import { isValidCronSchedule } from '../shared/cronSchedule';
|
||||
import { scheduleStacks } from './data';
|
||||
import { latestMissedSlot } from '../shared/cronRecovery';
|
||||
import { slotId } from '../shared/triggerHistory';
|
||||
import { runCron } from '../shared/runCron';
|
||||
import Logger from '../loaders/logger';
|
||||
import { t, tf } from '../shared/i18n';
|
||||
@@ -12,9 +14,12 @@ const isValidCronField = (cron: string): boolean => {
|
||||
return isValidCronSchedule(cron);
|
||||
};
|
||||
|
||||
const definitions = new WeakMap<CronJob[], string>();
|
||||
|
||||
const addCron = (
|
||||
call: ServerUnaryCall<AddCronRequest, AddCronResponse>,
|
||||
callback: sendUnaryData<AddCronResponse>,
|
||||
savePlan?: () => Promise<void>,
|
||||
) => {
|
||||
// ===== 第一遍:预校验所有 cron 表达式 =====
|
||||
const validationErrors: string[] = [];
|
||||
@@ -54,26 +59,41 @@ const addCron = (
|
||||
|
||||
// Prepare stopped jobs first: construction errors must preserve the old snapshot.
|
||||
const prepared = new Map<string, CronJob[]>();
|
||||
const desired = new Map(call.request.crons.map((item) => [item.id, item]));
|
||||
const recoveries: (() => Promise<void>)[] = [];
|
||||
try {
|
||||
for (const item of call.request.crons) {
|
||||
for (const item of desired.values()) {
|
||||
const signature = JSON.stringify([item.schedule, item.command,
|
||||
(item.extra_schedules || []).map((s) => s.schedule), item.max_queue_wait || 0,
|
||||
!!item.skip_if_pending, item.catchup_seconds || 0, item.catchup_seconds || item.skip_if_pending ? item.recovery_from : 0]);
|
||||
const current = scheduleStacks.get(item.id);
|
||||
if (current && definitions.get(current) === signature) continue;
|
||||
const jobs: CronJob[] = [];
|
||||
prepared.get(item.id)?.forEach((job) => job.cancel());
|
||||
definitions.set(jobs, signature);
|
||||
prepared.set(item.id, jobs);
|
||||
for (const schedule of [
|
||||
for (const [ruleIndex, schedule] of [
|
||||
item.schedule,
|
||||
...(item.extra_schedules || []).map((x) => x.schedule),
|
||||
]) {
|
||||
].entries()) {
|
||||
const fire = async (date: Date, source: 'node' | 'recovery' = 'node') => {
|
||||
const isCurrent = () => scheduleStacks.get(item.id) === jobs;
|
||||
if (!isCurrent()) return;
|
||||
await runCron(item.command, item, isCurrent, date && {
|
||||
id: slotId(item.id, ruleIndex, schedule, +date), time: +date, source,
|
||||
});
|
||||
};
|
||||
if (call.request.replace && item.catchup_seconds && item.recovery_from) {
|
||||
recoveries.push(async () => {
|
||||
const now = Date.now();
|
||||
const since = Math.max(item.recovery_from!, now - item.catchup_seconds! * 1000);
|
||||
const time = latestMissedSlot(schedule, since, now);
|
||||
if (time !== null) await fire(new Date(time), 'recovery');
|
||||
});
|
||||
}
|
||||
jobs.push(
|
||||
createCronJob(
|
||||
schedule,
|
||||
async () => {
|
||||
Logger.info(t('[schedule][准备运行任务] 命令: %s'), item.command);
|
||||
await runCron(
|
||||
item.command,
|
||||
item,
|
||||
() => scheduleStacks.get(item.id) === jobs,
|
||||
);
|
||||
},
|
||||
fire,
|
||||
{
|
||||
name: `${item.id}: ${item.name || ''}`,
|
||||
logger: Logger,
|
||||
@@ -94,23 +114,34 @@ const addCron = (
|
||||
return;
|
||||
}
|
||||
|
||||
if (call.request.replace) {
|
||||
for (const jobs of scheduleStacks.values())
|
||||
jobs.forEach((job) => job?.cancel());
|
||||
scheduleStacks.clear();
|
||||
}
|
||||
for (const [id, jobs] of prepared) {
|
||||
scheduleStacks.get(id)?.forEach((job) => job.cancel());
|
||||
scheduleStacks.set(id, jobs);
|
||||
jobs.forEach((job) => job.start());
|
||||
Logger.info(
|
||||
t('[schedule][创建定时任务] 任务ID: %s, 规则数: %s'),
|
||||
id,
|
||||
jobs.length,
|
||||
);
|
||||
}
|
||||
const activate = () => {
|
||||
if (call.request.replace) {
|
||||
for (const [id, jobs] of scheduleStacks) {
|
||||
if (desired.has(id)) continue;
|
||||
jobs.forEach((job) => job?.cancel());
|
||||
scheduleStacks.delete(id);
|
||||
}
|
||||
}
|
||||
for (const [id, jobs] of prepared) {
|
||||
scheduleStacks.get(id)?.forEach((job) => job.cancel());
|
||||
scheduleStacks.set(id, jobs);
|
||||
jobs.forEach((job) => job.start());
|
||||
Logger.info(
|
||||
t('[schedule][创建定时任务] 任务ID: %s, 规则数: %s'),
|
||||
id,
|
||||
jobs.length,
|
||||
);
|
||||
}
|
||||
|
||||
callback(null, null);
|
||||
callback(null, null);
|
||||
for (const recover of recoveries) setImmediate(() => recover().catch((err) => Logger.error(String(err))));
|
||||
};
|
||||
if (savePlan) {
|
||||
savePlan().then(activate).catch(error => {
|
||||
for (const jobs of prepared.values()) jobs.forEach(job => job.cancel());
|
||||
callback(error);
|
||||
});
|
||||
} else activate();
|
||||
};
|
||||
|
||||
export { addCron };
|
||||
Reference in new issue
Block a user