Skip to content

Commit bb0118c

Browse files
committed
Improve readme
1 parent 8b6f21e commit bb0118c

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,25 @@ const prexit = require('prexit')
1010
prexit((signal, code_or_err) => {
1111
// Do cleanup before shutdown
1212
// Return a promise to delay exit
13+
// set prexit.code to exit with non 0
14+
// eg prexit.code = 1
15+
})
16+
```
17+
18+
Here's a sample for shutting down an http server and database.
19+
First we stop the http server from accepting any new connections.
20+
Then we gracefully close the database connection to allow any pending queries to resolve.
21+
22+
```js
23+
prexit(async () => {
24+
await new Promise(r => server.close(r))
25+
await db.end({ timeout: 5 })
1326
})
1427
```
1528

1629
Prexit is a simple function that takes a callback. This will be called with the signal and exit code / error on the following `process` events.
1730

18-
`exit | uncaughtException | SIGTSTP | SIGQUIT | SIGHUP | SIGTERM | SIGINT`
31+
`beforeExit | uncaughtException | SIGTSTP | SIGQUIT | SIGHUP | SIGTERM | SIGINT`
1932

2033
You can call prexit as many times as you'd like so you can do cleanup in the relevant places in your code. Prexit will await all promises that callbacks returns, and will ensure they are only called once. After all the promises finalizes prexit will call `prexit.ondone()` which defaults to calling `process.exit()`.
2134

0 commit comments

Comments
 (0)