Skip to content

Commit

Permalink
refactor: Rewrite health check server
Browse files Browse the repository at this point in the history
  • Loading branch information
haphut committed Dec 13, 2023
1 parent 90439a3 commit 1dd09ab
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 43 deletions.
43 changes: 0 additions & 43 deletions src/healthCheck.ts

This file was deleted.

49 changes: 49 additions & 0 deletions src/util/healthCheck.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import http from "node:http";
import util from "node:util";
import type {
HealthCheckConfig,
HealthCheckServer,
HealthCheckStatus,
RuntimeResources,
} from "../types";

const isHealthy = (
isHealthSetToOk: boolean,
resources: Partial<RuntimeResources>,
) =>
isHealthSetToOk &&
resources.producer?.isConnected() &&
resources.hfpConsumer?.isConnected() &&
resources.partialApcConsumer?.isConnected();

const createHealthCheckServer = (
{ port }: HealthCheckConfig,
resources: Partial<RuntimeResources>,
): HealthCheckServer => {
let isHealthSetToOk = false;
let server: http.Server | undefined = http.createServer((req, res) => {
if (req.url === "/healthz") {
if (isHealthy(isHealthSetToOk, resources)) {
res.writeHead(204);
} else {
res.writeHead(500);
}
} else {
res.writeHead(404);
}
res.end();
});
server.listen(port);
const setHealth = (status: HealthCheckStatus) => {
isHealthSetToOk = status === "ok";
};
const close = async (): Promise<void> => {
if (server && server.listening) {
await util.promisify(server.close.bind(server))();
server = undefined;
}
};
return { close, setHealth };
};

export default createHealthCheckServer;

0 comments on commit 1dd09ab

Please sign in to comment.