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
+51
-28
@@ -1,11 +1,11 @@
|
||||
import { ServerUnaryCall, sendUnaryData, status } from '@grpc/grpc-js';
|
||||
import { AddCronRequest, AddCronResponse } from '../protos/cron';
|
||||
import { createCronJob, CronJob } from '../shared/cronScheduler';
|
||||
import { isValidCronSchedule } from '../shared/cronSchedule';
|
||||
import { createCronJob, CronJob } from '../cron/scheduling/cronScheduler';
|
||||
import { isValidCronSchedule } from '../cron/scheduling/cronSchedule';
|
||||
import { scheduleStacks } from './data';
|
||||
import { latestMissedSlot } from '../shared/cronRecovery';
|
||||
import { slotId } from '../shared/triggerHistory';
|
||||
import { runCron } from '../shared/runCron';
|
||||
import { latestMissedSlot } from '../cron/scheduling/cronRecovery';
|
||||
import { slotId } from '../cron/history';
|
||||
import { runCron } from '../cron/execution/runCron';
|
||||
import Logger from '../loaders/logger';
|
||||
import { t, tf } from '../shared/i18n';
|
||||
|
||||
@@ -63,9 +63,15 @@ const addCron = (
|
||||
const recoveries: (() => Promise<void>)[] = [];
|
||||
try {
|
||||
for (const item of desired.values()) {
|
||||
const signature = JSON.stringify([item.schedule, item.command,
|
||||
(item.extra_schedules || []).map((s) => s.schedule), item.max_queue_wait || 0,
|
||||
!!item.skip_if_pending, item.catchup_seconds || 0, item.catchup_seconds || item.skip_if_pending ? item.recovery_from : 0]);
|
||||
const signature = JSON.stringify([
|
||||
item.schedule,
|
||||
item.command,
|
||||
(item.extra_schedules || []).map((s) => s.schedule),
|
||||
item.max_queue_wait || 0,
|
||||
!!item.skip_if_pending,
|
||||
item.catchup_seconds || 0,
|
||||
item.catchup_seconds || item.skip_if_pending ? item.recovery_from : 0,
|
||||
]);
|
||||
const current = scheduleStacks.get(item.id);
|
||||
if (current && definitions.get(current) === signature) continue;
|
||||
const jobs: CronJob[] = [];
|
||||
@@ -75,31 +81,44 @@ const addCron = (
|
||||
item.schedule,
|
||||
...(item.extra_schedules || []).map((x) => x.schedule),
|
||||
].entries()) {
|
||||
const fire = async (date: Date, source: 'node' | 'recovery' = 'node') => {
|
||||
const fire = async (
|
||||
date: Date,
|
||||
source: 'node' | 'recovery' = 'node',
|
||||
) => {
|
||||
const isCurrent = () => scheduleStacks.get(item.id) === jobs;
|
||||
if (!isCurrent()) return;
|
||||
await runCron(item.command, item, isCurrent, date && {
|
||||
id: slotId(item.id, ruleIndex, schedule, +date), time: +date, source,
|
||||
});
|
||||
await runCron(
|
||||
item.command,
|
||||
item,
|
||||
isCurrent,
|
||||
date && {
|
||||
id: slotId(item.id, ruleIndex, schedule, +date),
|
||||
time: +date,
|
||||
source,
|
||||
},
|
||||
);
|
||||
};
|
||||
if (call.request.replace && item.catchup_seconds && item.recovery_from) {
|
||||
if (
|
||||
call.request.replace &&
|
||||
item.catchup_seconds &&
|
||||
item.recovery_from
|
||||
) {
|
||||
recoveries.push(async () => {
|
||||
const now = Date.now();
|
||||
const since = Math.max(item.recovery_from!, now - item.catchup_seconds! * 1000);
|
||||
const since = Math.max(
|
||||
item.recovery_from!,
|
||||
now - item.catchup_seconds! * 1000,
|
||||
);
|
||||
const time = latestMissedSlot(schedule, since, now);
|
||||
if (time !== null) await fire(new Date(time), 'recovery');
|
||||
});
|
||||
}
|
||||
jobs.push(
|
||||
createCronJob(
|
||||
schedule,
|
||||
fire,
|
||||
{
|
||||
name: `${item.id}: ${item.name || ''}`,
|
||||
logger: Logger,
|
||||
start: false,
|
||||
},
|
||||
),
|
||||
createCronJob(schedule, fire, {
|
||||
name: `${item.id}: ${item.name || ''}`,
|
||||
logger: Logger,
|
||||
start: false,
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -134,13 +153,17 @@ const addCron = (
|
||||
}
|
||||
|
||||
callback(null, null);
|
||||
for (const recover of recoveries) setImmediate(() => recover().catch((err) => Logger.error(String(err))));
|
||||
for (const recover of recoveries)
|
||||
setImmediate(() => recover().catch((err) => Logger.error(String(err))));
|
||||
};
|
||||
if (savePlan) {
|
||||
savePlan().then(activate).catch(error => {
|
||||
for (const jobs of prepared.values()) jobs.forEach(job => job.cancel());
|
||||
callback(error);
|
||||
});
|
||||
savePlan()
|
||||
.then(activate)
|
||||
.catch((error) => {
|
||||
for (const jobs of prepared.values())
|
||||
jobs.forEach((job) => job.cancel());
|
||||
callback(error);
|
||||
});
|
||||
} else activate();
|
||||
};
|
||||
|
||||
|
||||
+42
-19
@@ -10,7 +10,7 @@ import config from '../config';
|
||||
import { getGrpcCerts } from '../config/grpcCerts';
|
||||
|
||||
import { HealthService } from '../protos/health';
|
||||
import { SchedulerReadiness } from '../shared/schedulerReadiness';
|
||||
import { SchedulerReadiness } from '../cron/scheduling/schedulerReadiness';
|
||||
|
||||
class Client {
|
||||
readonly readiness = new SchedulerReadiness(() => this.probe());
|
||||
@@ -19,14 +19,14 @@ class Client {
|
||||
try {
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
this.client.waitForReady(Date.now() + timeoutMs, (err) =>
|
||||
err ? reject(err) : resolve()
|
||||
err ? reject(err) : resolve(),
|
||||
);
|
||||
});
|
||||
} catch (error) {
|
||||
this.readiness.invalidate();
|
||||
throw Object.assign(
|
||||
error instanceof Error ? error : new Error(String(error)),
|
||||
{ status: 503 }
|
||||
{ status: 503 },
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -40,7 +40,12 @@ class Client {
|
||||
HealthService.check.responseDeserialize,
|
||||
{ service: 'scheduler' },
|
||||
{ deadline: Date.now() + 1000 },
|
||||
(err, res) => err ? reject(err) : res?.status === 1 ? resolve() : reject(new Error('Scheduler unavailable')),
|
||||
(err, res) =>
|
||||
err
|
||||
? reject(err)
|
||||
: res?.status === 1
|
||||
? resolve()
|
||||
: reject(new Error('Scheduler unavailable')),
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -66,10 +71,15 @@ class Client {
|
||||
await this.waitForReady(2000);
|
||||
return new Promise((resolve, reject) => {
|
||||
this.client.setConcurrency(
|
||||
{ concurrency }, new Metadata(), { deadline: Date.now() + 5000 },
|
||||
{ concurrency },
|
||||
new Metadata(),
|
||||
{ deadline: Date.now() + 5000 },
|
||||
(err) => {
|
||||
if (err) {
|
||||
if (err.code === status.UNAVAILABLE || err.code === status.DEADLINE_EXCEEDED) {
|
||||
if (
|
||||
err.code === status.UNAVAILABLE ||
|
||||
err.code === status.DEADLINE_EXCEEDED
|
||||
) {
|
||||
this.readiness.invalidate();
|
||||
Object.assign(err, { status: 503 });
|
||||
}
|
||||
@@ -83,7 +93,7 @@ class Client {
|
||||
|
||||
async addCron(
|
||||
request: AddCronRequest['crons'],
|
||||
replace = false
|
||||
replace = false,
|
||||
): Promise<AddCronResponse> {
|
||||
await this.waitForReady(2000);
|
||||
// Include every rule in a recovery snapshot, but bound a stuck worker.
|
||||
@@ -99,7 +109,10 @@ class Client {
|
||||
{ deadline: Date.now() + registrationTimeoutMs },
|
||||
(err, res) => {
|
||||
if (err) {
|
||||
if (err.code === status.UNAVAILABLE || err.code === status.DEADLINE_EXCEEDED) {
|
||||
if (
|
||||
err.code === status.UNAVAILABLE ||
|
||||
err.code === status.DEADLINE_EXCEEDED
|
||||
) {
|
||||
// A timed-out write may already have reached the scheduler.
|
||||
// Reconcile its state from the DB instead of replaying the RPC.
|
||||
this.readiness.invalidate();
|
||||
@@ -108,24 +121,34 @@ class Client {
|
||||
return reject(err);
|
||||
}
|
||||
resolve(res);
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
async delCron(request: DeleteCronRequest['ids']): Promise<DeleteCronResponse> {
|
||||
async delCron(
|
||||
request: DeleteCronRequest['ids'],
|
||||
): Promise<DeleteCronResponse> {
|
||||
await this.waitForReady(2000);
|
||||
return new Promise((resolve, reject) => {
|
||||
this.client.delCron({ ids: request }, new Metadata(), { deadline: Date.now() + 5000 }, (err, res) => {
|
||||
if (err) {
|
||||
if (err.code === status.UNAVAILABLE || err.code === status.DEADLINE_EXCEEDED) {
|
||||
this.readiness.invalidate();
|
||||
Object.assign(err, { status: 503 });
|
||||
this.client.delCron(
|
||||
{ ids: request },
|
||||
new Metadata(),
|
||||
{ deadline: Date.now() + 5000 },
|
||||
(err, res) => {
|
||||
if (err) {
|
||||
if (
|
||||
err.code === status.UNAVAILABLE ||
|
||||
err.code === status.DEADLINE_EXCEEDED
|
||||
) {
|
||||
this.readiness.invalidate();
|
||||
Object.assign(err, { status: 503 });
|
||||
}
|
||||
return reject(err);
|
||||
}
|
||||
return reject(err);
|
||||
}
|
||||
resolve(res);
|
||||
});
|
||||
resolve(res);
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { CronJob } from '../shared/cronScheduler';
|
||||
import { CronJob } from '../cron/scheduling/cronScheduler';
|
||||
import { ToadScheduler } from 'toad-scheduler';
|
||||
|
||||
export const scheduleStacks = new Map<string, CronJob[]>();
|
||||
|
||||
@@ -8,7 +8,10 @@ import {
|
||||
import { addCron } from './addCron';
|
||||
import { delCron } from './delCron';
|
||||
import { randomUUID } from 'crypto';
|
||||
import { saveSchedulePlan, confirmSchedulePlan } from '../shared/scheduleAudit';
|
||||
import {
|
||||
saveSchedulePlan,
|
||||
confirmSchedulePlan,
|
||||
} from '../cron/scheduling/scheduleAudit';
|
||||
|
||||
// Keep acknowledgement, publication and timer replacement in request order,
|
||||
// even if the client times out and sends a replacement snapshot.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { ServerUnaryCall, sendUnaryData, status } from '@grpc/grpc-js';
|
||||
import os from 'os';
|
||||
import { AddCronResponse, SetConcurrencyRequest } from '../protos/cron';
|
||||
import taskLimit from '../shared/pLimit';
|
||||
import taskLimit from '../cron/execution/cronLimit';
|
||||
|
||||
export async function setConcurrency(
|
||||
call: ServerUnaryCall<SetConcurrencyRequest, AddCronResponse>,
|
||||
|
||||
Reference in new issue
Block a user