allow shutdown to happen without swallowing exceptions (#257)

This commit is contained in:
Jeff Williams 2012-11-10 12:04:35 -08:00
parent 13274551c5
commit 1a7a7f8588
2 changed files with 11 additions and 6 deletions

View File

@ -1,4 +1,4 @@
/*global app: true, args: true, env: true, Packages: true, publish: true */
/*global __timerPool: true, app: true, args: true, env: true, Packages: true, publish: true */
/**
* @project jsdoc
* @author Michael Mathews <micmath@gmail.com>
@ -433,5 +433,8 @@ catch(e) {
}
finally {
env.run.finish = new Date();
process.exit(0);
// on Rhino, we never exit unless we shut down the timer pool
if (__timerPool) {
__timerPool.shutdownNow();
}
}

View File

@ -16,6 +16,8 @@ __dirname = env.dirname;
* @see https://developer.mozilla.org/en-US/docs/DOM/window#Methods
*/
__timerPool = null;
setTimeout = null,
clearTimeout = null,
setInterval = null,
@ -23,7 +25,7 @@ clearInterval = null;
(function() {
// TODO: tune number of threads if necessary
var timerPool = new java.util.concurrent.ScheduledThreadPoolExecutor(10);
__timerPool = new java.util.concurrent.ScheduledThreadPoolExecutor(10);
var timers = {};
var timerCount = 1;
var timerUnits = java.util.concurrent.TimeUnit.MILLISECONDS;
@ -37,13 +39,13 @@ clearInterval = null;
setTimeout = function(fn, delay) {
var timerId = timerCount++;
var callback = getCallback(fn);
timers[timerId] = timerPool.schedule(callback, delay, timerUnits);
timers[timerId] = __timerPool.schedule(callback, delay, timerUnits);
return timerId;
};
clearTimeout = function(timerId) {
if (timers[timerId]) {
timerPool.remove(timers[timerId]);
__timerPool.remove(timers[timerId]);
delete timers[timerId];
}
};
@ -51,7 +53,7 @@ clearInterval = null;
setInterval = function(fn, delay) {
var timerId = timerCount++;
var callback = getCallback(fn);
timers[timerId] = timerPool.scheduleAtFixedRate(callback, delay, delay, timerUnits);
timers[timerId] = __timerPool.scheduleAtFixedRate(callback, delay, delay, timerUnits);
return timerId;
};