Каркас monorepo: scheduler, data_collector, khbuild_assistant_prep, khb_common; миграции; CI/CD
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
25
packages/scheduler/src/http-dispatch.ts
Normal file
25
packages/scheduler/src/http-dispatch.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import type { DispatchResult } from "./tick.js";
|
||||
import type { JobSettings, SchedulerSettings } from "./settings.js";
|
||||
|
||||
/** HTTP-вызов сервиса по описанию задания (Bearer-токен сервиса из settings.scheduler.services). */
|
||||
export function makeHttpDispatch(services: SchedulerSettings["services"], fetchFn: typeof fetch = fetch) {
|
||||
return async (job: JobSettings): Promise<DispatchResult> => {
|
||||
const svc = services[job.call.service];
|
||||
if (!svc) return { httpStatus: 0, message: `неизвестный сервис ${job.call.service}` };
|
||||
const res = await fetchFn(svc.baseUrl + job.call.path, {
|
||||
method: job.call.method,
|
||||
headers: { authorization: `Bearer ${svc.token}`, "content-type": "application/json" },
|
||||
body: job.call.method === "POST" ? JSON.stringify(job.call.body ?? {}) : undefined,
|
||||
signal: AbortSignal.timeout(30_000),
|
||||
});
|
||||
const text = await res.text();
|
||||
let ref: string | undefined;
|
||||
try {
|
||||
const j = JSON.parse(text) as { snapshotTs?: string; runId?: string };
|
||||
ref = j.snapshotTs ?? j.runId;
|
||||
} catch {
|
||||
/* ответ не JSON */
|
||||
}
|
||||
return { httpStatus: res.status, responseRef: ref, message: res.ok ? undefined : text.slice(0, 300) };
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user