feat: harden cron execution recovery and expose scheduling diagnostics

Reconcile scheduled and system cron triggers, isolate concurrent executions and retain verifiable exit statistics across restarts. Add paged task history, log and script browsing, dashboard timing details and regression coverage.
This commit is contained in:
whyour committed 2026-11-17 01:01:57 +08:00
1 parent 3b6a873520
commit 8a9e70e90b
166 files changed
+18942 -1800

No files matched your search

+68 -96
View File
@@ -4,33 +4,8 @@ const fs = require('node:fs/promises');
const os = require('node:os');
const path = require('node:path');
const { createContext } = require('../../dist/internal/runtime/context');
const { extendedLifecycle } = require('../../dist/internal/runtime/lifecycle');
const { loggedOperation } = require('../../dist/internal/runtime/commandLog');
test('installed panel version selects lifecycle schema and unknown versions stay conservative', async (t) => {
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'ql-lifecycle-'));
t.after(() => fs.rm(root, { recursive: true, force: true }));
const context = createContext({ root }, {});
assert.equal(await extendedLifecycle(context), false);
for (const [version, expected] of [
['2.20.1', false],
['2.19.0', false],
['2.21.0-14', true],
['3.0.0', false],
['invalid', false],
]) {
await fs.writeFile(
path.join(root, 'package.json'),
JSON.stringify({ version }),
);
assert.equal(await extendedLifecycle(context), expected);
}
context.env.QL_CLI_LIFECYCLE = 'extended';
assert.equal(await extendedLifecycle(context), true);
context.env.QL_CLI_LIFECYCLE = 'legacy';
assert.equal(await extendedLifecycle(context), false);
});
test('command log preserves scheduler path and rejects escaping the log directory', async (t) => {
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'ql-scheduler-log-'));
t.after(() => fs.rm(root, { recursive: true, force: true }));
@@ -50,85 +25,82 @@ test('command log preserves scheduler path and rejects escaping the log director
);
});
test('released version.yaml selects lifecycle only when package version is absent', async (t) => {
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'ql-release-version-'));
t.after(() => fs.rm(root, { recursive: true, force: true }));
const context = createContext({ root }, {});
await fs.writeFile(
path.join(root, 'package.json'),
'{"name":"released-panel"}',
);
for (const [release, expected] of [
['version: 2.20.1\nchangeLog: |\n version: 2.99.0\n', false],
['version: 2.21.0-14\npublishTime: 2026-09-25\n', true],
['version: "2.21.1" # release\n', true],
["\ufeffversion: '2.22.0'\r\n", true],
['version: 3.0.0\n', false],
['version: 2.21.invalid\n', false],
['version: 2.21.0\nversion: 2.20.1\n', false],
['changeLog: |\n version: 2.21.0\n', false],
['version: !custom 2.21.0\n', false],
]) {
await fs.writeFile(path.join(root, 'version.yaml'), release);
assert.equal(await extendedLifecycle(context), expected, release);
}
await fs.writeFile(path.join(root, 'version.yaml'), 'version: 2.21.1\n');
await fs.writeFile(path.join(root, 'package.json'), '{"version":"2.20.1"}');
assert.equal(await extendedLifecycle(context), false);
context.env.QL_CLI_LIFECYCLE = 'extended';
assert.equal(await extendedLifecycle(context), true);
context.env.QL_CLI_LIFECYCLE = 'legacy';
assert.equal(await extendedLifecycle(context), false);
delete context.env.QL_CLI_LIFECYCLE;
await fs.rm(path.join(root, 'package.json'));
assert.equal(await extendedLifecycle(context), true);
await fs.rm(path.join(root, 'version.yaml'));
assert.equal(await extendedLifecycle(context), false);
});
test('release-only version metadata controls actual task and maintenance status payloads', async (t) => {
test('managed CLI tasks and maintenance commands report against the parent receipt', async t => {
const { LocalApi } = require('../../dist/internal/runtime/api');
const { executeTask } = require('../../dist/internal/execution/taskRunner');
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'ql-release-payload-'));
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'ql-managed-cli-'));
t.after(() => fs.rm(root, { recursive: true, force: true }));
const context = createContext(
{ root },
{ PATH: process.env.PATH, ID: '7', no_tee: 'true' },
);
const id = 'parent-receipt-7';
const context = createContext({ root }, {
PATH: process.env.PATH, ID: '7', no_tee: 'true', QL_TRIGGER_ID: id,
QL_CLI_LIFECYCLE: 'extended', QL_EXECUTION_ORIGIN: 'scheduled_system',
});
await fs.mkdir(context.paths.dir_scripts, { recursive: true });
await fs.writeFile(
path.join(context.paths.dir_scripts, 'fixture.sh'),
'return 7',
);
await fs.writeFile(path.join(context.paths.dir_scripts, 'fixture.sh'), 'return 7');
const calls = [];
t.mock.method(LocalApi.prototype, 'call', async (endpoint, method, body) => {
calls.push({ endpoint, body });
return { code: 200 };
});
for (const version of ['2.20.1', '2.21.0-14']) {
await fs.writeFile(
path.join(root, 'version.yaml'),
`version: ${version}\n`,
);
calls.length = 0;
const result = await executeTask(context, {
argv: ['fixture.sh'],
mode: 'now',
});
assert.equal(result.exitCode, 7);
const final = calls
.filter((row) => row.endpoint === 'crons/status')
.at(-1).body;
assert.equal(final.exit_code, version === '2.20.1' ? undefined : 7);
assert.equal(
calls.some((row) => row.endpoint === 'dashboard/record'),
version !== '2.20.1',
);
calls.length = 0;
await loggedOperation(context, 'extra', async () => true);
assert.equal(
calls.at(-1).body.exit_code,
version === '2.20.1' ? undefined : 0,
);
const result = await executeTask(context, { argv: ['fixture.sh'], mode: 'now' });
assert.equal(result.exitCode, 7);
assert.deepEqual(calls.map(r => r.endpoint), ['crons/status', 'crons/status', 'dashboard/record']);
await loggedOperation(context, 'extra', async () => true);
assert.equal(calls.length, 5);
for (const { body } of calls) {
assert.equal(body.trigger_id, id);
assert.equal(body.execution_id, undefined);
}
calls.length = 0;
context.env.QL_CLI_LIFECYCLE = 'legacy';
await loggedOperation(context, 'extra', async () => true);
assert.ok(calls.length);
assert.ok(calls.every(r => !('trigger_id' in r.body)));
});
test('distinct managed triggers in the same clock tick have separate maintenance and task logs', async t => {
const { executeTask } = require('../../dist/internal/execution/taskRunner');
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'ql-same-tick-'));
t.after(() => fs.rm(root, { recursive: true, force: true }));
t.mock.method(Date, 'now', () => 1800000000000);
const run = async id => {
const context = createContext({ root }, { QL_CLI_LIFECYCLE: 'extended', QL_TRIGGER_ID: id,
PATH: process.env.PATH, no_tee: 'true', log_name: 'shared' });
await fs.mkdir(context.paths.dir_scripts, { recursive: true });
await fs.writeFile(path.join(context.paths.dir_scripts, id + '.sh'), 'echo ' + id);
const command = await loggedOperation(context, 'extra', async () => {});
const task = await executeTask(context, { argv: [id + '.sh'], mode: 'now' });
return { command: command.logPath, file: task.logPath,
content: await fs.readFile(path.join(context.paths.dir_log, task.logPath), 'utf8') };
};
const [a, b] = await Promise.all([run('marker-alpha'), run('marker-bravo')]);
assert.notEqual(a.command, b.command);
assert.notEqual(a.file, b.file);
assert.match(a.content, /marker-alpha/);
assert.doesNotMatch(a.content, /marker-bravo/);
assert.match(b.content, /marker-bravo/);
assert.doesNotMatch(b.content, /marker-alpha/);
});
test('same-tick standalone tasks and commands share a receipt only within one invocation', async t => {
const { LocalApi } = require('../../dist/internal/runtime/api');
const { executeTask } = require('../../dist/internal/execution/taskRunner');
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'ql-client-receipts-'));
t.after(() => fs.rm(root, { recursive: true, force: true }));
t.mock.method(Date, 'now', () => 1800000000000);
const context = createContext({root}, {ID:'5', QL_CLI_LIFECYCLE:'extended', no_tee:'true',
real_log_path:'shared.log', PATH:process.env.PATH});
await fs.mkdir(context.paths.dir_scripts, {recursive:true});
await fs.writeFile(path.join(context.paths.dir_scripts, 'receipt.sh'), 'exit 0');
const groups = new Map();
t.mock.method(LocalApi.prototype, 'call', async (endpoint, method, body) => {
const id = body.execution_id;
assert.match(id, /^client-[0-9a-f-]{36}$/);
groups.set(id, [...(groups.get(id) || []), body.status ?? 'stat']);
return {code:200};
});
await Promise.all([executeTask(context, {argv:['receipt.sh'], mode:'now'}),
executeTask(context, {argv:['receipt.sh'], mode:'now'}),
loggedOperation(context, 'extra', async () => {})]);
assert.deepEqual([...groups.values()].map(sequence => sequence.join(',')).sort(), ['0,1','0,1,stat','0,1,stat']);
});