-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_launcher.js
55 lines (45 loc) · 1.64 KB
/
server_launcher.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Parse commandline options (needed for foreground mode and pidfile)
const opt = require("node-getopt").create([
["F", "foreground", "stay in foreground"],
["p", "pidfile=ARG", "name of pidfile"],
["h", "help", "display this help"]
]).bindHelp().parseSystem();
const fs = require("fs");
// If in daemon mode, fork child and wait until it crashes, exits or disconnects
if (!opt.options.foreground) {
// Spawn the child process
var child = require("child_process").spawn(
process.execPath,
[ process.argv[1], "-F" ].concat(process.argv.slice(2)),
{
env: process.env,
cwd: process.cwd(),
stdio: [ "ignore", "ignore", "ignore", "ipc" ],
detached: false
}
);
// If the child exits, then just pass its exit code on
child.on("exit", (code, signal) => {
//console.log("child exited, code=" + code);
process.exit(code);
});
// If the child disconnects, then create the pid file and exit with success
child.on("disconnect", () => {
//console.log("child disconnected");
if (opt.options.pidfile) {
fs.writeFileSync(opt.options.pidfile, child.pid.toString() + "\n", { mode: 0o600 });
}
process.exit(0);
});
// Don't continue this script, but let the event loop running to wait for the child startup
return;
}
// Now we're in the child process
// Register exit handler for cleaning up the pid file
if (opt.options.pidfile) {
process.on("exit", (code) => {
fs.unlinkSync(opt.options.pidfile);
});
}
require("./server");
// vim: set ts=4 sw=4 et: