From b0bcdbea666f8625b1b01488bd2c70784eb357c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl-Gerhard=20Lindesva=CC=88rd?= Date: Mon, 3 Jun 2024 23:25:15 +0200 Subject: [PATCH] add health check to api --- apps/api/src/index.ts | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/apps/api/src/index.ts b/apps/api/src/index.ts index 5410a2fd..d7b5ba1c 100644 --- a/apps/api/src/index.ts +++ b/apps/api/src/index.ts @@ -6,8 +6,10 @@ import { fastifyTRPCPlugin } from '@trpc/server/adapters/fastify'; import Fastify from 'fastify'; import metricsPlugin from 'fastify-metrics'; +import { db } from '@openpanel/db'; import type { IServiceClient } from '@openpanel/db'; -import { redisPub } from '@openpanel/redis'; +import { eventsQueue } from '@openpanel/queue'; +import { redis, redisPub } from '@openpanel/redis'; import type { AppRouter } from '@openpanel/trpc'; import { appRouter, createContext } from '@openpanel/trpc'; @@ -79,14 +81,22 @@ const startServer = async () => { fastify.get('/', (_request, reply) => { reply.send({ name: 'openpanel sdk api' }); }); - // fastify.get('/health-check', async (request, reply) => { - // try { - // await utils.healthCheck() - // reply.status(200).send() - // } catch (e) { - // reply.status(500).send() - // } - // }) + fastify.get('/healthcheck', async (request, reply) => { + try { + const [redisRes, dbRes, queueRes] = await Promise.all([ + redis.keys('*'), + db.project.findFirst(), + eventsQueue.getCompleted(), + ]); + reply.send({ + redis: redisRes.length > 0, + db: dbRes ? true : false, + queue: queueRes.length > 0, + }); + } catch (e) { + reply.status(500).send(); + } + }); if (process.env.NODE_ENV === 'production') { for (const signal of ['SIGINT', 'SIGTERM']) { process.on(signal, (err) => {