From 9936b1ed440859dc9e5e0e7a886eda5644ffda56 Mon Sep 17 00:00:00 2001 From: Aditya Choudhari Date: Tue, 15 Oct 2024 23:54:16 -0700 Subject: [PATCH 1/3] fix: Webservice healthcheck --- apps/webservice/src/app/api/healthz/route.ts | 15 ++++++++++++++- apps/webservice/src/env.ts | 1 + 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/apps/webservice/src/app/api/healthz/route.ts b/apps/webservice/src/app/api/healthz/route.ts index 5db491a8..7ed71bb0 100644 --- a/apps/webservice/src/app/api/healthz/route.ts +++ b/apps/webservice/src/app/api/healthz/route.ts @@ -1,5 +1,18 @@ import { NextResponse } from "next/server"; +import IORedis from "ioredis"; + +import { sql } from "@ctrlplane/db"; +import { db } from "@ctrlplane/db/client"; + +import { env } from "~/env"; + +export const GET = async () => { + try { + await new IORedis(env.REDIS_URL).ping(); + await db.execute(sql`SELECT 1`); + } catch (error) { + return NextResponse.json({ status: "error", error }, { status: 500 }); + } -export const GET = () => { return NextResponse.json({ status: "ok" }); }; diff --git a/apps/webservice/src/env.ts b/apps/webservice/src/env.ts index 0865b5b6..67b63890 100644 --- a/apps/webservice/src/env.ts +++ b/apps/webservice/src/env.ts @@ -24,6 +24,7 @@ export const env = createEnv({ GITHUB_BOT_PRIVATE_KEY: z.string().optional(), GITHUB_WEBHOOK_SECRET: z.string().optional(), BASE_URL: z.string(), + REDIS_URL: z.string(), OTEL_SAMPLER_RATIO: z.number().optional().default(1), }, From 55595cb68189b41f48ed29f47f7e22fc27ef04f8 Mon Sep 17 00:00:00 2001 From: Justin Brooks Date: Wed, 16 Oct 2024 16:51:49 -0400 Subject: [PATCH 2/3] clean up --- apps/webservice/src/app/api/healthz/route.ts | 7 +++---- packages/job-dispatch/src/index.ts | 2 ++ packages/job-dispatch/src/queue.ts | 6 +++--- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/apps/webservice/src/app/api/healthz/route.ts b/apps/webservice/src/app/api/healthz/route.ts index 7ed71bb0..6cac3c3f 100644 --- a/apps/webservice/src/app/api/healthz/route.ts +++ b/apps/webservice/src/app/api/healthz/route.ts @@ -1,16 +1,15 @@ import { NextResponse } from "next/server"; -import IORedis from "ioredis"; import { sql } from "@ctrlplane/db"; import { db } from "@ctrlplane/db/client"; - -import { env } from "~/env"; +import { checkHealth } from "@ctrlplane/job-dispatch"; export const GET = async () => { try { - await new IORedis(env.REDIS_URL).ping(); + await checkHealth(); await db.execute(sql`SELECT 1`); } catch (error) { + console.error(error); return NextResponse.json({ status: "error", error }, { status: 500 }); } diff --git a/packages/job-dispatch/src/index.ts b/packages/job-dispatch/src/index.ts index c663a461..34b09a37 100644 --- a/packages/job-dispatch/src/index.ts +++ b/packages/job-dispatch/src/index.ts @@ -9,6 +9,8 @@ export * from "./gradual-rollout.js"; export * from "./new-target.js"; export * from "./target.js"; export * from "./lock-checker.js"; +export * from "./queue.js"; + export { isDateInTimeWindow } from "./utils.js"; export * from "./policies/gradual-rollout.js"; diff --git a/packages/job-dispatch/src/queue.ts b/packages/job-dispatch/src/queue.ts index eaed6759..4b4b540a 100644 --- a/packages/job-dispatch/src/queue.ts +++ b/packages/job-dispatch/src/queue.ts @@ -10,7 +10,7 @@ const connection = new IORedis(env.REDIS_URL, { maxRetriesPerRequest: null }); export const dispatchJobsQueue = new Queue( Channel.DispatchJob, - { - connection, - }, + { connection }, ); + +export const checkHealth = () => connection.ping(); From da0db8c6be9c6e84c980a12111b1b1ee2ab8aef0 Mon Sep 17 00:00:00 2001 From: Justin Brooks Date: Wed, 16 Oct 2024 17:18:39 -0400 Subject: [PATCH 3/3] simplify --- apps/webservice/src/app/api/healthz/route.ts | 2 -- apps/webservice/src/env.ts | 1 - packages/job-dispatch/src/queue.ts | 2 -- 3 files changed, 5 deletions(-) diff --git a/apps/webservice/src/app/api/healthz/route.ts b/apps/webservice/src/app/api/healthz/route.ts index 6cac3c3f..875a54b6 100644 --- a/apps/webservice/src/app/api/healthz/route.ts +++ b/apps/webservice/src/app/api/healthz/route.ts @@ -2,11 +2,9 @@ import { NextResponse } from "next/server"; import { sql } from "@ctrlplane/db"; import { db } from "@ctrlplane/db/client"; -import { checkHealth } from "@ctrlplane/job-dispatch"; export const GET = async () => { try { - await checkHealth(); await db.execute(sql`SELECT 1`); } catch (error) { console.error(error); diff --git a/apps/webservice/src/env.ts b/apps/webservice/src/env.ts index 67b63890..0865b5b6 100644 --- a/apps/webservice/src/env.ts +++ b/apps/webservice/src/env.ts @@ -24,7 +24,6 @@ export const env = createEnv({ GITHUB_BOT_PRIVATE_KEY: z.string().optional(), GITHUB_WEBHOOK_SECRET: z.string().optional(), BASE_URL: z.string(), - REDIS_URL: z.string(), OTEL_SAMPLER_RATIO: z.number().optional().default(1), }, diff --git a/packages/job-dispatch/src/queue.ts b/packages/job-dispatch/src/queue.ts index 4b4b540a..8d0c9071 100644 --- a/packages/job-dispatch/src/queue.ts +++ b/packages/job-dispatch/src/queue.ts @@ -12,5 +12,3 @@ export const dispatchJobsQueue = new Queue( Channel.DispatchJob, { connection }, ); - -export const checkHealth = () => connection.ping();