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:
whyour committed 2026-11-22 12:24:29 +08:00
1 parent 77ece406ac
commit c5458596c8
346 files changed
+32133 -11556

No files matched your search

+62 -19
View File
@@ -3,9 +3,14 @@ const assert = require('node:assert/strict');
const fs = require('node:fs');
const ts = require('typescript');
const load = require('../helpers/load-security-module.cjs');
const { SchedulerReadiness } = require('../../back/shared/schedulerReadiness');
const {
SchedulerReadiness,
} = require('../../back/cron/scheduling/schedulerReadiness');
const { AddCronRequest } = require('../../back/protos/cron');
const { getInvalidCronSchedules } = require('../../back/shared/cronSchedule');
const {
getInvalidCronSchedules,
} = require('../../back/cron/scheduling/cronSchedule');
const { nodeCron } = require('../../back/cron/scheduling/cronScheduling');
function fixture(client) {
const source = fs.readFileSync('back/services/cron.ts', 'utf8');
@@ -16,12 +21,21 @@ function fixture(client) {
{ compilerOptions: { target: ts.ScriptTarget.ES2020 } },
).outputText;
const module = { exports: {} };
new Function('module', 'isDemoEnv', 'cronClient', 'withSchedulerMutation', 'getInvalidCronSchedules', js)(
new Function(
'module',
'isDemoEnv',
'cronClient',
'withSchedulerMutation',
'getInvalidCronSchedules',
'nodeCron',
js,
)(
module,
() => false,
client,
(fn) => fn(),
getInvalidCronSchedules,
nodeCron,
);
const service = new module.exports();
service.setCrontab = async () => {};
@@ -42,12 +56,13 @@ test('recovery reconciles a surviving scheduler after missed deletes and disable
);
const { addCron } = load('back/schedule/addCron.ts', {
'./data': { scheduleStacks: stacks },
'../shared/cronScheduler': {
'../cron/scheduling/cronScheduler': {
createCronJob: (_schedule, _callback, options) => ({
start() {}, cancel: () => cancelled.push(options.name.split(':')[0]),
start() {},
cancel: () => cancelled.push(options.name.split(':')[0]),
}),
},
'../shared/runCron': {},
'../cron/execution/runCron': {},
'../loaders/logger': { info() {}, warn() {} },
'../shared/i18n': { t: (s) => s, tf: (s) => s },
});
@@ -96,7 +111,7 @@ test('invalid replacement leaves the previous schedule intact', async () => {
]);
const { addCron } = load('back/schedule/addCron.ts', {
'./data': { scheduleStacks: stacks },
'../shared/runCron': {},
'../cron/execution/runCron': {},
'../loaders/logger': {},
'../shared/i18n': { t: (s) => s, tf: (s) => s },
});
@@ -121,22 +136,41 @@ test('invalid persisted schedules cannot block recovery, valid jobs or HTTP heal
const warnings = [];
const { addCron } = load('back/schedule/addCron.ts', {
'./data': { scheduleStacks: stacks },
'../shared/runCron': {},
'../cron/execution/runCron': {},
'../loaders/logger': { info() {}, warn() {} },
'../shared/i18n': { t: (s) => s, tf: require('node:util').format },
});
const client = { addCron: (crons, replace) => new Promise((resolve, reject) => {
addCron({ request: { crons, replace } }, (error) => error ? reject(error) : resolve());
}) };
const client = {
addCron: (crons, replace) =>
new Promise((resolve, reject) => {
addCron({ request: { crons, replace } }, (error) =>
error ? reject(error) : resolve(),
);
}),
};
const service = fixture(client);
let snapshot;
service.setCrontab = async (tabs) => { snapshot = tabs.data; };
service.logger.warn = (...args) => warnings.push(require('node:util').format(...args));
service.setCrontab = async (tabs) => {
snapshot = tabs.data;
};
service.logger.warn = (...args) =>
warnings.push(require('node:util').format(...args));
let rows = [
{ id: 'good', name: 'valid', schedule: '0 0/30 * * * ?', isDisabled: 0 },
{ id: 'bad-main', name: 'bad main', schedule: 'not a cron', isDisabled: 0 },
{ id: 'bad-extra', name: 'bad extra', schedule: '* * * * *', extra_schedules: [{ schedule: '0 70 * * * ?' }], isDisabled: 0 },
{ id: 'unsupported', name: 'unsupported', schedule: 'H * * * *', isDisabled: 0 },
{
id: 'bad-extra',
name: 'bad extra',
schedule: '* * * * *',
extra_schedules: [{ schedule: '0 70 * * * ?' }],
isDisabled: 0,
},
{
id: 'unsupported',
name: 'unsupported',
schedule: 'H * * * *',
isDisabled: 0,
},
{ id: 'disabled', name: 'disabled', schedule: 'bad', isDisabled: 1 },
];
service.crontabs = async () => ({ data: rows });
@@ -148,20 +182,29 @@ test('invalid persisted schedules cannot block recovery, valid jobs or HTTP heal
state.configure(() => service.autosave_crontab(true));
assert.equal(await state.recover(), true);
assert.deepEqual([...stacks.keys()], ['good']);
assert.deepEqual(snapshot.map((x) => x.id), ['good', 'disabled']);
assert.deepEqual(
snapshot.map((x) => x.id),
['good', 'disabled'],
);
assert.equal(rows.length, 5, 'persisted rows are preserved for editing');
assert.equal(rows[1].isDisabled, 0);
assert.equal(warnings.length, 3);
assert.match(warnings.join('\n'), /bad-main.*bad main.*not a cron/);
assert.match(warnings.join('\n'), /bad-extra.*0 70/);
const { HealthService } = load('back/services/health.ts', {
typedi: { Service: () => (x) => x }, './http': {},
'../schedule/client': { readiness: state }, '../loaders/logger': { error() {} },
typedi: { Service: () => (x) => x },
'./http': {},
'../schedule/client': { readiness: state },
'../loaders/logger': { error() {} },
});
const health = new HealthService({ getServer: () => ({}) });
assert.equal((await health.check()).status, 'ok');
rows = rows.filter((x) => x.id !== 'good');
assert.equal(await state.recover(), true, 'even all-invalid snapshots can recover');
assert.equal(
await state.recover(),
true,
'even all-invalid snapshots can recover',
);
assert.equal(stacks.size, 0);
assert.equal((await health.check()).status, 'ok');
});