feat: per-region due scheduling + run_id to group pings across regions

This commit is contained in:
M1
2026-03-18 16:36:35 +04:00
parent e057a65535
commit f7ab3b96b2
6 changed files with 20 additions and 5 deletions
+1
View File
@@ -51,6 +51,7 @@ export async function migrate() {
await sql`ALTER TABLE pings ADD COLUMN IF NOT EXISTS jitter_ms INTEGER`;
await sql`ALTER TABLE monitors ADD COLUMN IF NOT EXISTS regions TEXT[] NOT NULL DEFAULT '{}'`;
await sql`ALTER TABLE pings ADD COLUMN IF NOT EXISTS region TEXT`;
await sql`ALTER TABLE pings ADD COLUMN IF NOT EXISTS run_id TEXT`;
await sql`CREATE INDEX IF NOT EXISTS idx_pings_monitor ON pings(monitor_id, checked_at DESC)`;
await sql`CREATE INDEX IF NOT EXISTS idx_pings_checked_at ON pings(checked_at)`;
+4 -1
View File
@@ -32,7 +32,9 @@ export const internal = new Elysia({ prefix: "/internal", detail: { hide: true }
})
// Returns monitors that are due for a check.
// scheduled_at = last_checked_at + interval_s (ideal fire time), so jitter = actual_start - scheduled_at
// When a region is provided, due-ness and scheduled_at are based on
// the last ping from that specific region — so each region has its own
// independent schedule and they don't drift against each other.
.get("/due", async ({ request }) => {
const region = new URL(request.url).searchParams.get('region') || undefined;
const monitors = await sql`
@@ -45,6 +47,7 @@ export const internal = new Elysia({ prefix: "/internal", detail: { hide: true }
LEFT JOIN LATERAL (
SELECT checked_at FROM pings
WHERE monitor_id = m.id
AND (${region ? sql`region = ${region}` : sql`true`})
ORDER BY checked_at DESC LIMIT 1
) last ON true
WHERE m.enabled = true
+4 -2
View File
@@ -73,7 +73,7 @@ export const ingest = new Elysia()
const jitterMs = body.jitter_ms ?? null;
const [ping] = await sql`
INSERT INTO pings (monitor_id, scheduled_at, jitter_ms, status_code, latency_ms, up, error, meta, region)
INSERT INTO pings (monitor_id, scheduled_at, jitter_ms, status_code, latency_ms, up, error, meta, region, run_id)
VALUES (
${body.monitor_id},
${scheduledAt},
@@ -83,7 +83,8 @@ export const ingest = new Elysia()
${body.up},
${body.error ?? null},
${Object.keys(meta).length > 0 ? sql.json(meta) : null},
${body.region ?? null}
${body.region ?? null},
${body.run_id ?? null}
)
RETURNING *
`;
@@ -105,6 +106,7 @@ export const ingest = new Elysia()
cert_expiry_days: t.Optional(t.Nullable(t.Number())),
meta: t.Optional(t.Any()),
region: t.Optional(t.Nullable(t.String())),
run_id: t.Optional(t.Nullable(t.String())),
}),
detail: { hide: true },
})