forked from meteor/meteor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleanup.js
36 lines (30 loc) · 823 Bytes
/
cleanup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/// A simple interface to register functions to be called when the process
/// exits.
var _ = require('underscore');
var Fiber = require("fibers");
var fiberHelpers = require('./fiber-helpers.js');
var cleanup = exports;
_.extend(exports, {
_exitHandlers: [],
// register a function that will be called on SIGINT (e.g. Cmd-C on
// mac)
onExit: function (func) {
this._exitHandlers.push(func);
}
});
var runHandlers = function () {
fiberHelpers.noYieldsAllowed(function () {
var handlers = cleanup._exitHandlers;
cleanup._exitHandlers = [];
_.each(handlers, function (f) {
f();
});
});
};
process.on('exit', runHandlers);
_.each(['SIGINT', 'SIGHUP', 'SIGTERM'], function (sig) {
process.once(sig, function () {
runHandlers();
process.kill(process.pid, sig);
});
});