Files
khb_microservices/packages/scheduler/src/http-dispatch.ts
Yuri Yaryshev ebb1cb7950
Some checks failed
ci / test (push) Failing after 20s
ci / deploy (push) Has been skipped
Каркас monorepo: scheduler, data_collector, khbuild_assistant_prep, khb_common; миграции; CI/CD
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-11 09:44:55 +03:00

26 lines
1.2 KiB
TypeScript

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) };
};
}