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

+254 -24
View File
@@ -1,26 +1,55 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const ts = require('typescript');
const { ScheduleType } = require('../../back/interface/schedule');
const load = require('../helpers/load-security-module.cjs');
const {
makeCommand,
nodeCron,
} = require('../../back/cron/scheduling/cronScheduling');
function fixture(mode, failAt) {
const events = [];
const step = (name, ...args) => {
events.push([name, ...args]);
if (name === failAt) throw Error(`failed ${name}`);
};
const Service = load('back/services/cron.ts', {
'../config/index': { crontabFile: '/unused' },
'../config/util': {},
'../data/cron': {
CrontabModel: {
sequelize: {},
update: async (values, options) => step('saved', values, options.where),
},
},
'../data/runningInstance': {},
'../cron/execution/cronLimit': {},
'../shared/logStreamManager': {},
'../schedule/client': {
readiness: { invalidate: () => step('invalidate') },
},
'../shared/utils': {
writeFileWithLock: async (file, contents) =>
step('write', file, contents),
},
'../cron/scheduling/systemScheduleAudit': {
stageSystemPlan: async (db, rules) => {
step('stage', rules);
return 42;
},
confirmSystemPlan: async (db, plan) => step('confirm', plan),
suspendSystemPlan: async () => step('suspend'),
},
child_process: { execFileSync: (...args) => step('install', ...args) },
'../shared/i18n': { t: (value) => value },
}).default;
const service = new Service({ error: (...args) => step('error', ...args) });
Object.defineProperty(service, 'schedulerMode', { value: mode });
return { service, events };
}
test('system and node modes route extended cron once and keep portable rules in crontab', async () => {
const source = fs.readFileSync('back/services/cron.ts', 'utf8');
const begin = source.indexOf(' private isNodeCron(');
const end = source.indexOf(' private async getLogName', begin);
const saveBegin = source.indexOf(' private async setCrontab(');
const saveEnd = source.indexOf(' public importCrontab(', saveBegin);
const js = ts.transpileModule(`class Fixture {${source.slice(begin, end)}${source.slice(saveBegin, saveEnd)}}; module.exports = Fixture;`, {
compilerOptions: { target: ts.ScriptTarget.ES2020 },
}).outputText;
for (const mode of ['system', 'node']) {
const module = { exports: {} };
let file = '', installs = 0;
new Function('module', 'process', 'execFileSync', 'ScheduleType', 'config', 'writeFileWithLock', 'CrontabModel', 'shellQuote', 'stageSystemPlan', 'confirmSystemPlan', 'suspendSystemPlan', js)(
module, { env: { QL_SCHEDULER: mode } }, () => { installs++; }, ScheduleType,
{ crontabFile: '/unused' }, async (_, data) => { file = data; }, { update: async () => {} }, require('../../back/shared/shellQuote').shellQuote, async () => 1, async () => {}, async () => {},
);
const service = new module.exports();
const { service, events } = fixture(mode);
service.makeCommand = (row) => `task ${row.id}.js`;
const rows = [
{ id: 'plain', schedule: '*/5 0-23 * * 1,3' },
@@ -40,20 +69,221 @@ test('system and node modes route extended cron once and keep portable rules in
{ id: 'named', schedule: '0 0 * JAN MON' },
{ id: 'macro', schedule: '@daily' },
{ id: 'short', schedule: '*' },
{ id: 'extra', schedule: '* * * * *', extra_schedules: [{ schedule: '0 * * * *' }] },
{
id: 'extra',
schedule: '* * * * *',
extra_schedules: [{ schedule: '0 * * * *' }],
},
{ id: 'wait', schedule: '* * * * *', max_queue_wait: 30 },
{ id: 'catchup', schedule: '* * * * *', catchup_seconds: 60 },
{ id: 'pending', schedule: '* * * * *', skip_if_pending: 1 },
{ id: 'once', schedule: '@once' },
{ id: 'boot', schedule: '@boot' },
];
for (const row of rows) {
const special = ['once', 'boot'].includes(row.id);
const portable = ['plain', 'spaces'].includes(row.id);
assert.equal(service.shouldUseCronClient(row), !special && (mode === 'node' || !portable), `${mode}: ${row.id}`);
assert.equal(
service.shouldUseCronClient(row),
!special && (mode === 'node' || !portable),
`${mode}: ${row.id}`,
);
}
await service.setCrontab({ data: rows, total: rows.length });
assert.equal(installs, mode === 'system' ? 1 : 0);
for (const id of ['numeric-step', 'sunday-seven', 'day-step', 'restricted-days', 'restricted-week', 'annually', 'midnight', 'minutely', 'seconds', 'question', 'last', 'nth', 'named', 'macro', 'short', 'extra', 'once', 'boot']) {
const line = file.split('\n').find((line) => line.endsWith(`task ${id}.js`));
assert.ok(line.startsWith('# '), `${mode}: ${id} cannot also run from system crontab`);
assert.deepEqual(
events.map(([name]) => name),
mode === 'system'
? ['stage', 'write', 'install', 'confirm']
: ['suspend', 'write'],
);
const file = events.find(([name]) => name === 'write')[2];
for (const id of rows
.filter((row) => !['plain', 'spaces'].includes(row.id))
.map((row) => row.id)) {
const line = file
.split('\n')
.find((line) => line.endsWith(`task ${id}.js`));
assert.ok(
line.startsWith('# '),
`${mode}: ${id} cannot also run from system crontab`,
);
}
for (const id of ['plain', 'spaces']) {
const line = file.split('\n').find((line) => line.includes(`ID=${id} `));
assert.ok(!line.startsWith('# '));
assert.ok(line.includes('QL_SYSTEM_DISPATCHED=$(date +\\%s)'));
assert.ok(
line.includes(
'QL_TRIGGER_SOURCE=system bash "$QL_DIR/shell/system-cron.sh"',
),
);
}
}
});
test('only acknowledged registrations become saved after system publication completes', async () => {
const { service, events } = fixture('system');
const rows = [
{ id: 1, name: 'portable', command: 'a.js', schedule: '* * * * *' },
{ id: 2, command: 'b.js', schedule: '@minutely' },
];
await service.setCrontab({ data: rows, total: rows.length }, [1]);
assert.deepEqual(
events.map(([name]) => name),
['stage', 'write', 'install', 'confirm', 'saved'],
);
assert.deepEqual(events[0][1], [
{ id: 1, name: 'portable', schedule: '* * * * *' },
]);
assert.deepEqual(events.at(-1), [
'saved',
{ saved: true },
{ id: [1], saved: false },
]);
});
for (const mode of ['system', 'node']) {
for (const phase of mode === 'system'
? ['stage', 'write', 'install', 'confirm']
: ['suspend', 'write']) {
test(`${mode} publication failure at ${phase} invalidates readiness without acknowledging schedules`, async () => {
const { service, events } = fixture(mode, phase);
const rows = [
{ id: 1, command: 'a.js', schedule: '* * * * *' },
{ id: 2, command: 'b.js', schedule: '* * * * *', isDisabled: 1 },
{ id: 3, command: 'c.js', schedule: '@minutely' },
{ id: 4, command: 'd.js', schedule: '@boot' },
];
await assert.rejects(
service.setCrontab({ data: rows, total: rows.length }, [1, 3]),
(error) =>
error.status === 503 && /旧规则可能仍在运行/.test(error.message),
);
const sequence =
mode === 'system'
? ['stage', 'write', 'install', 'confirm']
: ['suspend', 'write'];
assert.deepEqual(
events.map(([name]) => name),
[
...sequence.slice(0, sequence.indexOf(phase) + 1),
'error',
'invalidate',
...(mode === 'system' ? ['saved'] : []),
],
);
if (mode === 'system') {
assert.deepEqual(events.at(-1), [
'saved',
{ saved: false },
{ id: [1, 2] },
]);
}
});
}
}
test('manual and scheduled commands preserve the shell contract and Node registration fields', () => {
const doc = {
id: 7,
name: 'task',
command: 'ql repo https://example.test/repo',
schedule: '* * * * *',
log_name: "logs/it's a task",
work_dir: "/scripts/it's here",
task_before: 'echo before;\necho ready',
task_after: 'echo done',
extra_schedules: [{ schedule: '0 0 * * *' }],
max_queue_wait: 30,
catchup_seconds: 60,
skip_if_pending: 1,
recovery_from: 1234,
};
const scheduled = makeCommand(doc);
assert.equal(
scheduled,
"QL_CRON_REVISION=1234 real_time=false no_tee=true ID=7 log_name='logs/it'\\''s a task' task_before='echo before;echo ready' task_after='echo done' work_dir='/scripts/it'\\''s here' ql repo https://example.test/repo",
);
assert.equal(
makeCommand(doc, true),
scheduled.replace('real_time=false', 'real_time=true'),
);
assert.deepEqual(nodeCron(doc, scheduled), {
id: '7',
name: 'task',
command: scheduled,
schedule: '* * * * *',
extra_schedules: [{ schedule: '0 0 * * *' }],
max_queue_wait: 30,
catchup_seconds: 60,
skip_if_pending: true,
recovery_from: 1234,
});
assert.equal(
makeCommand({ id: 8, command: 'script.js' }),
'real_time=false no_tee=true ID=8 task script.js',
);
assert.equal(
makeCommand({ id: 8, command: 'task script.js', log_name: '/dev/null' }),
'real_time=false no_tee=true ID=8 log_name=/dev/null task script.js',
);
});
test('scheduler mode honors explicit overrides and probes crond again for automatic selection', (t) => {
const previous = process.env.QL_SCHEDULER;
t.after(() => {
if (previous === undefined) delete process.env.QL_SCHEDULER;
else process.env.QL_SCHEDULER = previous;
});
let installed = true,
probes = 0;
const { schedulerMode } = load('back/cron/scheduling/cronScheduling.ts', {
child_process: {
execSync: (command, options) => {
probes++;
assert.equal(command, 'which crond');
assert.deepEqual(options, { stdio: 'ignore' });
if (!installed) throw Error('missing crond');
},
},
});
for (const mode of ['system', 'node']) {
process.env.QL_SCHEDULER = mode;
assert.equal(schedulerMode(), mode);
}
assert.equal(probes, 0);
delete process.env.QL_SCHEDULER;
assert.equal(schedulerMode(), 'system');
installed = false;
assert.equal(schedulerMode(), 'node');
process.env.QL_SCHEDULER = 'auto';
installed = true;
assert.equal(schedulerMode(), 'system');
assert.equal(probes, 3);
});
test('a gRPC worker still notifies its parent when system installation fails', async (t) => {
const previousType = process.env.SERVICE_TYPE;
const previousSend = process.send;
const messages = [];
process.env.SERVICE_TYPE = 'grpc';
process.send = (message) => {
messages.push(message);
return true;
};
t.after(() => {
if (previousType === undefined) delete process.env.SERVICE_TYPE;
else process.env.SERVICE_TYPE = previousType;
if (previousSend === undefined) delete process.send;
else process.send = previousSend;
});
const { service } = fixture('system', 'install');
await assert.rejects(
service.setCrontab({
data: [{ id: 1, command: 'a.js', schedule: '* * * * *' }],
total: 1,
}),
(error) => error.status === 503,
);
assert.deepEqual(messages, ['crontab-install-failed']);
});