-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
141 lines (119 loc) · 3.2 KB
/
index.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
'use strict';
const fse = require('fs-extra');
const path = require('path');
class Daemon {
constructor(serverName, serverScript, options, logs, resp) {
if (!(this instanceof Daemon)) {
return new Daemon(serverName, serverScript, options, logs, resp);
}
const xConfig = require('xcraft-core-etc')(null, resp).load('xcraft');
this._serverName = serverName;
this._serverScript = serverScript;
this._options = options;
this._logs = logs;
this._resp = resp || {
log: {
verb: console.log,
info: console.log,
warn: console.warn,
err: console.error,
dbg: console.log,
},
};
this._proc = null;
this._pidFile = path.join(
xConfig.xcraftRoot,
'./var/run/' + serverName + 'd.pid'
);
}
get proc() {
return this._proc;
}
start() {
let isRunning = false;
if (fse.existsSync(this._pidFile)) {
this._resp.log.info('the ' + this._serverName + ' server seems running');
isRunning = true;
const pid = fse.readFileSync(this._pidFile, 'utf8');
try {
process.kill(pid, 0);
} catch (err) {
if (err.code === 'ESRCH') {
this._resp.log.info(
'but the process can not be found, then we try to start it'
);
fse.removeSync(this._pidFile);
isRunning = false;
}
}
}
if (!isRunning) {
const xProcess = require('xcraft-core-process')({
logger: 'daemon',
resp: this._resp,
});
/* TODO: add logging capabilities. */
const options = {
detached: this._options.detached,
stdio: this._options.detached || !this._logs ? 'ignore' : 'pipe',
env: this._options.env || process.env,
};
let args = [this._serverScript];
if (
process.env.hasOwnProperty('XCRAFT_DEBUG') &&
parseInt(process.env.XCRAFT_DEBUG) === 1
) {
args.unshift(`--inspect=${this._options.inspectPort || 9229}`);
}
if (process.argv.indexOf('--no-sandbox') !== -1) {
args.unshift('--no-sandbox');
}
if (this._options.argv) {
args = args.concat(this._options.argv);
}
this._proc = xProcess.spawn(
this._options.bin || process.execPath,
args,
options,
(err) => {
if (err) {
this._resp.log.err(err);
}
try {
fse.removeSync(this._pidFile);
} catch (ex) {
// ignore exceptions
}
}
);
this._resp.log.info(this._serverName + ' server PID: ' + this._proc.pid);
fse.writeFileSync(this._pidFile, `${this._proc.pid}`);
if (this._options.detached) {
this._proc.unref();
}
}
}
stop() {
try {
const pid = fse.readFileSync(this._pidFile, 'utf8');
process.kill(pid, 'SIGTERM');
try {
fse.removeSync(this._pidFile);
} catch (ex) {
// ignore exceptions
}
} catch (err) {
if (err.code !== 'ENOENT') {
this._resp.log.err(err);
}
}
}
restart() {
this.stop();
this.start();
}
isOurDaemon() {
return this._proc && this._proc.pid;
}
}
module.exports = Daemon;