Files
khb_microservices/packages/scheduler/src/settings.ts

32 lines
1.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { z } from "zod";
const daysSchema = z.union([z.literal("daily"), z.literal("workdays"), z.array(z.enum(["mon", "tue", "wed", "thu", "fri", "sat", "sun"]))]);
export const jobSchema = z.object({
name: z.string(),
call: z.object({
service: z.string(),
method: z.enum(["GET", "POST"]).default("POST"),
path: z.string(),
body: z.unknown().optional(),
}),
/** Ежедневный режим: время запуска HH:MM (по common.timezone). Не задавать вместе с everyMinutes. */
at: z.string().regex(/^\d{2}:\d{2}$/).optional(),
days: daysSchema,
/** Интервальный режим: запуск каждые N минут от полуночи (по common.timezone), в днях `days` и окне from-to. */
everyMinutes: z.number().int().min(1).max(720).optional(),
/** Окно интервального режима (HH:MM, включительно); по умолчанию весь день. */
from: z.string().regex(/^\d{2}:\d{2}$/).optional(),
to: z.string().regex(/^\d{2}:\d{2}$/).optional(),
}).refine((j) => (j.at !== undefined) !== (j.everyMinutes !== undefined), { message: "у задания должно быть ровно одно из: at, everyMinutes" });
export const schedulerSchema = z.object({
tickSeconds: z.number().default(10),
missedGraceSeconds: z.number().default(60),
http: z.object({ host: z.string().default("127.0.0.1"), port: z.number(), token: z.string().min(1) }),
jobs: z.array(jobSchema),
services: z.record(z.object({ baseUrl: z.string(), token: z.string() })),
});
export type SchedulerSettings = z.infer<typeof schedulerSchema>;
export type JobSettings = z.infer<typeof jobSchema>;