Lifecycle Events

Every application element has a lifecycle managed by Nest. Nest offers lifecycle hooks that provide visibility into key life moments and theability to act when they occur.

Lifecycle sequence

After creating a injectable/controller by calling its constructor, Nest calls the lifecycle hook methods in the following sequence at specific moments:

OnModuleInit Called once the host module has been initialized
OnApplicationBootstrap Called once the application has fully started and is bootstrapped
OnModuleDestroy Cleanup just before Nest destroys the host module (app.close() method has been evaluated)
OnApplicationShutdown Responds to the system signals (when application gets shutdown by e.g. SIGTERM)

Usage

Each lifecycle hook is represented by interface. Interfaces are technically optional because they do not exist anyway after TypeScript compilation. Nonetheless, it’s a good practice to use them in order to benefit from strong typing and editor tooling.

  1. @@filename()
  2. import { Injectable, OnModuleInit } from '@nestjs/common';
  3. @Injectable()
  4. export class UsersService implements OnModuleInit {
  5. onModuleInit() {
  6. console.log(`The module has been initialized.`);
  7. }
  8. }
  9. @@switch
  10. import { Injectable } from '@nestjs/common';
  11. @Injectable()
  12. export class UsersService {
  13. onModuleInit() {
  14. console.log(`The module has been initialized.`);
  15. }
  16. }

Additionally, both OnModuleInit and OnApplicationBootstrap hooks allow you to defer the application initialization process (return a Promise or mark the method as async).

  1. @@filename()
  2. async onModuleInit(): Promise<void> {
  3. await this.fetch();
  4. }
  5. @@switch
  6. async onModuleInit() {
  7. await this.fetch();
  8. }

OnApplicationShutdown

The OnApplicationShutdown responds to the system signals (when application gets shutdown by e.g. SIGTERM).Use this hook to gracefully shutdown a Nest application. This feature is often used with Kubernetes,Heroku or similar services.

To use this hook you must activate a listener which listens to shutdown signals.

  1. import { NestFactory } from '@nestjs/core';
  2. import { AppModule } from './app.module';
  3. async function bootstrap() {
  4. const app = await NestFactory.create(AppModule);
  5. // Starts listening to shutdown hooks
  6. app.enableShutdownHooks();
  7. await app.listen(3000);
  8. }
  9. bootstrap();

If the application receives a signal it will call the onApplicationShutdown function of yourInjectable with the corresponding signal as first parameter. If your function does return apromise, it will not shutdown your Nest application until the promise is resolved or rejected.

  1. @@filename()
  2. @Injectable()
  3. class UsersService implements OnApplicationShutdown {
  4. onApplicationShutdown(signal: string) {
  5. console.log(signal); // e.g. "SIGINT"
  6. }
  7. }
  8. @@switch
  9. @Injectable()
  10. class UsersService implements OnApplicationShutdown {
  11. onApplicationShutdown(signal) {
  12. console.log(signal); // e.g. "SIGINT"
  13. }
  14. }