⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.
TypeScript honoissue-managementissue-trackerjira-alternativekanbanlinear-alternativemcpproject-managementreactself-hostedtypescript
fix(api): compute scheduled job lease expiry in UTC
Tin357bc1a863f9
diff --git a/apps/api/src/scheduler/leader-lock.ts b/apps/api/src/scheduler/leader-lock.tsindex 1f5e2c883..aa4f27bcf 100644--- a/apps/api/src/scheduler/leader-lock.ts+++ b/apps/api/src/scheduler/leader-lock.ts@@ -14,14 +14,14 @@ export async function withJobLease<T>(whenHeldElsewhere: () => T,leaseMs: number = DEFAULT_LEASE_MS,): Promise<T> {- const expiresAt = new Date(Date.now() + leaseMs);-+ // expires_at has no time zone and holds UTC. Computing it in the database+ // keeps the lease independent of the Node process and session time zones.const claimed = await db.execute(sql`INSERT INTO job_lease ("name", "owner", "expires_at")- VALUES (${name}, ${INSTANCE_ID}, ${expiresAt})+ VALUES (${name}, ${INSTANCE_ID}, timezone('UTC', clock_timestamp()) + ${leaseMs} * interval '1 millisecond')ON CONFLICT ("name") DO UPDATESET "owner" = EXCLUDED."owner", "expires_at" = EXCLUDED."expires_at"- WHERE job_lease."expires_at" < now()+ WHERE job_lease."expires_at" < timezone('UTC', clock_timestamp())RETURNING "name";`);diff --git a/tests/api-integration/leader-lock.test.ts b/tests/api-integration/leader-lock.test.tsindex 902e34ae3..ca5cdedb7 100644--- a/tests/api-integration/leader-lock.test.ts+++ b/tests/api-integration/leader-lock.test.ts@@ -1,5 +1,5 @@import { sql } from "drizzle-orm";-import { beforeAll, describe, expect, it, vi } from "vite-plus/test";+import { afterAll, beforeAll, describe, expect, it, vi } from "vite-plus/test";import db from "../../apps/api/src/database";import { withJobLease } from "../../apps/api/src/scheduler/leader-lock";import { ensureTestDatabaseMigrated } from "./helpers/database";@@ -111,7 +111,7 @@ describe("withJobLease", () => {await db.execute(sql`INSERT INTO job_lease ("name", "owner", "expires_at")- VALUES (${LEASE}, 'dead-replica', now() - interval '1 minute');+ VALUES (${LEASE}, 'dead-replica', timezone('UTC', now()) - interval '1 minute');`);const result = await withJobLease(@@ -128,7 +128,7 @@ describe("withJobLease", () => {await db.execute(sql`INSERT INTO job_lease ("name", "owner", "expires_at")- VALUES (${LEASE}, 'other-replica', now() + interval '10 minutes');+ VALUES (${LEASE}, 'other-replica', timezone('UTC', now()) + interval '10 minutes');`);const result = await withJobLease(@@ -180,3 +180,41 @@ describe("withJobLease", () => {}});});++describe.each(["America/New_York", "Asia/Tokyo"])(+ "withJobLease when the process runs in %s",+ (timeZone) => {+ const originalTimeZone = process.env.TZ;++ beforeAll(() => {+ // Node applies TZ changes to Date immediately, so this stands in for a+ // server whose local time is not UTC.+ process.env.TZ = timeZone;+ });++ afterAll(() => {+ if (originalTimeZone === undefined) delete process.env.TZ;+ else process.env.TZ = originalTimeZone;+ });++ it("stores the expiry one lease window from now in UTC", async () => {+ await clearLease();++ const remaining = await withJobLease(+ LEASE,+ async () => {+ const rows = await db.execute(sql`+ SELECT extract(epoch FROM "expires_at" - timezone('UTC', now())) AS "seconds"+ FROM job_lease WHERE "name" = ${LEASE};+ `);+ return Number(rows.rows[0]?.seconds);+ },+ () => Number.NaN,+ 60_000,+ );++ expect(remaining).toBeGreaterThan(50);+ expect(remaining).toBeLessThanOrEqual(60);+ });+ },+);