From 1d0f7e22b7fb8f6295b149a6584933a3a657ec08 Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Wed, 11 Oct 2023 19:23:04 +0100 Subject: [PATCH] feat(faucet,store-indexer): add k8s healthcheck endpoints (#1739) --- .changeset/twelve-boats-kick.md | 6 ++++++ packages/faucet/bin/faucet-server.ts | 4 ++++ packages/store-indexer/bin/postgres-indexer.ts | 6 ++++++ packages/store-indexer/bin/sqlite-indexer.ts | 6 ++++++ 4 files changed, 22 insertions(+) create mode 100644 .changeset/twelve-boats-kick.md diff --git a/.changeset/twelve-boats-kick.md b/.changeset/twelve-boats-kick.md new file mode 100644 index 0000000000..2b66cb19f4 --- /dev/null +++ b/.changeset/twelve-boats-kick.md @@ -0,0 +1,6 @@ +--- +"@latticexyz/faucet": minor +"@latticexyz/store-indexer": minor +--- + +Added `/healthz` and `/readyz` healthcheck endpoints for Kubernetes diff --git a/packages/faucet/bin/faucet-server.ts b/packages/faucet/bin/faucet-server.ts index 625eb72232..d826af811f 100644 --- a/packages/faucet/bin/faucet-server.ts +++ b/packages/faucet/bin/faucet-server.ts @@ -22,6 +22,10 @@ const server = fastify({ await server.register(import("@fastify/cors")); +// k8s healthchecks +server.get("/healthz", (req, res) => res.code(200).send()); +server.get("/readyz", (req, res) => res.code(200).send()); + // @see https://trpc.io/docs/server/adapters/fastify server.register(fastifyTRPCPlugin, { prefix: "/trpc", diff --git a/packages/store-indexer/bin/postgres-indexer.ts b/packages/store-indexer/bin/postgres-indexer.ts index 3a0428684a..51d9a8c6b1 100644 --- a/packages/store-indexer/bin/postgres-indexer.ts +++ b/packages/store-indexer/bin/postgres-indexer.ts @@ -80,6 +80,7 @@ const { latestBlockNumber$, storedBlockLogs$ } = await createStoreSync({ storedBlockLogs$.subscribe(); +let isCaughtUp = false; combineLatest([latestBlockNumber$, storedBlockLogs$]) .pipe( filter( @@ -88,6 +89,7 @@ combineLatest([latestBlockNumber$, storedBlockLogs$]) first() ) .subscribe(() => { + isCaughtUp = true; console.log("all caught up"); }); @@ -98,6 +100,10 @@ const server = fastify({ await server.register(import("@fastify/cors")); +// k8s healthchecks +server.get("/healthz", (req, res) => res.code(200).send()); +server.get("/readyz", (req, res) => (isCaughtUp ? res.code(200).send("ready") : res.code(424).send("backfilling"))); + // @see https://trpc.io/docs/server/adapters/fastify server.register(fastifyTRPCPlugin, { prefix: "/trpc", diff --git a/packages/store-indexer/bin/sqlite-indexer.ts b/packages/store-indexer/bin/sqlite-indexer.ts index afa4ab97bb..b880c4b6d5 100644 --- a/packages/store-indexer/bin/sqlite-indexer.ts +++ b/packages/store-indexer/bin/sqlite-indexer.ts @@ -70,6 +70,7 @@ const { latestBlockNumber$, storedBlockLogs$ } = await syncToSqlite({ maxBlockRange: env.MAX_BLOCK_RANGE, }); +let isCaughtUp = false; combineLatest([latestBlockNumber$, storedBlockLogs$]) .pipe( filter( @@ -78,6 +79,7 @@ combineLatest([latestBlockNumber$, storedBlockLogs$]) first() ) .subscribe(() => { + isCaughtUp = true; console.log("all caught up"); }); @@ -88,6 +90,10 @@ const server = fastify({ await server.register(import("@fastify/cors")); +// k8s healthchecks +server.get("/healthz", (req, res) => res.code(200).send()); +server.get("/readyz", (req, res) => (isCaughtUp ? res.code(200).send("ready") : res.code(424).send("backfilling"))); + // @see https://trpc.io/docs/server/adapters/fastify server.register(fastifyTRPCPlugin, { prefix: "/trpc",