From 001143652e7420fbd35e56929d3c67df7a5afcd1 Mon Sep 17 00:00:00 2001 From: Ole Langbehn <ole.langbehn@inoio.de> Date: Mon, 2 Dec 2024 16:26:11 +0100 Subject: [PATCH] feat(infra): startup and liveness probes for cloud run --- app/server.ts | 12 ++++++++++-- terraform/environments/deployment.tf | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/app/server.ts b/app/server.ts index 9f558d3..5179681 100644 --- a/app/server.ts +++ b/app/server.ts @@ -163,6 +163,8 @@ const getLanguage = (headers?: Headers) => { DEFAULT_LANGUAGE) as BetterPlaceLanguage } +const isAlive = () => new Response(`${true}`) + export const createGraphQLServer = (config: ServerConfig): GraphQLServer => { const plugins = config.cacheEnabled ? [ @@ -200,15 +202,21 @@ type GraphQLContext = { export const startServer = (config: ServerConfig): Promise<void> => { const graphQLServer: GraphQLServer = createGraphQLServer(config) - return serve(graphQLServer, { + return serve({ port: config.port, + handler: (req: Request) => { + const url = new URL(req.url) + const pathname = url.pathname + if (pathname.startsWith('/health') || pathname.startsWith('/ready')) return isAlive() + return graphQLServer.handleRequest(req) + }, onListen({ port, hostname }) { logger.info( `Server started at http://${hostname === '0.0.0.0' ? 'localhost' : hostname}:${port}/graphql`, ) if (config.fake) { logger.info( - `Server is serving fake data due to FAKE env var set to true`, + 'Server is serving fake data due to FAKE env var set to true', ) } }, diff --git a/terraform/environments/deployment.tf b/terraform/environments/deployment.tf index 37f5b84..2fa8ac9 100644 --- a/terraform/environments/deployment.tf +++ b/terraform/environments/deployment.tf @@ -44,6 +44,21 @@ resource "google_cloud_run_service" "donations_api" { ports { container_port = 8001 } + startup_probe { + http_get { + path = "/ready" + } + timeout_seconds = 10 + period_seconds = 10 + failure_threshold = 6 + } + liveness_probe { + http_get { + path = "/health" + } + timeout_seconds = 20 + period_seconds = 60 + } env { name = "ENVIRONMENT" value = local.environment -- GitLab