Healthchecks
Healthchecks
Healthchecks are a proven way to verify that your application is working correctly,
usually they consist if simple operations to verify that each part of your program is working,
such as a database query to check if connectivity works.
In Basica, they can be easily added in all your services by implementing IHealthcheck
.
note
Any registered service or entrypoint implementing IHealthcheck
will also be registered as an healthcheck.
Implementing IHealthcheck
- Classes
- Object Factory
service.ts
import { IHealthcheck } from "@basica/core";
export class MyService implements IHealthcheck {
async healthcheck(signal: AbortSignal) {
// query the db
}
}
entrypoint.ts
import { IHealthcheckManager, IEntrypoint } from "@basica/core";
export class MyEntrypoint implements IEntrypoint {
constructor(private readonly healthcheckManager) {}
async start(signal: AbortSignal) {
const result = this.healthcheckManager.healthcheck();
if(result.status != "healthy") {
throw new Error(result);
}
}
async stop(signal: AbortSignal) {
// ...
}
}
service.ts
import { IHealthcheck } from "@basica/core";
export const createMyService = () => ({
healthcheck: async (signal: AbortSignal) => {
// query the db
}
}) satisfies IHealthcheck;
entrypoint.ts
import { IHealthcheckManager, IEntrypoint } from "@basica/core";
export const createMyEntrypoint = (healthcheckManager: IHealthcheckManager) => ({
start: async (signal: AbortSignal) => {
const result = healthcheckManager.healthcheck();
if(result.status != "healthy") {
throw new Error(result);
}
},
stop: async (signal: AbortSignal) => {
// ...
}
}) satisfies IEntrypoint;
index.ts
// ...
const app = new AppBuilder(container)
.configureLifecycle(b =>
b.addHealthcheck("my-service", () => container.myService)
.addEntrypoint("test", (s, h) => createMyEntrypoint(h))
)
.build();
app.run()