Files
qinglong/test/helpers/receipt-boundary-worker.cjs
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

95 lines
3.3 KiB
JavaScript

// Run real panel operations in a separate process. The parent replaces public
// files and runs two independent publishers while this worker is at the barrier.
const fs = require('node:fs');
const path = require('node:path');
const [operation, stage, side, id] = process.argv.slice(2);
const root = process.env.QL_DATA_DIR;
const load = require('./load-security-module.cjs');
const db = load('back/data/index.ts', {
'../config/index': { dbPath: root },
}).sequelize;
const { TriggerHistory } = require('../../back/cron/history');
const history = new TriggerHistory(db);
const stats = load('back/data/cronStats.ts', { '.': { sequelize: db } });
const running = load('back/data/runningInstance.ts', {
'.': { sequelize: db },
});
const Retention = load('back/services/retention.ts', {
'../data': { sequelize: db },
'../data/cronStats': stats,
'../data/runningInstance': running,
'../data/system': {},
'../config': {},
}).default;
function barrier() {
const ready = path.join(root, 'at-barrier');
fs.writeFileSync(ready + '.tmp', stage + '/' + side);
fs.renameSync(ready + '.tmp', ready);
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0);
}
const statements = {
booking: 'INSERT OR IGNORE INTO CronLegacyExitBookings',
claim: 'UPDATE CronTriggers SET counted=',
increment: 'INSERT INTO "CrontabStats"',
dispute: 'INSERT OR IGNORE INTO CronBoundExitConflicts',
debit: 'UPDATE CrontabStats SET run_count=run_count-1',
commit: 'COMMIT',
deleteDay: 'DELETE FROM CrontabStats WHERE date',
cutoff: 'INSERT INTO CronStatRetention',
historyCutoff: 'INSERT INTO CronHistoryRetention',
deleteHistory: 'DELETE FROM CronTriggers WHERE',
pruneBooking: 'DELETE FROM CronLegacyExitBookings WHERE day',
};
function intercept(object, method, matches) {
const original = object[method].bind(object);
object[method] = async (...args) => {
const matched = matches(...args);
if (matched && side === 'before') barrier();
const result = await original(...args);
if (matched && side === 'after') barrier();
return result;
};
}
const captured = (file) =>
path.basename(file).startsWith('.pending.' + id + '.r-');
if (stage === 'readClaim') {
const open = fs.promises.open.bind(fs.promises);
fs.promises.open = async (file, ...options) => {
const handle = await open(file, ...options);
if (captured(file)) {
if (side === 'before') barrier();
const close = handle.close.bind(handle);
handle.close = async () => {
await close();
if (side === 'after') barrier();
};
}
return handle;
};
} else if (stage === 'move')
intercept(fs.promises, 'rename', (src) => path.basename(src) === id);
else if (stage === 'unlink') intercept(fs.promises, 'unlink', captured);
else
intercept(db, 'query', (sql) =>
String(typeof sql === 'string' ? sql : sql.query)
.replace(/[`"]/g, '')
.startsWith(statements[stage].replace(/"/g, '')),
);
(async () => {
if (operation === 'prune')
await new Retention().cleanup({
runningInstanceRetentionDays: 1,
cronStatRetentionDays: 1,
});
else await history.complete(id, 'unknown', null, null);
throw Error('Expected barrier was never reached: ' + operation + '/' + stage);
})()
.catch((e) => {
console.error(e);
process.exitCode = 1;
})
.finally(() => db.close());