Lifecycle Hooks
Hook into the application lifecycle for initialization and cleanup.
Lifecycle hooks let providers and controllers run logic at specific points during application startup and shutdown.
Startup Hooks
Startup hooks execute in order after the module is initialized:
| Hook | Interface | Description |
|---|---|---|
onModuleInit() | OnModuleInit | Called after the module's dependencies are resolved |
onApplicationBootstrap() | OnApplicationBootstrap | Called after all modules are initialized |
import { Injectable, OnModuleInit, OnApplicationBootstrap } from '@velajs/vela';
@Injectable()
class DatabaseService implements OnModuleInit, OnApplicationBootstrap {
onModuleInit() {
console.log('Module initialized — connecting to database');
}
onApplicationBootstrap() {
console.log('Application ready — running migrations');
}
}Shutdown Hooks
Shutdown hooks execute in order when app.close() is called:
| Hook | Interface | Description |
|---|---|---|
beforeApplicationShutdown(signal?) | BeforeApplicationShutdown | Called before the app starts shutting down |
onModuleDestroy(signal?) | OnModuleDestroy | Called when the module is being destroyed |
onApplicationShutdown(signal?) | OnApplicationShutdown | Called after all modules are destroyed |
Each shutdown hook receives an optional signal parameter passed to app.close().
import {
Injectable, OnModuleDestroy, OnApplicationShutdown,
} from '@velajs/vela';
@Injectable()
class DatabaseService implements OnModuleDestroy, OnApplicationShutdown {
onModuleDestroy(signal?: string) {
console.log(`Module destroying (signal: ${signal}) — closing connections`);
}
onApplicationShutdown(signal?: string) {
console.log(`Application shut down (signal: ${signal})`);
}
}Triggering Shutdown
Call app.close() to trigger the shutdown sequence. An optional signal string is forwarded to all hooks:
const app = await VelaFactory.create(AppModule);
// Graceful shutdown
await app.close('SIGTERM');Execution Order
During shutdown, instances are called in reverse registration order. This ensures that dependencies are cleaned up before the services that depend on them.
@Module({
providers: [DatabaseService, CacheService, AppService],
})
class AppModule {}
// Shutdown order: AppService → CacheService → DatabaseServiceFull Example
import {
Injectable, Module,
OnModuleInit, OnApplicationBootstrap,
BeforeApplicationShutdown, OnModuleDestroy, OnApplicationShutdown,
} from '@velajs/vela';
@Injectable()
class AppService
implements OnModuleInit, OnApplicationBootstrap,
BeforeApplicationShutdown, OnModuleDestroy, OnApplicationShutdown {
onModuleInit() {
console.log('1. Module initialized');
}
onApplicationBootstrap() {
console.log('2. Application bootstrapped');
}
beforeApplicationShutdown(signal?: string) {
console.log(`3. Before shutdown (${signal})`);
}
onModuleDestroy(signal?: string) {
console.log(`4. Module destroying (${signal})`);
}
onApplicationShutdown(signal?: string) {
console.log(`5. Application shut down (${signal})`);
}
}Custom Decorators
Create reusable parameter decorators and metadata decorators.
Overview
First-class Cloudflare Workers integration for Vela applications.
Last updated on