mirror of
https://github.com/whyour/qinglong.git
synced 2026-12-01 17:56:30 +08:00
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.
105 lines
2.8 KiB
JavaScript
105 lines
2.8 KiB
JavaScript
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const grpc = require('@grpc/grpc-js');
|
|
const load = require('../helpers/load-security-module.cjs');
|
|
const {
|
|
SchedulerReadiness,
|
|
} = require('../../back/cron/scheduling/schedulerReadiness');
|
|
|
|
for (const [name, rows, delay, succeeds] of [
|
|
[
|
|
'large distinct snapshot',
|
|
Array.from({ length: 10000 }, (_, i) => ({
|
|
id: String(i),
|
|
schedule: `${i % 60} ${Math.floor(i / 60) % 60} ${Math.floor(
|
|
i / 3600,
|
|
)} * * *`,
|
|
})),
|
|
10000,
|
|
true,
|
|
],
|
|
[
|
|
'extra schedules',
|
|
[
|
|
{
|
|
id: 'extra',
|
|
schedule: '* * * * *',
|
|
extra_schedules: Array.from({ length: 2000 }, () => ({
|
|
schedule: '0 * * * *',
|
|
})),
|
|
},
|
|
],
|
|
10000,
|
|
true,
|
|
],
|
|
[
|
|
'unresponsive small write',
|
|
[{ id: 'small', schedule: '* * * * *' }],
|
|
30000,
|
|
false,
|
|
],
|
|
[
|
|
'unresponsive large write remains bounded',
|
|
Array.from({ length: 30000 }, () => ({ schedule: '* * * * *' })),
|
|
180000,
|
|
false,
|
|
],
|
|
]) {
|
|
test(`recovery deadline: ${name}`, async (t) => {
|
|
t.mock.timers.enable({ apis: ['Date', 'setTimeout'], now: 0 });
|
|
let calls = 0,
|
|
deadline;
|
|
const fake = {
|
|
waitForReady: (_, cb) => cb(),
|
|
addCron: (_req, _meta, options, cb) => {
|
|
calls++;
|
|
deadline = options.deadline;
|
|
const budget = deadline - Date.now();
|
|
setTimeout(
|
|
() =>
|
|
cb(
|
|
delay <= budget
|
|
? null
|
|
: Object.assign(Error('deadline'), {
|
|
code: grpc.status.DEADLINE_EXCEEDED,
|
|
}),
|
|
{},
|
|
),
|
|
Math.min(delay, budget),
|
|
);
|
|
},
|
|
};
|
|
const client = load('back/schedule/client.ts', {
|
|
'../protos/cron': {
|
|
...require('../../back/protos/cron'),
|
|
CronClient: class {
|
|
constructor() {
|
|
return fake;
|
|
}
|
|
},
|
|
},
|
|
'../config': { grpcPort: 5500 },
|
|
'../config/grpcCerts': {
|
|
getGrpcCerts: () => ({
|
|
caCert: 'ca',
|
|
clientKey: 'key',
|
|
clientCert: 'cert',
|
|
}),
|
|
},
|
|
'@grpc/grpc-js': { ...grpc, credentials: { createSsl: () => ({}) } },
|
|
}).default;
|
|
const readiness = new SchedulerReadiness(async () => {}, 60000);
|
|
t.after(() => clearTimeout(readiness.retry));
|
|
client.readiness = readiness;
|
|
readiness.configure(() => client.addCron(rows, true));
|
|
const recovering = readiness.recover();
|
|
await new Promise(setImmediate);
|
|
assert.equal(calls, 1);
|
|
assert.ok(deadline <= 120000);
|
|
t.mock.timers.tick(Math.min(delay, deadline));
|
|
assert.equal(await recovering, succeeds);
|
|
assert.equal(calls, 1, 'uncertain writes are not replayed inline');
|
|
assert.equal(await readiness.check(), succeeds);
|
|
});
|
|
}
|