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.
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
:
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();
}
}
// Subscribe to your service's shutdown event, run app.close() when emitted
app.get(ShutdownService).subscribeToShutdown(() => app.close());
See a running example here:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With