Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger application shutdown from a service in Nest.js?

I'm looking for a way to trigger application shutdown from a service in Nest.js that will still call hooks.

I have a case when I'm handling a message in a service and in some scenario this should shutdown the application. I used to throw unhandled exceptions but when I do this Nest.js doesn't call hooks like onModuleDestroy or even shutdown hooks like onApplicationShutdown which are required in my case.

calling .close() from INestApplication works as expected but how do I inject it into my service? Or maybe there is some other pattern I could use to achieve what I'm trying to do?

Thank you very much for all the help.

like image 848
superrafal Avatar asked Jul 22 '19 12:07

superrafal


1 Answers

You cannot inject the application. Instead, you can emit a shutdown event from your service, let the application subscribe to it and then trigger the actual shutdown in your main.ts:

Service

export class ShutdownService implements OnModuleDestroy {
  // Create an rxjs Subject that your application can subscribe to
  private shutdownListener$: Subject<void> = new Subject();

  // Your hook will be executed
  onModuleDestroy() {
    console.log('Executing OnDestroy Hook');
  }

  // Subscribe to the shutdown in your main.ts
  subscribeToShutdown(shutdownFn: () => void): void {
    this.shutdownListener$.subscribe(() => shutdownFn());
  }

  // Emit the shutdown event
  shutdown() {
    this.shutdownListener$.next();
  }
}

main.ts

// Subscribe to your service's shutdown event, run app.close() when emitted
app.get(ShutdownService).subscribeToShutdown(() => app.close());

See a running example here:

Edit Nest shutdown from service

like image 134
Kim Kern Avatar answered Sep 19 '22 14:09

Kim Kern