Files
qinglong/back/cron/scheduling/resumeAcceptedCrons.ts
whyour 2f01f756d7 perf(cron): share global concurrency budget and streamline execution
Share durable permits across manual, scheduled and recovered runs, prioritize
short ready tasks with aging, and record global queue occupancy.

Reduce launch, log polling, dashboard and SQLite overhead while preserving
transactional receipts, cancellation, recovery and script hook compatibility.
Keep ordinary CRUD in ORM and bound SQL on measured execution hot paths.

Fix supervisor descendant tracking, asynchronous log draining, concurrent log
directory creation, Linux fallback launches and snapshot clock rollback.
Include regression coverage, SQL audits and evolution/review documentation.

Validation: five review rounds; local full suite 1478 passed, 28 platform skips;
dev suites 415 passed, no failures/skips; backend types and formatting passed.
2026-11-25 00:54:41 +08:00

52 lines
1.5 KiB
TypeScript

import { executionModel } from '../../data/executionModels';
import { Op } from 'sequelize';
import type { ICron } from '../../protos/cron';
import { CrontabModel } from '../../data/cron';
import { slotId } from '../identity';
import { runCron } from '../execution/runCron';
// Accepted work is distinct from catching up timers missed while offline.
// Keep its identity and original queue deadline; never replay a bound command.
export async function resumeAcceptedCrons(item: ICron, current: () => boolean) {
const rows = await executionModel(
CrontabModel.sequelize!,
'CronTriggers',
).findAll({
attributes: ['id', 'scheduled_at', 'queued_at'],
where: {
cron_id: Number(item.id),
source: { [Op.in]: ['node', 'recovery'] },
state: 'skipped',
reason: 'restart',
resume_pending: 1,
started_at: null,
queued_at: { [Op.gte]: item.recovery_from || 0 },
},
order: [
['queued_at', 'ASC'],
['id', 'ASC'],
],
raw: true,
});
const rules = [
item.schedule,
...(item.extra_schedules || []).map((s) => s.schedule),
];
await Promise.all(
rows
.filter((row) =>
rules.some(
(rule, i) => slotId(item.id, i, rule, row.scheduled_at!) === row.id,
),
)
.map((row) =>
runCron(item.command, item, current, {
id: row.id,
time: row.scheduled_at!,
queuedAt: row.queued_at!,
source: 'recovery',
}),
),
);
}