Skip to main content

Lifecycle

Lifecycle

Application lifecycle managent is the main feature of Basica. The following flowchart represents how it works.

For a service or entrypoint to be considered part of the lifecycle, it has to be registered in configureLifecycle().

Startup

A startup item is a service that performs a one off operation, such as running database migrations or loading a ML model into memory.

service.ts
import { IStartup } from "@basica/core";

export class MyService implements IStartup {

async start(signal: AbortSignal) {
// run migrations
}
}
index.ts
// ...
const app = new AppBuilder(container)
.configureLifecycle(b => b
b.addStartup("my-service", () => container.myService)
)
.build();
// ...

Shutdown

A shutdown item is a service that performs a cleanup of resources before shutdown, such as closing a database connection or stopping a server from accepting requests.

service.ts
import { IShutdown } from "@basica/core";

export class MyService implements IShutdown {

async shutdown(signal: AbortSignal) {
// close db connection
}
}
index.ts
// ...
const app = new AppBuilder(container)
.configureLifecycle(b => b
b.addGracefulShutdown("my-service", () => container.myService)
)
.build();
// ...

Entrypoint

An entrypoint is a service that performs both startup and shutdown operations, from which external external events interact with your application. It should be used as the starting point for your business logic, such as a message broker subscriber or an api server.

service.ts
import { IEntrypoint } from "@basica/core";

export class MyService implements IEntrypoint {

async shutdown(signal: AbortSignal) {
// listen for events from the msg broker
}

async shutdown(signal: AbortSignal) {
// stop listening
}
}
index.ts
// ...
const app = new AppBuilder(container)
.configureLifecycle(b => b
b.addEntrypoint("my-service", () => container.myService)
)
.build();
// ...