mirror of
https://github.com/whyour/qinglong.git
synced 2026-12-01 09:45:36 +08:00
feat(cron): expose run receipts and modularize execution
Return committed trigger IDs and add CLI wait and trigger-specific logs while preserving API, CLI, transaction, deduplication and unknown-result semantics. Move scheduling, lifecycle, settlement, queries and legacy adapters into back/cron; remove obsolete internal import paths and circular dependencies. Apply project formatting across the repository, cover CJS/MJS in formatting hooks, add prettier:check and require formatting checks in AGENTS.md. Validation: backend build; 1398 backend/frontend tests passed (17 platform skips); 327 CLI tests passed (6 platform skips); 670 files pass format checks.
This commit is contained in:
1 parent
77ece406ac
commit
c5458596c8
346 files changed
+32133
-11556
No files matched your search
@@ -1,8 +1,8 @@
|
||||
import { storageFull, writeExit } from '../shared/managedExit.cjs';
|
||||
import { storageFull, writeExit } from '../cron/settlement/exitReceipt.cjs';
|
||||
import { QueryTypes } from 'sequelize';
|
||||
import { randomUUID } from 'crypto';
|
||||
import { sequelize } from '../data';
|
||||
import { systemOwner, TriggerHistory } from '../shared/triggerHistory';
|
||||
import { systemOwner, TriggerHistory } from '../cron/history';
|
||||
|
||||
async function receipt() {
|
||||
const [action, task, pid, id, status, end] = process.argv.slice(2);
|
||||
@@ -15,36 +15,68 @@ async function receipt() {
|
||||
const at = Number(process.env.QL_SYSTEM_ENTERED);
|
||||
const rule = process.env.QL_SYSTEM_RULE;
|
||||
const dispatched = Number(process.env.QL_SYSTEM_DISPATCHED) * 1000;
|
||||
const dispatch = Number.isSafeInteger(dispatched) && dispatched > 0 && dispatched <= at ? dispatched : undefined;
|
||||
const wrapper = 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 dispatch =
|
||||
Number.isSafeInteger(dispatched) && dispatched > 0 && dispatched <= at
|
||||
? dispatched
|
||||
: undefined;
|
||||
const wrapper =
|
||||
rule && Number.isSafeInteger(at) && at > 0 && at <= Date.now()
|
||||
? { rule, at, dispatch }
|
||||
: undefined;
|
||||
const acquired = await history.startSystem(
|
||||
triggerId,
|
||||
Number(task),
|
||||
owner,
|
||||
command,
|
||||
wrapper,
|
||||
);
|
||||
if (!acquired) 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.
|
||||
const fromWrapper = process.env.QL_TRIGGER_ID === id && process.env.ID === task;
|
||||
const fromWrapper =
|
||||
process.env.QL_TRIGGER_ID === id && process.env.ID === task;
|
||||
if (!fromWrapper || !id?.startsWith(`system-${Number(task)}-`)) {
|
||||
const [registered] = await sequelize.query(
|
||||
`SELECT 1 FROM CronTriggers WHERE id=$id AND cron_id=$task AND source='system' AND owner LIKE 'crond:%'
|
||||
`SELECT 1 FROM CronTriggers WHERE id=$id AND cron_id=$task AND source='system' AND owner LIKE 'crond:%'
|
||||
UNION ALL SELECT 1 FROM CronLegacyExitBookings WHERE trigger_id=$id AND cron_id=$task AND source='system' LIMIT 1`,
|
||||
{ bind: { id, task: Number(task) }, type: QueryTypes.SELECT });
|
||||
{ bind: { id, task: Number(task) }, type: QueryTypes.SELECT },
|
||||
);
|
||||
if (!registered) return;
|
||||
}
|
||||
const code = Number(status);
|
||||
const finishedAt = end && /^\d{13}$/.test(end) && Number(end) <= Date.now() ? Number(end) : undefined;
|
||||
const finishedAt =
|
||||
end && /^\d{13}$/.test(end) && Number(end) <= Date.now()
|
||||
? Number(end)
|
||||
: undefined;
|
||||
const bound = fromWrapper && finishedAt !== undefined;
|
||||
let journaled = false;
|
||||
if (bound) {
|
||||
try { writeExit(id, code, null, finishedAt); journaled = true; }
|
||||
catch (error) {
|
||||
try {
|
||||
writeExit(id, code, null, finishedAt);
|
||||
journaled = true;
|
||||
} catch (error) {
|
||||
if (!storageFull(error)) throw error;
|
||||
console.error('[crond] Receipt storage full; settling the observed exit in SQLite:', error);
|
||||
console.error(
|
||||
'[crond] Receipt storage full; settling the observed exit in SQLite:',
|
||||
error,
|
||||
);
|
||||
}
|
||||
}
|
||||
try {
|
||||
await history.complete(id, code === 0 ? 'finished' : 'failed', null, code, finishedAt,
|
||||
bound ? { version: 1, code, signal: null, time: finishedAt } : finishedAt === undefined ? undefined : 'legacy');
|
||||
await history.complete(
|
||||
id,
|
||||
code === 0 ? 'finished' : 'failed',
|
||||
null,
|
||||
code,
|
||||
finishedAt,
|
||||
bound
|
||||
? { version: 1, code, signal: null, time: finishedAt }
|
||||
: finishedAt === undefined
|
||||
? undefined
|
||||
: 'legacy',
|
||||
);
|
||||
} catch (error) {
|
||||
if (!journaled || !storageFull(error)) throw error;
|
||||
// The regular recovery pass will settle the receipt after space returns.
|
||||
@@ -54,8 +86,11 @@ async function receipt() {
|
||||
}
|
||||
}
|
||||
|
||||
receipt().catch((error) => {
|
||||
console.error('[crond] Failed to persist execution receipt:', error);
|
||||
// At start, 75 means that another execution already owns the slot.
|
||||
process.exitCode = process.argv[2] === 'finish' && storageFull(error) ? 75 : 1;
|
||||
}).finally(() => sequelize.close());
|
||||
receipt()
|
||||
.catch((error) => {
|
||||
console.error('[crond] Failed to persist execution receipt:', error);
|
||||
// At start, 75 means that another execution already owns the slot.
|
||||
process.exitCode =
|
||||
process.argv[2] === 'finish' && storageFull(error) ? 75 : 1;
|
||||
})
|
||||
.finally(() => sequelize.close());
|
||||
Reference in new issue
Block a user