Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix(web): reduce D1 heartbeat throttle to stay under offline threshold (#81)

Gener108a84a40d6e
diff --git a/src/web/src/app/api/daemon/tasks/poll/route.ts b/src/web/src/app/api/daemon/tasks/poll/route.ts
index 6a187aea4..8270805db 100644
--- a/src/web/src/app/api/daemon/tasks/poll/route.ts
+++ b/src/web/src/app/api/daemon/tasks/poll/route.ts
@@ -1,6 +1,6 @@
import { NextRequest } from "next/server";
import { getCloudflareContext } from "@opennextjs/cloudflare";
-import { queries, PollRequestSchema, semverGte, toAlookAddress, type FileRequestItem, type PollMeetingItem } from "@alook/shared";
+import { queries, PollRequestSchema, semverGte, toAlookAddress, OFFLINE_THRESHOLD_MS, POLL_INTERVAL_MS, type FileRequestItem, type PollMeetingItem } from "@alook/shared";
import { getDb, withD1Retry } from "@/lib/db"
import { withAuth } from "@/lib/middleware/auth";
import { writeJSON, writeError, parseBody } from "@/lib/middleware/helpers";
@@ -34,7 +34,10 @@ export const POST = withAuth(async (req: NextRequest, ctx) => {
return writeJSON({ tasks: [], evicted: true });
}
- // 2. Liveness: write heartbeat to KV (fast) + throttle D1 upsert to every 60s
+ // 2. Liveness: write heartbeat to KV (fast) + D1 upsert throttled to stay under OFFLINE_THRESHOLD
+ // KV is eventually consistent across colos, so D1 must stay fresh enough as fallback.
+ // Derived from timing constants so it auto-adapts if threshold or poll interval changes.
+ const D1_HEARTBEAT_THROTTLE_S = Math.floor((OFFLINE_THRESHOLD_MS - POLL_INTERVAL_MS) / 1000) - 1;
// Non-critical — D1 transient failures should not block task polling
const kv = (env as Env).CACHE_KV ?? null;
if (kv) {
@@ -47,7 +50,7 @@ export const POST = withAuth(async (req: NextRequest, ctx) => {
try {
await cached(
`hb_d1:${ctx.workspaceId}:${body.daemon_id}`,
- 60,
+ D1_HEARTBEAT_THROTTLE_S,
async () => {
await queries.machine.upsertMachine(db, {
daemonId: body.daemon_id,
diff --git a/src/web/src/app/api/runtimes/route.ts b/src/web/src/app/api/runtimes/route.ts
index 40ae03e26..c5b8f0f5b 100644
--- a/src/web/src/app/api/runtimes/route.ts
+++ b/src/web/src/app/api/runtimes/route.ts
@@ -26,7 +26,7 @@ export const GET = withAuth(async (req, ctx) => {
await Promise.all(runtimes.map(async (rt) => {
if (rt.daemonId) {
const hb = await kv.get(cacheKeys.heartbeat(ws.workspaceId, rt.daemonId)).catch(() => null);
- if (hb) (rt as any).machineLastSeenAt = hb;
+ if (hb) rt.machineLastSeenAt = hb;
}
}));
}