forked from NodeBB/NodeBB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader.js
76 lines (67 loc) · 1.59 KB
/
loader.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"use strict";
var nconf = require('nconf'),
fs = require('fs'),
pidFilePath = __dirname + '/pidfile',
start = function() {
var fork = require('child_process').fork,
nbb_start = function() {
nbb = fork('./app', process.argv.slice(2), {
env: {
'NODE_ENV': process.env.NODE_ENV
}
});
nbb.on('message', function(cmd) {
if (cmd === 'nodebb:restart') {
nbb_restart();
}
});
},
nbb_stop = function() {
nbb.kill();
if (fs.existsSync(pidFilePath)) {
var pid = parseInt(fs.readFileSync(pidFilePath, { encoding: 'utf-8' }), 10);
if (process.pid === pid) {
fs.unlinkSync(pidFilePath);
}
}
},
nbb_restart = function() {
nbb.on('exit', function() {
nbb_start();
});
nbb.kill();
};
process.on('SIGINT', nbb_stop);
process.on('SIGTERM', nbb_stop);
process.on('SIGHUP', nbb_restart);
nbb_start();
},
nbb;
nconf.argv();
// Start the daemon!
if (nconf.get('d')) {
// Check for a still-active NodeBB process
if (fs.existsSync(pidFilePath)) {
try {
var pid = fs.readFileSync(pidFilePath, { encoding: 'utf-8' });
process.kill(pid, 0);
console.log('\n Error: Another NodeBB is already running!');
process.exit();
} catch (e) {
fs.unlinkSync(pidFilePath);
}
}
// Initialise logging streams
var outputStream = fs.createWriteStream(__dirname + '/logs/output.log');
outputStream.on('open', function(fd) {
// Daemonize
require('daemon')({
stdout: fd
});
// Write its pid to a pidfile
fs.writeFile(__dirname + '/pidfile', process.pid);
start();
});
} else {
start();
}