Skip to main content

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

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) {
// ...
}
}
index.ts
// ...
const app = new AppBuilder(container)
.configureLifecycle(b =>
b.addHealthcheck("my-service", () => container.myService)
.addEntrypoint("test", (s, h) => createMyEntrypoint(h))
)
.build();

app.run()