mirror of
https://github.com/whyour/qinglong.git
synced 2026-12-01 09:45:36 +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
@@ -40,7 +40,12 @@ function fixture(get) {
|
||||
config: { apiPrefix: '/api/' },
|
||||
cron: { id: 1 },
|
||||
session: { current: new AbortController() },
|
||||
pinnedTrigger: {}, reading: {current: false}, drained: {}, lastPath: {}, lastCursor: {}, scrollTimer: {},
|
||||
pinnedTrigger: {},
|
||||
reading: { current: false },
|
||||
drained: {},
|
||||
lastPath: {},
|
||||
lastCursor: {},
|
||||
scrollTimer: {},
|
||||
LOG_CHUNK_BYTES: 256 * 1024,
|
||||
MAX_LOG_VIEW_CHARS: 1024 * 1024,
|
||||
logOffsetRef: { current: undefined },
|
||||
@@ -54,8 +59,12 @@ function fixture(get) {
|
||||
},
|
||||
},
|
||||
intl: { get: (value) => value },
|
||||
setLoading(value) { values.loading = value; },
|
||||
setReadFailed(value) { values.failed = value; },
|
||||
setLoading(value) {
|
||||
values.loading = value;
|
||||
},
|
||||
setReadFailed(value) {
|
||||
values.failed = value;
|
||||
},
|
||||
autoScroll() {},
|
||||
handleCancel() {},
|
||||
setValue(value) {
|
||||
@@ -87,7 +96,9 @@ function fixture(get) {
|
||||
return {
|
||||
values,
|
||||
timers,
|
||||
retry() { context.getCronLog(context.logOffsetRef.current === undefined); },
|
||||
retry() {
|
||||
context.getCronLog(context.logOffsetRef.current === undefined);
|
||||
},
|
||||
start() {
|
||||
context.getCronLog(true);
|
||||
},
|
||||
@@ -228,12 +239,17 @@ test('first log read failure is recoverable on retry', async () => {
|
||||
|
||||
test('independent log readers can receive responses in reverse order across close and reopen', async () => {
|
||||
let resolveOld;
|
||||
const old = fixture(() => new Promise(resolve => { resolveOld = resolve; }));
|
||||
const old = fixture(
|
||||
() =>
|
||||
new Promise((resolve) => {
|
||||
resolveOld = resolve;
|
||||
}),
|
||||
);
|
||||
old.start();
|
||||
old.close();
|
||||
const current = fixture(async () => ({code: 200, data: 'new log'}));
|
||||
const current = fixture(async () => ({ code: 200, data: 'new log' }));
|
||||
await current.read();
|
||||
resolveOld({code: 200, data: 'old log', logStatus: 'running'});
|
||||
resolveOld({ code: 200, data: 'old log', logStatus: 'running' });
|
||||
await old.settle();
|
||||
assert.equal(current.values.content, 'new log');
|
||||
assert.equal(old.values.content, '');
|
||||
@@ -242,7 +258,10 @@ test('independent log readers can receive responses in reverse order across clos
|
||||
|
||||
test('repeated retries share one in-flight read and closing aborts its transport', async () => {
|
||||
const pending = [];
|
||||
const view = fixture((url, { signal }) => new Promise(resolve => pending.push({ signal, resolve })));
|
||||
const view = fixture(
|
||||
(url, { signal }) =>
|
||||
new Promise((resolve) => pending.push({ signal, resolve })),
|
||||
);
|
||||
view.start();
|
||||
view.retry();
|
||||
assert.equal(pending.length, 1);
|
||||
@@ -257,25 +276,43 @@ test('repeated retries share one in-flight read and closing aborts its transport
|
||||
test('polling a missing log displays its status once and retries from the last confirmed byte', async () => {
|
||||
const calls = [];
|
||||
const responses = [
|
||||
{data: 'waiting', total: 0, nextOffset: 0}, {data: 'waiting', total: 0, nextOffset: 0},
|
||||
{data: 'abc', total: 3, nextOffset: 3, logPath: 'a'}, null,
|
||||
{data: 'd', total: 4, nextOffset: 4, logPath: 'a'},
|
||||
{data: 'new!', total: 4, nextOffset: 4, logPath: 'b', logStatus: 'completed'},
|
||||
{ data: 'waiting', total: 0, nextOffset: 0 },
|
||||
{ data: 'waiting', total: 0, nextOffset: 0 },
|
||||
{ data: 'abc', total: 3, nextOffset: 3, logPath: 'a' },
|
||||
null,
|
||||
{ data: 'd', total: 4, nextOffset: 4, logPath: 'a' },
|
||||
{
|
||||
data: 'new!',
|
||||
total: 4,
|
||||
nextOffset: 4,
|
||||
logPath: 'b',
|
||||
logStatus: 'completed',
|
||||
},
|
||||
];
|
||||
const view = fixture(async url => {
|
||||
const view = fixture(async (url) => {
|
||||
calls.push(new URL(url, 'http://localhost').searchParams);
|
||||
const r = responses.shift();
|
||||
if (!r) throw Error('offline');
|
||||
return {code: 200, logStatus: 'running', ...r};
|
||||
return { code: 200, logStatus: 'running', ...r };
|
||||
});
|
||||
await view.read(); await view.tick();
|
||||
await view.read();
|
||||
await view.tick();
|
||||
assert.equal(view.values.content, 'waiting');
|
||||
await view.tick(); await view.tick();
|
||||
await view.tick();
|
||||
await view.tick();
|
||||
assert.equal(view.values.failed, true);
|
||||
assert.equal(view.values.content, 'abc');
|
||||
view.retry(); view.retry(); await view.settle();
|
||||
view.retry();
|
||||
view.retry();
|
||||
await view.settle();
|
||||
assert.equal(view.values.content, 'abcd');
|
||||
assert.deepEqual(calls.slice(3).map(p => [p.get('offset'), p.get('previousPath')]), [['3','a'],['3','a']]);
|
||||
assert.deepEqual(
|
||||
calls.slice(3).map((p) => [p.get('offset'), p.get('previousPath')]),
|
||||
[
|
||||
['3', 'a'],
|
||||
['3', 'a'],
|
||||
],
|
||||
);
|
||||
await view.tick();
|
||||
assert.equal(view.values.content, 'new!');
|
||||
assert.equal(view.timers.size, 0);
|
||||
@@ -283,21 +320,28 @@ test('polling a missing log displays its status once and retries from the last c
|
||||
|
||||
test('a shortened file during a retry is reread from its new tail', async () => {
|
||||
const responses = [
|
||||
{data: 'previous log', nextOffset: 12, total: 12}, {data: '', nextOffset: 3, total: 3},
|
||||
{data: 'new', nextOffset: 3, total: 3},
|
||||
{ data: 'previous log', nextOffset: 12, total: 12 },
|
||||
{ data: '', nextOffset: 3, total: 3 },
|
||||
{ data: 'new', nextOffset: 3, total: 3 },
|
||||
];
|
||||
const calls = [];
|
||||
const view = fixture(async url => {
|
||||
const view = fixture(async (url) => {
|
||||
calls.push(url);
|
||||
return {code: 200, logStatus: responses.length === 3 ? 'running' : 'completed', ...responses.shift()};
|
||||
return {
|
||||
code: 200,
|
||||
logStatus: responses.length === 3 ? 'running' : 'completed',
|
||||
...responses.shift(),
|
||||
};
|
||||
});
|
||||
await view.read(); await view.tick(); await view.tick();
|
||||
await view.read();
|
||||
await view.tick();
|
||||
await view.tick();
|
||||
assert.match(calls[2], /tail=true/);
|
||||
assert.equal(view.values.content, 'new');
|
||||
assert.equal(view.timers.size, 0);
|
||||
});
|
||||
|
||||
test('retry after a truncate and regrow replaces displayed bytes and drains all pages exactly once', async t => {
|
||||
test('retry after a truncate and regrow replaces displayed bytes and drains all pages exactly once', async (t) => {
|
||||
const dir = await fsp.mkdtemp(path.join(os.tmpdir(), 'ql-log-regrow-'));
|
||||
t.after(() => fsp.rm(dir, { recursive: true, force: true }));
|
||||
const file = path.join(dir, 'run.log');
|
||||
@@ -305,23 +349,34 @@ test('retry after a truncate and regrow replaces displayed bytes and drains all
|
||||
let running = true;
|
||||
let fail = false;
|
||||
const offsets = [];
|
||||
const view = fixture(async url => {
|
||||
const view = fixture(async (url) => {
|
||||
const p = new URL(url, 'http://localhost').searchParams;
|
||||
const offset = p.has('offset') ? Number(p.get('offset')) : undefined;
|
||||
offsets.push(offset);
|
||||
if (fail) throw Error('timeout');
|
||||
const chunk = await readLogChunk(file, {
|
||||
offset, follow: p.get('follow') === 'true', cursor: p.get('cursor'),
|
||||
tail: p.get('tail') === 'true', limit: Number(p.get('limit')),
|
||||
offset,
|
||||
follow: p.get('follow') === 'true',
|
||||
cursor: p.get('cursor'),
|
||||
tail: p.get('tail') === 'true',
|
||||
limit: Number(p.get('limit')),
|
||||
});
|
||||
return { ...chunk, code: 200, data: chunk.content, logPath: 'run.log',
|
||||
logStatus: running ? 'running' : 'completed' };
|
||||
return {
|
||||
...chunk,
|
||||
code: 200,
|
||||
data: chunk.content,
|
||||
logPath: 'run.log',
|
||||
logStatus: running ? 'running' : 'completed',
|
||||
};
|
||||
});
|
||||
await view.read();
|
||||
fail = true;
|
||||
await view.tick();
|
||||
assert.equal(view.values.failed, true);
|
||||
const current = Array.from({ length: 16000 }, (_, i) => `${i} · 中文日志🚀\n`).join('');
|
||||
const current = Array.from(
|
||||
{ length: 16000 },
|
||||
(_, i) => `${i} · 中文日志🚀\n`,
|
||||
).join('');
|
||||
await fsp.writeFile(file, current);
|
||||
fail = running = false;
|
||||
view.retry();
|
||||
@@ -338,42 +393,68 @@ test('retry after a truncate and regrow replaces displayed bytes and drains all
|
||||
test('a fresh list snapshot wakes a drained live viewer only for a new path or active task', () => {
|
||||
const effects = [];
|
||||
function visitEffect(node) {
|
||||
if (ts.isCallExpression(node) && node.expression.getText(ast) === 'useEffect' &&
|
||||
node.arguments[1]?.getText(ast) === '[cron]') effects.push(node.arguments[0]);
|
||||
if (
|
||||
ts.isCallExpression(node) &&
|
||||
node.expression.getText(ast) === 'useEffect' &&
|
||||
node.arguments[1]?.getText(ast) === '[cron]'
|
||||
)
|
||||
effects.push(node.arguments[0]);
|
||||
ts.forEachChild(node, visitEffect);
|
||||
}
|
||||
visitEffect(ast);
|
||||
assert.equal(effects.length, 1);
|
||||
const wake = ts.transpile('var wake = ' + effects[0].getText(ast), {target: ts.ScriptTarget.ES2020});
|
||||
const wake = ts.transpile('var wake = ' + effects[0].getText(ast), {
|
||||
target: ts.ScriptTarget.ES2020,
|
||||
});
|
||||
let calls = 0;
|
||||
const context = { cron: { log_path: 'old', status: 1 }, lastPath: {current: 'old'},
|
||||
drained: {current: true}, pinnedTrigger: {}, logUrl: undefined,
|
||||
CrontabStatus: {running: 0, queued: 3}, getCronLog: () => calls++ };
|
||||
const context = {
|
||||
cron: { log_path: 'old', status: 1 },
|
||||
lastPath: { current: 'old' },
|
||||
drained: { current: true },
|
||||
pinnedTrigger: {},
|
||||
logUrl: undefined,
|
||||
CrontabStatus: { running: 0, queued: 3 },
|
||||
getCronLog: () => calls++,
|
||||
};
|
||||
const update = () => vm.runInNewContext(wake + '\nwake()', context);
|
||||
update(); assert.equal(calls, 0);
|
||||
context.cron = {log_path: 'new', status: 1};
|
||||
update(); assert.equal(calls, 1);
|
||||
context.cron = {log_path: 'old', status: 3};
|
||||
update(); assert.equal(calls, 2);
|
||||
update();
|
||||
assert.equal(calls, 0);
|
||||
context.cron = { log_path: 'new', status: 1 };
|
||||
update();
|
||||
assert.equal(calls, 1);
|
||||
context.cron = { log_path: 'old', status: 3 };
|
||||
update();
|
||||
assert.equal(calls, 2);
|
||||
context.drained.current = false;
|
||||
update(); assert.equal(calls, 2);
|
||||
update();
|
||||
assert.equal(calls, 2);
|
||||
context.drained.current = true;
|
||||
context.logUrl = '/api/logs/detail?file=old';
|
||||
update(); assert.equal(calls, 2);
|
||||
update();
|
||||
assert.equal(calls, 2);
|
||||
});
|
||||
|
||||
test('concurrent live log polling and retries stay scoped to the initially selected trigger', async () => {
|
||||
const calls = [];
|
||||
const view = fixture(async url => {
|
||||
const view = fixture(async (url) => {
|
||||
const p = new URL(url, 'http://localhost').searchParams;
|
||||
calls.push(p.get('triggerId'));
|
||||
if (calls.length === 2) throw Error('timeout');
|
||||
const selected = p.get('triggerId') || (calls.length === 1 ? 'a' : 'b');
|
||||
return { code: 200, triggerId: selected, logPath: selected + '.log', total: calls.length === 1 ? 1 : 2,
|
||||
nextOffset: calls.length === 1 ? 1 : 2, logStatus: calls.length < 3 ? 'running' : 'completed',
|
||||
data: calls.length === 1 ? 'A' : selected === 'a' ? '!' : 'B!' };
|
||||
return {
|
||||
code: 200,
|
||||
triggerId: selected,
|
||||
logPath: selected + '.log',
|
||||
total: calls.length === 1 ? 1 : 2,
|
||||
nextOffset: calls.length === 1 ? 1 : 2,
|
||||
logStatus: calls.length < 3 ? 'running' : 'completed',
|
||||
data: calls.length === 1 ? 'A' : selected === 'a' ? '!' : 'B!',
|
||||
};
|
||||
});
|
||||
await view.read(); await view.tick(); view.retry(); await view.settle();
|
||||
await view.read();
|
||||
await view.tick();
|
||||
view.retry();
|
||||
await view.settle();
|
||||
assert.deepEqual(calls, [null, 'a', 'a']);
|
||||
assert.equal(view.values.content, 'A!');
|
||||
assert.equal(view.timers.size, 0);
|
||||
|
||||
Reference in new issue
Block a user