Lifecycle Events
Carno.js allows you to hook into key moments of the application lifecycle using decorators.
Available Hooks
| Decorator | Trigger |
|---|---|
@OnApplicationInit() | Called when the DI container initializes, before the server starts. |
@OnApplicationBoot() | Called right after the application is fully bootstrapped. |
@OnApplicationShutdown() | Called when the application receives a termination signal (SIGTERM, SIGINT). |
Usage
Decorate any method in your @Service or @Controller classes.
Execution Priority
All lifecycle decorators accept an optional priority parameter (default is 0). Hooks with a higher priority number are executed first. This is crucial when one service depends on another being initialized first.
import { Service, OnApplicationInit } from '@carno.js/core';
@Service()
export class ConfigService {
@OnApplicationInit(100) // Runs first
async loadConfig() {
console.log('Loading configuration...');
}
}
@Service()
export class DatabaseService {
@OnApplicationInit(50) // Runs after ConfigService
async connect() {
console.log('Connecting to database...');
}
}
Available Hooks
- Init: Providers are loaded.
@OnApplicationInithooks run. - Boot: Server starts.
@OnApplicationBoothooks run. - Runtime: Requests are handled.
- Shutdown: Signal received.
@OnApplicationShutdownhooks run.