forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
process: add optional timeout to process.exit()
When set, an internal timer will be set that will exit the process at the given timeout. In the meantime, the registered listeners for process.on('exitingSoon') will be invoked and passed a callback to be called when the handler is ready for the process to exit. The process will exit either when the internal timer fires or all the callbacks are called, whichever comes first. This is an attempt to deal more intelligently with resource cleanup and async op completion on exit (see nodejs#6456).
- Loading branch information
Showing
3 changed files
with
124 additions
and
2 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
|
||
// Schedule something that'll keep the loop active normally | ||
const timer1 = setInterval(() => {}, 1000); | ||
const timer2 = setInterval(() => {}, 1000); | ||
|
||
// Register an exitingSoon handler to clean up before exit | ||
process.on('exitingSoon', common.mustCall((ready) => { | ||
// Simulate some async task | ||
assert.strictEqual(process.exitCode, 0); | ||
// Shouldn't be callable twice | ||
assert.strictEqual(process.exit(0, 10000), false); | ||
setImmediate(() => { | ||
// Clean up resources | ||
clearInterval(timer1); | ||
// Notify that we're done | ||
ready(); | ||
}); | ||
})); | ||
|
||
process.on('exitingSoon', common.mustCall((ready) => { | ||
clearInterval(timer2); | ||
ready(); | ||
})); | ||
|
||
assert.strictEqual(process.exit(0, 10000), true); |