mirror of
https://github.com/whyour/qinglong.git
synced 2026-12-01 01:39:52 +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
@@ -13,8 +13,17 @@ function extract(file, name) {
|
||||
}
|
||||
const helpers = [
|
||||
fs.readFileSync('shell/task-timeout.sh', 'utf8'),
|
||||
...['handle_task_start', 'handle_task_end', 'run_task_before', 'run_task_after', 'get_env_array', 'clear_env'].map(n => extract('shell/share.sh', n)),
|
||||
...['run_shell_script', 'define_program', 'format_params'].map(n => extract('shell/task.sh', n)),
|
||||
...[
|
||||
'handle_task_start',
|
||||
'handle_task_end',
|
||||
'run_task_before',
|
||||
'run_task_after',
|
||||
'get_env_array',
|
||||
'clear_env',
|
||||
].map((n) => extract('shell/share.sh', n)),
|
||||
...['run_shell_script', 'define_program', 'format_params'].map((n) =>
|
||||
extract('shell/task.sh', n),
|
||||
),
|
||||
].join('\n');
|
||||
const taskSource = path.resolve('shell/otask.sh');
|
||||
|
||||
@@ -23,10 +32,21 @@ function fixture(t) {
|
||||
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
|
||||
const write = (name, body) => fs.writeFileSync(path.join(root, name), body);
|
||||
write('env.sh', 'export QA_PANEL="alpha&beta&gamma"\n');
|
||||
write('before.sh', 'export QA_BEFORE=ready\nqa_function() { printf "HOOK_FUNCTION\\n"; }\n');
|
||||
write('after.sh', 'printf "AFTER:%s:%s\\n" "$QA_PANEL" "$QA_BEFORE" >> "$QA_ROOT/events"\n');
|
||||
write(
|
||||
'before.sh',
|
||||
'export QA_BEFORE=ready\nqa_function() { printf "HOOK_FUNCTION\\n"; }\n',
|
||||
);
|
||||
write(
|
||||
'after.sh',
|
||||
'printf "AFTER:%s:%s\\n" "$QA_PANEL" "$QA_BEFORE" >> "$QA_ROOT/events"\n',
|
||||
);
|
||||
const run = (args, timeout = '') => {
|
||||
const r = spawnSync('/bin/bash', ['-c', helpers + `
|
||||
const r = spawnSync(
|
||||
'/bin/bash',
|
||||
[
|
||||
'-c',
|
||||
helpers +
|
||||
`
|
||||
dir_scripts=$QA_ROOT; dir_shell=$QA_ROOT; dir_dep=$QA_ROOT
|
||||
file_env=$QA_ROOT/env.sh; file_task_before=$QA_ROOT/before.sh; file_task_after=$QA_ROOT/after.sh
|
||||
ID=42; log_path=fixture.log; is_macos=0
|
||||
@@ -41,32 +61,76 @@ function fixture(t) {
|
||||
format_params "$@"; define_program "${'${task_shell_params[@]}'}"
|
||||
. "$QA_TASK_SOURCE"
|
||||
printf 'WRAPPER_FINISHED\\n' >> "$QA_ROOT/events"
|
||||
`, 'fixture', ...args], { cwd: root, env: { ...process.env, command_timeout_time: timeout, QA_ROOT: root, QA_TASK_SOURCE: taskSource }, encoding: 'utf8', timeout: 10000 });
|
||||
`,
|
||||
'fixture',
|
||||
...args,
|
||||
],
|
||||
{
|
||||
cwd: root,
|
||||
env: {
|
||||
...process.env,
|
||||
command_timeout_time: timeout,
|
||||
QA_ROOT: root,
|
||||
QA_TASK_SOURCE: taskSource,
|
||||
},
|
||||
encoding: 'utf8',
|
||||
timeout: 10000,
|
||||
},
|
||||
);
|
||||
assert.equal(r.status, 0, r.stdout + r.stderr);
|
||||
return { stdout: r.stdout, events: fs.readFileSync(path.join(root, 'events'), 'utf8').trim().split('\n') };
|
||||
return {
|
||||
stdout: r.stdout,
|
||||
events: fs
|
||||
.readFileSync(path.join(root, 'events'), 'utf8')
|
||||
.trim()
|
||||
.split('\n'),
|
||||
};
|
||||
};
|
||||
return { write, run };
|
||||
}
|
||||
|
||||
test('managed task propagates the sourced script status through direct output, redirection and tee', t => {
|
||||
test('managed task propagates the sourced script status through direct output, redirection and tee', (t) => {
|
||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'ql-wrapper-status-'));
|
||||
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
|
||||
const shellDir = path.join(root, 'shell');
|
||||
fs.mkdirSync(shellDir);
|
||||
// Exercise the real outer wrapper with deterministic paths and a failing payload.
|
||||
fs.writeFileSync(path.join(shellDir, 'share.sh'), [
|
||||
'is_macos=0; no_delay=true; dir_log=$QL_DIR; log_name=logs',
|
||||
'import_config() { :; }', 'make_dir() { mkdir -p "$1"; }',
|
||||
'format_timestamp() { echo 1; }', 'format_time() { echo now; }',
|
||||
].join('\n'));
|
||||
fs.writeFileSync(
|
||||
path.join(shellDir, 'share.sh'),
|
||||
[
|
||||
'is_macos=0; no_delay=true; dir_log=$QL_DIR; log_name=logs',
|
||||
'import_config() { :; }',
|
||||
'make_dir() { mkdir -p "$1"; }',
|
||||
'format_timestamp() { echo 1; }',
|
||||
'format_time() { echo now; }',
|
||||
].join('\n'),
|
||||
);
|
||||
fs.writeFileSync(path.join(shellDir, 'api.sh'), '');
|
||||
fs.writeFileSync(path.join(shellDir, 'task-timeout.sh'), '');
|
||||
fs.writeFileSync(path.join(shellDir, 'otask.sh'), 'echo payload; return 17\n');
|
||||
for (const [direct, noTee] of [[true,false], [false,true], [false,false]]) {
|
||||
const run = spawnSync('/bin/bash', [path.resolve('shell/task.sh'), 'probe.sh'], {
|
||||
env: { ...process.env, QL_DIR: root, ID: '42', QL_TRIGGER_ID: 'shell-probe',
|
||||
real_time: String(direct), no_tee: String(noTee) }, encoding: 'utf8',
|
||||
});
|
||||
fs.writeFileSync(
|
||||
path.join(shellDir, 'otask.sh'),
|
||||
'echo payload; return 17\n',
|
||||
);
|
||||
for (const [direct, noTee] of [
|
||||
[true, false],
|
||||
[false, true],
|
||||
[false, false],
|
||||
]) {
|
||||
const run = spawnSync(
|
||||
'/bin/bash',
|
||||
[path.resolve('shell/task.sh'), 'probe.sh'],
|
||||
{
|
||||
env: {
|
||||
...process.env,
|
||||
QL_DIR: root,
|
||||
ID: '42',
|
||||
QL_TRIGGER_ID: 'shell-probe',
|
||||
real_time: String(direct),
|
||||
no_tee: String(noTee),
|
||||
},
|
||||
encoding: 'utf8',
|
||||
},
|
||||
);
|
||||
assert.equal(run.status, 17, run.stderr);
|
||||
if (noTee) assert.equal(run.stdout, '');
|
||||
else assert.match(run.stdout, /payload/);
|
||||
@@ -81,20 +145,33 @@ for (const [label, body, code] of [
|
||||
['errexit failure', 'set -e\nfalse\necho SHOULD_NOT_RUN', 1],
|
||||
['script EXIT trap', `trap 'echo SCRIPT_EXIT_TRAP' EXIT\nexit 4`, 4],
|
||||
]) {
|
||||
test(`sourced shell ${label} still runs after hook and reports exactly one completion`, t => {
|
||||
test(`sourced shell ${label} still runs after hook and reports exactly one completion`, (t) => {
|
||||
const f = fixture(t);
|
||||
f.write('probe.sh', `[[ "$QA_PANEL" == 'alpha&beta&gamma' && "$QA_BEFORE" == ready ]] || exit 99\nqa_function\n${body}\n`);
|
||||
f.write(
|
||||
'probe.sh',
|
||||
`[[ "$QA_PANEL" == 'alpha&beta&gamma' && "$QA_BEFORE" == ready ]] || exit 99\nqa_function\n${body}\n`,
|
||||
);
|
||||
const r = f.run(['probe.sh']);
|
||||
assert.match(r.stdout, /HOOK_FUNCTION/);
|
||||
assert.doesNotMatch(r.stdout, /SHOULD_NOT_RUN/);
|
||||
if (label === 'script EXIT trap') assert.match(r.stdout, /SCRIPT_EXIT_TRAP/);
|
||||
assert.deepEqual(r.events, ['STATUS:0:', 'AFTER:alpha&beta&gamma:ready', `STATUS:1:${code}`, `STAT:${code}`, 'WRAPPER_FINISHED']);
|
||||
if (label === 'script EXIT trap')
|
||||
assert.match(r.stdout, /SCRIPT_EXIT_TRAP/);
|
||||
assert.deepEqual(r.events, [
|
||||
'STATUS:0:',
|
||||
'AFTER:alpha&beta&gamma:ready',
|
||||
`STATUS:1:${code}`,
|
||||
`STAT:${code}`,
|
||||
'WRAPPER_FINISHED',
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
test('shell arguments and before-hook functions survive isolation while script state stays in the child', t => {
|
||||
test('shell arguments and before-hook functions survive isolation while script state stays in the child', (t) => {
|
||||
const f = fixture(t);
|
||||
f.write('probe.sh', '[[ "$1" == "arg with spaces" ]] || exit 99\nqa_function\nexport QA_BEFORE=changed\ncd /\nexit 5\n');
|
||||
f.write(
|
||||
'probe.sh',
|
||||
'[[ "$1" == "arg with spaces" ]] || exit 99\nqa_function\nexport QA_BEFORE=changed\ncd /\nexit 5\n',
|
||||
);
|
||||
const r = f.run(['probe.sh', '--', 'arg with spaces']);
|
||||
assert.match(r.stdout, /HOOK_FUNCTION/);
|
||||
assert.ok(r.events.includes('AFTER:alpha&beta&gamma:ready'));
|
||||
@@ -105,42 +182,60 @@ for (const [label, args] of [
|
||||
['explicit shell interpreter', ['bash', 'probe.sh', 'argument']],
|
||||
['shell script with positional args', ['probe.sh', 'argument']],
|
||||
]) {
|
||||
test(`${label} preserves generated environment and reports failure`, t => {
|
||||
test(`${label} preserves generated environment and reports failure`, (t) => {
|
||||
const f = fixture(t);
|
||||
f.write('probe.sh', '[[ "$QA_PANEL" == "alpha&beta&gamma" && "$QA_BEFORE" == ready && "$1" == argument ]] || exit 99\nexit 6\n');
|
||||
f.write(
|
||||
'probe.sh',
|
||||
'[[ "$QA_PANEL" == "alpha&beta&gamma" && "$QA_BEFORE" == ready && "$1" == argument ]] || exit 99\nexit 6\n',
|
||||
);
|
||||
const r = f.run(args);
|
||||
assert.ok(r.events.includes('STATUS:1:6'));
|
||||
assert.equal(r.events.filter(x => x.startsWith('STATUS:1:')).length, 1);
|
||||
assert.equal(r.events.filter((x) => x.startsWith('STATUS:1:')).length, 1);
|
||||
});
|
||||
}
|
||||
|
||||
test('explicit Python interpreter receives panel and before-hook environment', t => {
|
||||
test('explicit Python interpreter receives panel and before-hook environment', (t) => {
|
||||
const f = fixture(t);
|
||||
f.write('probe.py', 'import os,sys\nassert os.environ["QA_PANEL"] == "alpha&beta&gamma"\nassert os.environ["QA_BEFORE"] == "ready"\nassert sys.argv[1] == "argument"\nsys.exit(8)\n');
|
||||
assert.ok(f.run(['python3', 'probe.py', 'argument']).events.includes('STATUS:1:8'));
|
||||
f.write(
|
||||
'probe.py',
|
||||
'import os,sys\nassert os.environ["QA_PANEL"] == "alpha&beta&gamma"\nassert os.environ["QA_BEFORE"] == "ready"\nassert sys.argv[1] == "argument"\nsys.exit(8)\n',
|
||||
);
|
||||
assert.ok(
|
||||
f.run(['python3', 'probe.py', 'argument']).events.includes('STATUS:1:8'),
|
||||
);
|
||||
});
|
||||
|
||||
test('explicit Node interpreter retains generated environment', t => {
|
||||
test('explicit Node interpreter retains generated environment', (t) => {
|
||||
const f = fixture(t);
|
||||
f.write('probe.cjs', 'if (process.env.QA_PANEL !== "alpha&beta&gamma" || process.env.QA_BEFORE !== "ready") process.exit(99); process.exit(8);\n');
|
||||
f.write(
|
||||
'probe.cjs',
|
||||
'if (process.env.QA_PANEL !== "alpha&beta&gamma" || process.env.QA_BEFORE !== "ready") process.exit(99); process.exit(8);\n',
|
||||
);
|
||||
assert.ok(f.run(['node', 'probe.cjs']).events.includes('STATUS:1:8'));
|
||||
});
|
||||
|
||||
test('plain commands retain generated environment', t => {
|
||||
test('plain commands retain generated environment', (t) => {
|
||||
const f = fixture(t);
|
||||
assert.match(f.run(['printenv', 'QA_PANEL']).stdout, /alpha&beta&gamma/);
|
||||
});
|
||||
|
||||
for (const mode of ['desi', 'conc']) {
|
||||
test(`shell ${mode} keeps selected account values and completion reporting`, t => {
|
||||
test(`shell ${mode} keeps selected account values and completion reporting`, (t) => {
|
||||
const f = fixture(t);
|
||||
f.write('probe.sh', 'printf "ACCOUNT=%s\\n" "$QA_PANEL"\nexit 0\n');
|
||||
const r = f.run(['probe.sh', mode, 'QA_PANEL', '2-3']);
|
||||
assert.match(r.stdout, mode === 'desi' ? /ACCOUNT=beta&gamma/ : /ACCOUNT=beta\nACCOUNT=gamma/);
|
||||
assert.match(
|
||||
r.stdout,
|
||||
mode === 'desi' ? /ACCOUNT=beta&gamma/ : /ACCOUNT=beta\nACCOUNT=gamma/,
|
||||
);
|
||||
assert.doesNotMatch(r.stdout, /ACCOUNT=alpha/);
|
||||
assert.ok(r.events.includes(`AFTER:${mode === 'desi' ? 'beta&gamma' : 'gamma'}:ready`));
|
||||
assert.equal(r.events.filter(x => x === 'STATUS:1:0').length, 1);
|
||||
assert.equal(r.events.filter(x => x.startsWith('AFTER:')).length, 1);
|
||||
assert.ok(
|
||||
r.events.includes(
|
||||
`AFTER:${mode === 'desi' ? 'beta&gamma' : 'gamma'}:ready`,
|
||||
),
|
||||
);
|
||||
assert.equal(r.events.filter((x) => x === 'STATUS:1:0').length, 1);
|
||||
assert.equal(r.events.filter((x) => x.startsWith('AFTER:')).length, 1);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -148,20 +243,36 @@ for (const [label, args] of [
|
||||
['sourced shell', ['probe.sh', '--', 'space value']],
|
||||
['explicit bash', ['bash', 'probe.sh', 'space value']],
|
||||
]) {
|
||||
test(`${label} timeout stops descendants and finalizes once with 124`, t => {
|
||||
test(`${label} timeout stops descendants and finalizes once with 124`, (t) => {
|
||||
const f = fixture(t);
|
||||
f.write('before.sh', 'export QA_BEFORE=ready\nprivate_value=private\nprivate_array=(one "two words")\nqa_function() { printf "HOOK_FUNCTION\\n"; }\n');
|
||||
f.write('probe.sh', `[[ "$QA_PANEL" == 'alpha&beta&gamma' && "$QA_BEFORE" == ready && "$1" == 'space value' ]] || exit 91\n${label === 'sourced shell' ? '[[ "$private_value" = private && "${private_array[1]}" = "two words" ]] || exit 92\nqa_function\n' : ''}trap 'echo EXIT_TRAP' EXIT\nsleep 3\necho SHOULD_NOT_RUN\n`);
|
||||
f.write(
|
||||
'before.sh',
|
||||
'export QA_BEFORE=ready\nprivate_value=private\nprivate_array=(one "two words")\nqa_function() { printf "HOOK_FUNCTION\\n"; }\n',
|
||||
);
|
||||
f.write(
|
||||
'probe.sh',
|
||||
`[[ "$QA_PANEL" == 'alpha&beta&gamma' && "$QA_BEFORE" == ready && "$1" == 'space value' ]] || exit 91\n${
|
||||
label === 'sourced shell'
|
||||
? '[[ "$private_value" = private && "${private_array[1]}" = "two words" ]] || exit 92\nqa_function\n'
|
||||
: ''
|
||||
}trap 'echo EXIT_TRAP' EXIT\nsleep 3\necho SHOULD_NOT_RUN\n`,
|
||||
);
|
||||
const before = Date.now();
|
||||
const r = f.run(args, '0.2s');
|
||||
assert.ok(Date.now() - before < 2500, r.stdout);
|
||||
assert.doesNotMatch(r.stdout, /SHOULD_NOT_RUN/);
|
||||
assert.match(r.stdout, /EXIT_TRAP/);
|
||||
assert.deepEqual(r.events, ['STATUS:0:', 'AFTER:alpha&beta&gamma:ready', 'STATUS:1:124', 'STAT:124', 'WRAPPER_FINISHED']);
|
||||
assert.deepEqual(r.events, [
|
||||
'STATUS:0:',
|
||||
'AFTER:alpha&beta&gamma:ready',
|
||||
'STATUS:1:124',
|
||||
'STAT:124',
|
||||
'WRAPPER_FINISHED',
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
test('timeout escalates when a shell and its children ignore TERM', t => {
|
||||
test('timeout escalates when a shell and its children ignore TERM', (t) => {
|
||||
const f = fixture(t);
|
||||
f.write('probe.sh', 'trap "" TERM\nsleep 3\necho SHOULD_NOT_RUN\n');
|
||||
const before = Date.now();
|
||||
@@ -171,7 +282,7 @@ test('timeout escalates when a shell and its children ignore TERM', t => {
|
||||
assert.ok(r.events.includes('STATUS:1:124'));
|
||||
});
|
||||
|
||||
test('completed timed tasks keep their exit code and do not wait for the timer', t => {
|
||||
test('completed timed tasks keep their exit code and do not wait for the timer', (t) => {
|
||||
const f = fixture(t);
|
||||
f.write('probe.sh', 'exit 7\n');
|
||||
const before = Date.now();
|
||||
@@ -180,7 +291,7 @@ test('completed timed tasks keep their exit code and do not wait for the timer',
|
||||
assert.ok(r.events.includes('STATUS:1:7'));
|
||||
});
|
||||
|
||||
test('zero disables the deadline and invalid durations never start the script', t => {
|
||||
test('zero disables the deadline and invalid durations never start the script', (t) => {
|
||||
const f = fixture(t);
|
||||
f.write('probe.sh', 'echo DID_RUN\n');
|
||||
assert.match(f.run(['probe.sh'], '0s').stdout, /DID_RUN/);
|
||||
@@ -191,28 +302,42 @@ test('zero disables the deadline and invalid durations never start the script',
|
||||
});
|
||||
|
||||
for (const [runtime, filename, body] of [
|
||||
['python3', 'timed.py', 'import time\nprint("STARTED", flush=True)\ntime.sleep(3)\nprint("SHOULD_NOT_RUN", flush=True)\n'],
|
||||
['node', 'timed.cjs', 'console.log("STARTED"); setTimeout(() => console.log("SHOULD_NOT_RUN"), 3000);\n'],
|
||||
[
|
||||
'python3',
|
||||
'timed.py',
|
||||
'import time\nprint("STARTED", flush=True)\ntime.sleep(3)\nprint("SHOULD_NOT_RUN", flush=True)\n',
|
||||
],
|
||||
[
|
||||
'node',
|
||||
'timed.cjs',
|
||||
'console.log("STARTED"); setTimeout(() => console.log("SHOULD_NOT_RUN"), 3000);\n',
|
||||
],
|
||||
]) {
|
||||
test(`${runtime} timeout stops execution and reports 124`, t => {
|
||||
test(`${runtime} timeout stops execution and reports 124`, (t) => {
|
||||
const f = fixture(t);
|
||||
f.write(filename, body);
|
||||
const r = f.run([runtime, filename], '0.5s');
|
||||
assert.match(r.stdout, /STARTED/);
|
||||
assert.doesNotMatch(r.stdout, /SHOULD_NOT_RUN/);
|
||||
assert.ok(r.events.includes('STATUS:1:124'));
|
||||
assert.equal(r.events.filter(x => x.startsWith('AFTER:')).length, 1);
|
||||
assert.equal(r.events.filter((x) => x.startsWith('AFTER:')).length, 1);
|
||||
});
|
||||
}
|
||||
|
||||
for (const mode of ['desi', 'conc']) {
|
||||
test(`timed shell ${mode} preserves selected accounts and terminates every worker`, t => {
|
||||
test(`timed shell ${mode} preserves selected accounts and terminates every worker`, (t) => {
|
||||
const f = fixture(t);
|
||||
f.write('probe.sh', 'echo "ACCOUNT=$QA_PANEL"\nsleep 3\necho SHOULD_NOT_RUN\n');
|
||||
f.write(
|
||||
'probe.sh',
|
||||
'echo "ACCOUNT=$QA_PANEL"\nsleep 3\necho SHOULD_NOT_RUN\n',
|
||||
);
|
||||
const r = f.run(['probe.sh', mode, 'QA_PANEL', '2-3'], '0.2s');
|
||||
assert.match(r.stdout, mode === 'desi' ? /ACCOUNT=beta&gamma/ : /ACCOUNT=beta\nACCOUNT=gamma/);
|
||||
assert.match(
|
||||
r.stdout,
|
||||
mode === 'desi' ? /ACCOUNT=beta&gamma/ : /ACCOUNT=beta\nACCOUNT=gamma/,
|
||||
);
|
||||
assert.doesNotMatch(r.stdout, /SHOULD_NOT_RUN/);
|
||||
assert.equal(r.events.filter(x => x.startsWith('AFTER:')).length, 1);
|
||||
assert.equal(r.events.filter((x) => x.startsWith('AFTER:')).length, 1);
|
||||
// Concurrent mode retains its existing aggregate wait status behavior.
|
||||
assert.ok(r.events.includes(`STATUS:1:${mode === 'desi' ? 124 : 0}`));
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user