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

+68 -17
View File
@@ -1,16 +1,26 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const load = require('../helpers/load-security-module.cjs');
const { createCronJob } = require('../../back/shared/cronScheduler');
const { createCronJob } = require('../../back/cron/scheduling/cronScheduler');
const logger = { info() {}, warn() {}, error() {} };
test('native calendar skips nonexistent spring time and duplicate fall hour', async (t) => {
const previous = process.env.TZ;
process.env.TZ = 'America/New_York';
t.after(() => { if (previous === undefined) delete process.env.TZ; else process.env.TZ = previous; });
t.mock.timers.enable({ apis: ['Date', 'setTimeout', 'setInterval'], now: new Date('2026-03-08T05:00:00Z') });
t.after(() => {
if (previous === undefined) delete process.env.TZ;
else process.env.TZ = previous;
});
t.mock.timers.enable({
apis: ['Date', 'setTimeout', 'setInterval'],
now: new Date('2026-03-08T05:00:00Z'),
});
const springRuns = [];
const spring = createCronJob('0 30 2 * * *', (date) => springRuns.push(date.toISOString()), { name: 'spring', logger });
const spring = createCronJob(
'0 30 2 * * *',
(date) => springRuns.push(date.toISOString()),
{ name: 'spring', logger },
);
t.after(() => spring.cancel());
t.mock.timers.tick(new Date('2026-03-09T06:30:00Z') - Date.now());
await new Promise(setImmediate);
@@ -18,7 +28,11 @@ test('native calendar skips nonexistent spring time and duplicate fall hour', as
spring.cancel();
t.mock.timers.setTime(new Date('2026-11-01T05:30:00Z').getTime());
const fallRuns = [];
const fall = createCronJob('0 30 1 * * *', (date) => fallRuns.push(date.toISOString()), { name: 'fall', logger });
const fall = createCronJob(
'0 30 1 * * *',
(date) => fallRuns.push(date.toISOString()),
{ name: 'fall', logger },
);
t.after(() => fall.cancel());
t.mock.timers.tick(new Date('2026-11-02T06:30:00Z') - Date.now());
await new Promise(setImmediate);
@@ -31,28 +45,65 @@ test('construction failure cleans staged jobs and preserves the running snapshot
let count = 0;
const { addCron } = load('back/schedule/addCron.ts', {
'./data': { scheduleStacks: stacks },
'../shared/cronScheduler': { createCronJob: () => {
if (++count === 2) throw Error('construction failed');
return { start() { throw Error('must not start'); }, cancel: () => cancelled.push('staged') };
} },
'../shared/runCron': {}, '../loaders/logger': logger, '../shared/i18n': { tf: require('node:util').format },
'../cron/scheduling/cronScheduler': {
createCronJob: () => {
if (++count === 2) throw Error('construction failed');
return {
start() {
throw Error('must not start');
},
cancel: () => cancelled.push('staged'),
};
},
},
'../cron/execution/runCron': {},
'../loaders/logger': logger,
'../shared/i18n': { tf: require('node:util').format },
});
await assert.rejects(new Promise((resolve, reject) => addCron({ request: {
replace: true, crons: [{ id: 'new', schedule: '* * * * *', extra_schedules: [{ schedule: '0 * * * *' }] }],
} }, (error) => error ? reject(error) : resolve())), /construction failed/);
await assert.rejects(
new Promise((resolve, reject) =>
addCron(
{
request: {
replace: true,
crons: [
{
id: 'new',
schedule: '* * * * *',
extra_schedules: [{ schedule: '0 * * * *' }],
},
],
},
},
(error) => (error ? reject(error) : resolve()),
),
),
/construction failed/,
);
assert.deepEqual([...stacks.keys()], ['old']);
assert.deepEqual(cancelled, ['staged']);
});
test('invalid subscription schedules are isolated and immediate runs without a schedule survive', async () => {
const warnings = [], runs = [];
const warnings = [],
runs = [];
const Service = load('back/services/schedule.ts', {
typedi: { Service: () => (x) => x, Inject: () => () => {} },
'../shared/pLimit': {},
}).default;
const service = new Service({ ...logger, warn: (...args) => warnings.push(args) });
service.runTask = async (...args) => { runs.push(args); };
const item = { id: 123, name: 'subscription', command: 'true', runOrigin: 'subscription' };
const service = new Service({
...logger,
warn: (...args) => warnings.push(args),
});
service.runTask = async (...args) => {
runs.push(args);
};
const item = {
id: 123,
name: 'subscription',
command: 'true',
runOrigin: 'subscription',
};
await service.createCronTask({ ...item, schedule: 'not a cron' });
assert.equal(warnings.length, 1);
assert.equal(service.scheduleStacks.size, 0);