import { BeforeApplicationShutdown , Injectable , Logger , OnApplicationBootstrap , OnApplicationShutdown , } from '@nestjs/common' ; enum State { UNKNOWN = 'UNKNOWN' , STARTING = 'STARTING' , UP = 'UP' , DOWN = 'DOWN' , STOPPING = 'STOPPING' , STOPPED = 'STOPPED' } @ Injectable ( ) export class AppService implements OnApplicationBootstrap , OnApplicationShutdown , BeforeApplicationShutdown { readonly logger = new Logger ( AppService . name ) ; private statusP = State . UNKNOWN ; constructor ( ) { } async onApplicationBootstrap ( ) { this . status = State . UP ; this . logger . log ( 'in ApplicationBootstrap, done' ) ; } async beforeApplicationShutdown ( ) { this . logger . log ( 'beforeApplicationShutdown called' ) ; return new Promise ( ( resolve ) => { this . logger . log ( 'starting shutdown...' ) ; setTimeout ( ( ) => { this . logger . log ( 'shutdown complete...' ) ; resolve ( ) ; } , 5000 ) ; } ) ; } onApplicationShutdown ( signal ) { this . status = State . STOPPING ; console . log ( 'in onApplicationShutdown, signal: ' , signal ) ; } get status ( ) : State { return this . statusP ; } set status ( status ) { switch ( this . statusP ) { case State . UNKNOWN : this . statusP = status ; break ; case State . STARTING : switch ( status ) { case State . STARTING : this . statusP = State . STARTING ; break ; case State . DOWN : this . statusP = State . DOWN ; break ; } break ; case State . UP : switch ( status ) { case State . STARTING : this . statusP = State . STARTING ; break ; case State . UP : this . statusP = State . UP ; break ; case State . DOWN : this . statusP = State . DOWN ; break ; } break ; case State . DOWN : break ; case State . STOPPING : switch ( status ) { case State . STOPPING : this . statusP = State . STOPPING ; break ; case State . DOWN : this . statusP = State . STOPPED ; break ; case State . STOPPED : this . statusP = State . STOPPED ; break ; } break ; case State . STOPPED : switch ( status ) { case State . STOPPING : this . statusP = State . STOPPING ; break ; case State . DOWN : this . statusP = State . STOPPED ; break ; } break ; } } }