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

+80 -3
View File
@@ -2,9 +2,44 @@ const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const ts = require('typescript');
const load = require('../helpers/load-security-module.cjs');
test('BSD identity and parent lookups distinguish a vanished PID from ps failures', () => {
const platform = Object.getOwnPropertyDescriptor(process, 'platform');
const pid = process.pid;
let result, field;
const probes = load('back/shared/cronProcess.ts', {
child_process: { spawnSync(command, args) {
assert.equal(command, 'ps');
assert.deepEqual(args, ['-p', String(pid), '-o', field + '=']);
return result;
} },
});
Object.defineProperty(process, 'platform', { ...platform, value: 'darwin' });
try {
for (const [name, probe] of [
['lstart', () => probes.systemOwner(pid)],
['ppid', () => probes.isProcessDescendant(pid, 1)],
]) {
field = name;
result = { status: 1, stdout: '', stderr: '' };
assert.throws(probe, { code: 'ESRCH' });
for (result of [
{ status: 1, stdout: '', stderr: 'Permission denied' },
{ status: 0, stdout: '', stderr: '' },
{ status: null, signal: 'SIGTERM', stdout: '', stderr: '' },
{ error: Object.assign(Error('spawn ps ENOENT'), { code: 'ENOENT' }) },
]) {
assert.throws(probe, error => !['ESRCH', 'ENOENT'].includes(error.code) && /ps|PID/.test(error.message));
}
}
} finally {
Object.defineProperty(process, 'platform', platform);
}
});
// Exercise the production function with a deterministic process-tree race.
function fixture(descendants, probe = () => {}) {
function fixture(descendants, probe = () => {}, birth, descendant) {
const source = fs.readFileSync('back/config/util.ts', 'utf8');
const start = source.indexOf('export async function killTask(');
const end = source.indexOf('export async function getPid(', start);
@@ -16,7 +51,7 @@ function fixture(descendants, probe = () => {}) {
}).outputText;
const signals = [];
const exports = {};
new Function('exports', 'process', 'psTree', 'fs', 'setTimeout', code)(
new Function('exports', 'process', 'psTree', 'fs', 'setTimeout', 'systemOwner', 'isProcessDescendant', code)(
exports,
{
platform: 'linux',
@@ -28,7 +63,7 @@ function fixture(descendants, probe = () => {}) {
},
async () => [...descendants],
{},
setTimeout,
setTimeout, birth, descendant,
);
return { killTask: exports.killTask, signals };
}
@@ -75,3 +110,45 @@ test('a leaf retains SIGINT and a process tree retains child-first SIGTERM', asy
[100, 'SIGTERM'],
]);
});
test('verified stop ignores reused, vanished and reparented descendants from the tree snapshot', async () => {
const seen = new Set();
let rootExited = false;
const birth = pid => {
if (pid === 102 || (pid === 100 && rootExited)) throw Object.assign(Error('gone'), { code: 'ESRCH' });
if (pid === 101 && seen.has(pid)) return 'replacement';
seen.add(pid);
return 'original-' + pid;
};
const f = fixture([101, 102, 103], (pid, sig) => {
if (pid === 100 && sig === 'SIGTERM') rootExited = true;
}, birth, pid => pid !== 103);
await f.killTask(100, true, () => {}, async signal => signal());
assert.deepEqual(f.signals, [[100, 'SIGTERM']]);
});
test('a root exiting immediately before TERM cannot claim a stop or signal a surviving child', async () => {
const f = fixture([101], (pid, sig) => {
if (pid === 100 && sig === 'SIGTERM') throw Object.assign(Error('gone'), { code: 'ESRCH' });
}, pid => 'birth-' + pid, () => true);
let committed = false;
await assert.rejects(f.killTask(100, true, () => {}, async signal => {
signal();
committed = true;
}), { code: 'ESRCH' });
assert.equal(committed, false);
assert.deepEqual(f.signals, [[100, 'SIGTERM']]);
});
test('a descendant delivery failure after root TERM preserves the claim and still signals siblings', async () => {
const denied = Object.assign(Error('denied'), { code: 'EPERM' });
const f = fixture([101, 102], (pid, signal) => {
if (pid === 102 && signal === 'SIGTERM') throw denied;
}, pid => 'birth-' + pid, () => true);
await assert.rejects(f.killTask(100, true, () => {}, async signal => {
const delivery = signal();
assert.equal(delivery, denied);
throw delivery;
}), error => error === denied);
assert.deepEqual(f.signals, [[100, 'SIGTERM'], [102, 'SIGTERM'], [101, 'SIGTERM']]);
});