26 lines
1.2 KiB
TypeScript
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) };
|
|
};
|
|
}
|