Files
MikrotikManager/backend/src/routes/scheduler.ts
T
Denozordec ee6fd675ac
Docker images / prepare-release (push) Successful in 10s
Docker images / backend-test (push) Successful in 10m25s
Docker images / frontend-image (push) Successful in 3m18s
Docker images / updater-image (push) Successful in 41s
Docker images / netflow-agent-image (push) Successful in 32s
Docker images / backend-image (push) Successful in 2m48s
Docker images / publish-release (push) Successful in 10s
Docker images / notify-webhook (push) Successful in 1s
fix(routing): устранить конфликты чтения фильтров и маршрутов
2026-11-24 12:46:31 +07:00

67 lines
2.7 KiB
TypeScript

import type { FastifyPluginAsyncZod } from "@fastify/type-provider-zod"
import {
getSchedulerStatus,
JOB_KEYS,
listSchedulerRuns,
refreshScheduler,
runSchedulerJobNow,
} from "../services/scheduler.js"
function parseJobKey(raw: string | undefined): typeof JOB_KEYS[number] | null {
const s = String(raw ?? "").trim()
return (JOB_KEYS as readonly string[]).includes(s) ? (s as typeof JOB_KEYS[number]) : null
}
export function createSchedulerRoutes(deps: {
status: typeof getSchedulerStatus
runs: typeof listSchedulerRuns
refresh: typeof refreshScheduler
run: typeof runSchedulerJobNow
}): FastifyPluginAsyncZod {
return async (app) => {
app.get("/scheduler/status", async (_req, reply) => {
return reply.send(await deps.status())
})
app.get("/scheduler/runs", async (req, reply) => {
const q = req.query as { jobKey?: string; limit?: string; offset?: string }
const jobKey = q.jobKey ? parseJobKey(q.jobKey) : undefined
if (q.jobKey && jobKey == null) {
return reply.status(400).send({ error: "Некорректный jobKey" })
}
const limit = Math.min(200, Math.max(1, Number.parseInt(String(q.limit ?? "50"), 10) || 50))
const offset = Math.max(0, Number.parseInt(String(q.offset ?? "0"), 10) || 0)
return reply.send(await deps.runs({ jobKey: jobKey ?? undefined, limit, offset }))
})
app.post("/scheduler/refresh", async (_req, reply) => {
await deps.refresh()
return reply.send({ ok: true, status: await deps.status() })
})
app.post("/scheduler/jobs/:jobKey/run-now", async (req, reply) => {
const jobKey = parseJobKey(String((req.params as { jobKey?: string }).jobKey ?? ""))
if (!jobKey) return reply.status(400).send({ error: "Некорректный jobKey" })
try {
const result = await deps.run(jobKey)
let status: Awaited<ReturnType<typeof getSchedulerStatus>> | null = null
let statusError: string | undefined
try { status = await deps.status() }
catch (error) { statusError = error instanceof Error ? error.message : String(error) }
return reply.send({ ok: result.status === "ok", result, status, ...(statusError ? { statusError } : {}) })
} catch (e) {
const statusCode = (e as Error & { statusCode?: number }).statusCode
const msg = e instanceof Error ? e.message : String(e)
if (statusCode === 409) {
const code = (e as Error & { code?: string }).code
return reply.status(409).send({ error: msg, ...(code ? { code } : {}) })
}
return reply.status(500).send({ error: msg })
}
})
}
}
export default createSchedulerRoutes({ status: getSchedulerStatus, runs: listSchedulerRuns, refresh: refreshScheduler, run: runSchedulerJobNow })