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.
This commit is contained in:
whyour committed 2026-11-25 00:54:41 +08:00
1 parent cddabbaf0e
commit 2f01f756d7
74 files changed
+6891 -837

No files matched your search

+39 -8
View File
@@ -1,8 +1,10 @@
import { executionModel } from '../data/executionModels';
import { storageFull, writeExit } from '../cron/settlement/exitReceipt.cjs';
import { QueryTypes } from 'sequelize';
import { randomUUID } from 'crypto';
import { sequelize } from '../data';
import { systemOwner, TriggerHistory } from '../cron/history';
import { globalCronBudget } from '../cron/execution/globalCronBudget';
async function receipt() {
const [action, task, pid, id, status, end] = process.argv.slice(2);
@@ -23,15 +25,35 @@ async function receipt() {
rule && Number.isSafeInteger(at) && at > 0 && at <= Date.now()
? { rule, at, dispatch }
: undefined;
const acquired = await history.startSystem(
triggerId,
Number(task),
owner,
command,
wrapper,
const queued = await sequelize.query(
`INSERT INTO CronTriggers(id,cron_id,source,state,owner,queued_at,observed_at)
VALUES ($id,$cronId,'system','queued',$owner,$now,$now) ON CONFLICT(id) DO NOTHING`,
{
bind: { id: triggerId, cronId: Number(task), owner, now: Date.now() },
type: QueryTypes.BULKUPDATE,
},
);
if (!acquired) process.exitCode = 75;
else if (!id) process.stdout.write(triggerId);
const budget = globalCronBudget(sequelize);
const token =
queued === 1 &&
(await budget.acquire(triggerId, {
owner,
commandIdentity: command,
current: () => systemOwner(Number(pid)) === owner,
}));
const acquired =
token &&
(await history.startSystem(
triggerId,
Number(task),
owner,
command,
wrapper,
));
if (!acquired) {
if (token) await budget.release(triggerId, token);
process.exitCode = 75;
} else if (!id) process.stdout.write(triggerId);
} else if (action === 'finish') {
// The live wrapper inherited both IDs from the start it just claimed. A
// standalone retry must match the registered system task before writing.
@@ -77,6 +99,15 @@ async function receipt() {
? undefined
: 'legacy',
);
const [lease] = await executionModel(
sequelize,
'CronConcurrencyLeases',
).findAll({
attributes: ['token'],
where: { trigger_id: id },
raw: true,
});
if (lease) await globalCronBudget(sequelize).release(id, lease.token);
} catch (error) {
if (!journaled || !storageFull(error)) throw error;
// The regular recovery pass will settle the receipt after space returns.
+19 -10
View File
@@ -43,17 +43,26 @@ async function probeUpgrade() {
try {
await loadDatabase();
// Exercise an actual on-disk transaction as well as schema initialization.
const { DataTypes } = await import('sequelize');
const probe = sequelize.define<
import('sequelize').Model<{ value: number }>
>(
'UpgradeProbe',
{ value: DataTypes.INTEGER },
{ tableName: 'upgrade_probe', timestamps: false },
);
probe.removeAttribute('id');
await sequelize.transaction(async (transaction) => {
await sequelize.query('CREATE TABLE upgrade_probe (value INTEGER)', {
transaction,
});
await sequelize.query('INSERT INTO upgrade_probe VALUES (42)', {
transaction,
});
const [rows] = await sequelize.query('SELECT value FROM upgrade_probe', {
transaction,
});
if ((rows[0] as { value: number })?.value !== 42)
await sequelize
.getQueryInterface()
.createTable(
'upgrade_probe',
{ value: DataTypes.INTEGER },
{ transaction },
);
await probe.create({ value: 42 }, { transaction });
const row = await probe.findOne({ attributes: ['value'], transaction });
if (row?.get('value') !== 42)
throw new Error('Upgrade database readback failed');
});
inject();