diff --git a/app/goodnews.ts b/app/goodnews.ts index bb364a1dec23328b91092b7fc6261e6d3a380967..499a3015931cea0d03eb07247c939db9875b24aa 100644 --- a/app/goodnews.ts +++ b/app/goodnews.ts @@ -103,9 +103,8 @@ const fetchPage = const fetchResult = await fetch(url) const resultJson = await fetchResult.json() return resultJson - // deno-lint-ignore no-explicit-any - } catch (err: any) { - logger.error('fetching articles failed: ' + err.message) + } catch (err) { + logger.error('fetching articles failed:', err as Error) throw err } finally { const duration = Date.now() - start diff --git a/app/server.ts b/app/server.ts index fdc5ea791996113969121c802c95974915c2cfb8..dcf24362ca0a6618562b936425759b33f8295b60 100644 --- a/app/server.ts +++ b/app/server.ts @@ -1,5 +1,5 @@ import { GoodNewsArticleQueryResponse, GoodNewsLanguage } from './api_types.ts' -import { createSchema, createYoga, gql, serve, useResponseCache } from './deps.ts' +import { createSchema, createYoga, gql, useResponseCache } from './deps.ts' import { DEFAULT_LANGUAGE, executeArticlesQuery, SUPPORTED_LANGUAGES } from './goodnews.ts' import { logger } from './logging.ts' @@ -94,6 +94,8 @@ export interface ServerConfig { contentful: ContentfulConfig } +const isAlive = () => new Response(`${true}`) + export const createGraphQLServer = (config: ServerConfig): GraphQLServer => { const plugins = config.cacheEnabled || DEFAULT_CACHE_ENABLED ? [ @@ -128,17 +130,23 @@ export type GraphQLContext = { } } -export const startServer = (config: ServerConfig): Promise<void> => { +export const startServer = (config: ServerConfig): Deno.HttpServer<Deno.NetAddr> => { const graphQLServer: GraphQLServer = createGraphQLServer(config) - return serve(graphQLServer, { + return Deno.serve({ port: config.port || DEFAULT_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.contentful.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/deno.json b/deno.json index 0952ad56cc684bc2d2c3528f30514b8bb276d9e1..5697d87044c2067803f90743635f819812549f34 100644 --- a/deno.json +++ b/deno.json @@ -4,7 +4,7 @@ "lint": "deno lint", "fmt": "deno fmt", "fmt:check": "deno fmt --check", - "test": "deno test --allow-import ", + "test": "deno test --allow-import", "updateDeps": "deno cache --allow-import --lock=deno.lock --lock-write app/deps.ts app/dev_deps.ts", "install": "deno cache --allow-import --reload --lock=deno.lock app/deps.ts app/dev_deps.ts", "cache": "deno cache --allow-import app/main.ts", diff --git a/terraform/environments/deployment.tf b/terraform/environments/deployment.tf index a728e61e6717614d3559d22d3995d76311f3a05d..585cad15ece0db5fd5442f599d630103a7f6da11 100644 --- a/terraform/environments/deployment.tf +++ b/terraform/environments/deployment.tf @@ -44,6 +44,21 @@ resource "google_cloud_run_service" "goodnews_api" { ports { container_port = 8002 } + 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