import { reportError } from './error-tracking' ; import { delay } from './util/promise' ; type ShutdownHandler = ( ) => Promise < unknown > ; const shutdownHandlers = [ ] ; export function registerShutdownHandler ( ) { process . on ( 'SIGTERM' , ( ) => shutdown ( 'SIGTERM' ) ) ; process . on ( 'SIGINT' , ( ) => shutdown ( 'SIGINT' ) ) ; } export function addShutdownHandler ( handler ) { shutdownHandlers . push ( handler ) ; } export async function shutdown ( cause ) { console . log ( `Shutting down after ${ cause }...` ) ; const shutdownPromises = Promise . all ( shutdownHandlers . map ( async ( handler ) => { try { await handler ( ) ; } catch ( e ) { reportError ( e ) ; } } ) ) ; await Promise . race ( [ shutdownPromises , delay ( 2000 ) ] ) ; process . exit ( 0 ) ; }