-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Addressed initialization issues and patterns
- Loading branch information
1 parent
704cbb4
commit 9ea81e4
Showing
17 changed files
with
351 additions
and
116 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,66 @@ | ||
import type { DwnServer } from './dwn-server.js'; | ||
|
||
export const gracefulShutdown = (dwnServer: DwnServer): void => { | ||
dwnServer.stop(() => { | ||
console.log('http server stopped.. exiting'); | ||
process.exit(0); | ||
}); | ||
export const gracefulShutdown = async (dwnServer: DwnServer): Promise<void> => { | ||
await dwnServer.stop(); | ||
console.log('http server stopped.. exiting'); | ||
process.exit(0); | ||
}; | ||
|
||
export const setProcessHandlers = (dwnServer: DwnServer): void => { | ||
process.on('unhandledRejection', (reason, promise) => { | ||
export type ProcessHandlers = { | ||
unhandledRejectionHandler: (reason: any, promise: Promise<any>) => void, | ||
uncaughtExceptionHandler: (err: Error) => void, | ||
sigintHandler: () => Promise<void>, | ||
sigtermHandler: () => Promise<void> | ||
}; | ||
|
||
|
||
export const setProcessHandlers = (dwnServer: DwnServer): ProcessHandlers => { | ||
const unhandledRejectionHandler = (reason: any, promise: Promise<any>): void => { | ||
console.error( | ||
`Unhandled promise rejection. Reason: ${reason}. Promise: ${JSON.stringify( | ||
promise, | ||
)}`, | ||
); | ||
}); | ||
}; | ||
|
||
process.on('uncaughtException', (err) => { | ||
const uncaughtExceptionHandler = (err: Error): void => { | ||
console.error('Uncaught exception:', err.stack || err); | ||
}); | ||
}; | ||
|
||
// triggered by ctrl+c with no traps in between | ||
process.on('SIGINT', async () => { | ||
const sigintHandler = async (): Promise<void> => { | ||
console.log('exit signal received [SIGINT]. starting graceful shutdown'); | ||
await gracefulShutdown(dwnServer); | ||
}; | ||
|
||
gracefulShutdown(dwnServer); | ||
}); | ||
|
||
// triggered by docker, tiny etc. | ||
process.on('SIGTERM', async () => { | ||
const sigtermHandler = async (): Promise<void> => { | ||
console.log('exit signal received [SIGTERM]. starting graceful shutdown'); | ||
await gracefulShutdown(dwnServer); | ||
}; | ||
|
||
gracefulShutdown(dwnServer); | ||
}); | ||
process.on('unhandledRejection', unhandledRejectionHandler); | ||
process.on('uncaughtException', uncaughtExceptionHandler); | ||
process.on('SIGINT', sigintHandler); | ||
process.on('SIGTERM', sigtermHandler); | ||
|
||
// Store handlers to be able to remove them later | ||
return { | ||
unhandledRejectionHandler, | ||
uncaughtExceptionHandler, | ||
sigintHandler, | ||
sigtermHandler | ||
}; | ||
}; | ||
|
||
export const unsetProcessHandlers = (handlers: ProcessHandlers): void => { | ||
const { | ||
unhandledRejectionHandler, | ||
uncaughtExceptionHandler, | ||
sigintHandler, | ||
sigtermHandler | ||
} = handlers; | ||
|
||
process.off('unhandledRejection', unhandledRejectionHandler); | ||
process.off('uncaughtException', uncaughtExceptionHandler); | ||
process.off('SIGINT', sigintHandler); | ||
process.off('SIGTERM', sigtermHandler); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.