feat: per-region due scheduling + run_id to group pings across regions
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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 },
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user