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",