mirror of
https://github.com/whyour/qinglong.git
synced 2026-12-01 18:40:37 +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
@@ -4,14 +4,24 @@ const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const { exhaust } = require('./receipt-volume-full.cjs');
|
||||
const { sequelize } = require('/ql/static/build/data');
|
||||
const { TriggerHistory } = require('/ql/static/build/shared/triggerHistory');
|
||||
const { TriggerHistory } = require('/ql/static/build/cron/history');
|
||||
const [stage, id] = process.argv.slice(2);
|
||||
const volume = '/ql/data/db';
|
||||
const canonical = path.join(volume, 'cron-exits', id);
|
||||
const control = '/tmp/journal-qa';
|
||||
const capacity = () => fs.statfsSync(volume).bavail;
|
||||
if (fs.statfsSync(volume).type !== 0xef53 || !/^[a-z0-9-]+$/.test(id) ||
|
||||
!['rename-before','rename-after','rename-enospc','unlink-before','unlink-after','unlink-injected'].includes(stage)) {
|
||||
if (
|
||||
fs.statfsSync(volume).type !== 0xef53 ||
|
||||
!/^[a-z0-9-]+$/.test(id) ||
|
||||
![
|
||||
'rename-before',
|
||||
'rename-after',
|
||||
'rename-enospc',
|
||||
'unlink-before',
|
||||
'unlink-after',
|
||||
'unlink-injected',
|
||||
].includes(stage)
|
||||
) {
|
||||
throw Error('Expected a supported ext4 collection boundary and execution ID');
|
||||
}
|
||||
|
||||
@@ -30,8 +40,9 @@ async function atBoundary(source, destination, operation) {
|
||||
const pin = path.join(volume, '.qa-reclaim-pin');
|
||||
fs.linkSync(source, pin);
|
||||
exhaust(false);
|
||||
const event = {stage, id, source, destination, blocksBefore: capacity()};
|
||||
if (event.blocksBefore !== 0) throw Error('Expected an exhausted data volume');
|
||||
const event = { stage, id, source, destination, blocksBefore: capacity() };
|
||||
if (event.blocksBefore !== 0)
|
||||
throw Error('Expected an exhausted data volume');
|
||||
if (stage === 'rename-enospc') {
|
||||
// Fill the target directory with entries the same length as the capture name.
|
||||
// Hard links expend directory space without needing another inode.
|
||||
@@ -39,7 +50,16 @@ async function atBoundary(source, destination, operation) {
|
||||
let count = 0;
|
||||
try {
|
||||
for (;;) {
|
||||
fs.linkSync(pin, path.join(dir, 'q' + count.toString(36).padStart(path.basename(destination).length - 1, '0')));
|
||||
fs.linkSync(
|
||||
pin,
|
||||
path.join(
|
||||
dir,
|
||||
'q' +
|
||||
count
|
||||
.toString(36)
|
||||
.padStart(path.basename(destination).length - 1, '0'),
|
||||
),
|
||||
);
|
||||
count++;
|
||||
}
|
||||
} catch (error) {
|
||||
@@ -47,45 +67,86 @@ async function atBoundary(source, destination, operation) {
|
||||
}
|
||||
event.extraDirectoryEntries = count;
|
||||
}
|
||||
if (stage.endsWith('-before')) hold({...event, called:false});
|
||||
if (stage.endsWith('-before')) hold({ ...event, called: false });
|
||||
if (stage === 'unlink-injected') {
|
||||
// Explicitly synthetic. Keep the file and let the real collector unwind.
|
||||
throw Object.assign(Error('Injected unlink capacity error'), {code:'ENOSPC', event});
|
||||
throw Object.assign(Error('Injected unlink capacity error'), {
|
||||
code: 'ENOSPC',
|
||||
event,
|
||||
});
|
||||
}
|
||||
try {
|
||||
await operation();
|
||||
} catch (error) {
|
||||
if (stage !== 'rename-enospc' || error.code !== 'ENOSPC') throw error;
|
||||
hold({...event, called:true, result:error.code, errno:error.errno, syscall:error.syscall,
|
||||
sourceExists:fs.existsSync(source), destinationExists:fs.existsSync(destination)});
|
||||
hold({
|
||||
...event,
|
||||
called: true,
|
||||
result: error.code,
|
||||
errno: error.errno,
|
||||
syscall: error.syscall,
|
||||
sourceExists: fs.existsSync(source),
|
||||
destinationExists: fs.existsSync(destination),
|
||||
});
|
||||
}
|
||||
if (stage === 'rename-enospc') throw Error('Expected rename to need another directory block');
|
||||
hold({...event, called:true, result:'success', blocksAfter:capacity(),
|
||||
sourceExists:fs.existsSync(source), destinationExists:destination && fs.existsSync(destination)});
|
||||
if (stage === 'rename-enospc')
|
||||
throw Error('Expected rename to need another directory block');
|
||||
hold({
|
||||
...event,
|
||||
called: true,
|
||||
result: 'success',
|
||||
blocksAfter: capacity(),
|
||||
sourceExists: fs.existsSync(source),
|
||||
destinationExists: destination && fs.existsSync(destination),
|
||||
});
|
||||
}
|
||||
|
||||
const rename = fs.promises.rename.bind(fs.promises);
|
||||
const unlink = fs.promises.unlink.bind(fs.promises);
|
||||
let reached = false, injected;
|
||||
fs.promises.rename = (from,to) => {
|
||||
let reached = false,
|
||||
injected;
|
||||
fs.promises.rename = (from, to) => {
|
||||
if (!reached && stage.startsWith('rename-') && from === canonical) {
|
||||
reached = true;
|
||||
const destination = path.join(fs.realpathSync(path.dirname(to)), path.basename(to));
|
||||
return atBoundary(from,destination,() => rename(from,to));
|
||||
const destination = path.join(
|
||||
fs.realpathSync(path.dirname(to)),
|
||||
path.basename(to),
|
||||
);
|
||||
return atBoundary(from, destination, () => rename(from, to));
|
||||
}
|
||||
return rename(from,to);
|
||||
return rename(from, to);
|
||||
};
|
||||
fs.promises.unlink = async file => {
|
||||
if (!reached && stage.startsWith('unlink-') && path.basename(file).startsWith('.pending.' + id + '.r-')) {
|
||||
fs.promises.unlink = async (file) => {
|
||||
if (
|
||||
!reached &&
|
||||
stage.startsWith('unlink-') &&
|
||||
path.basename(file).startsWith('.pending.' + id + '.r-')
|
||||
) {
|
||||
reached = true;
|
||||
try {return await atBoundary(fs.realpathSync(file),null,() => unlink(file));}
|
||||
catch (error) { if (stage === 'unlink-injected') injected = error; throw error; }
|
||||
try {
|
||||
return await atBoundary(fs.realpathSync(file), null, () => unlink(file));
|
||||
} catch (error) {
|
||||
if (stage === 'unlink-injected') injected = error;
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
return unlink(file);
|
||||
};
|
||||
|
||||
new TriggerHistory(sequelize).complete(id,'unknown',null,null).then(() => {
|
||||
if (injected?.code === 'ENOSPC') hold({...injected.event, called:false, injected:'ENOSPC',
|
||||
privateFileRetained:fs.existsSync(injected.event.source)});
|
||||
throw Error('Collector did not reach the expected power cut');
|
||||
}).catch(error => {console.error(error); process.exitCode = 1;}).finally(() => sequelize.close());
|
||||
new TriggerHistory(sequelize)
|
||||
.complete(id, 'unknown', null, null)
|
||||
.then(() => {
|
||||
if (injected?.code === 'ENOSPC')
|
||||
hold({
|
||||
...injected.event,
|
||||
called: false,
|
||||
injected: 'ENOSPC',
|
||||
privateFileRetained: fs.existsSync(injected.event.source),
|
||||
});
|
||||
throw Error('Collector did not reach the expected power cut');
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(error);
|
||||
process.exitCode = 1;
|
||||
})
|
||||
.finally(() => sequelize.close());
|
||||
Reference in new issue
Block a user