VelaVela
Techniques

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:

HookInterfaceDescription
onModuleInit()OnModuleInitCalled after the module's dependencies are resolved
onApplicationBootstrap()OnApplicationBootstrapCalled 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:

HookInterfaceDescription
beforeApplicationShutdown(signal?)BeforeApplicationShutdownCalled before the app starts shutting down
onModuleDestroy(signal?)OnModuleDestroyCalled when the module is being destroyed
onApplicationShutdown(signal?)OnApplicationShutdownCalled 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 → DatabaseService

Full 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})`);
  }
}
Edit on GitHub

Last updated on

On this page