diff --git a/app/server.ts b/app/server.ts index 9f558d32471010fb05d36f8661701a72ba8434ff..5179681818f6f0aa41e8301bc33b4e3003612cf9 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 37f5b84cce3712eb8d0fa8ed10085810b6afbb74..2fa8ac97ba5c732046d69bcd623bdb0c81b0c74c 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