Files
qinglong/back/shared/dashboardSnapshot.ts
T
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

36 lines
1.1 KiB
TypeScript

import type { Sequelize } from 'sequelize';
import { performance } from 'perf_hooks';
// Per database and day: concurrent viewers share one computation. Rejections
// are never cached; a new day cannot reuse yesterday's summary. Runtime/task
// control endpoints remain live and never read this display-only snapshot.
const snapshots = new WeakMap<
Sequelize,
{ key: string; until: number; value: Promise<unknown> }
>();
export function dashboardSnapshot<T>(
db: Sequelize,
key: string,
compute: () => Promise<T>,
): Promise<T> {
const existing = snapshots.get(db);
if (existing?.key === key && performance.now() < existing.until)
return existing.value as Promise<T>;
const entry = {
key,
until: Infinity,
value: Promise.resolve().then(compute),
};
snapshots.set(db, entry);
entry.value.then(
() => {
// Wall-clock corrections must not stretch a one-second cache for minutes.
entry.until = performance.now() + 1000;
},
() => {
if (snapshots.get(db) === entry) snapshots.delete(db);
},
);
return entry.value;
}