Repository navigation
Expand file tree
/
Copy pathclusterprocess.js
More file actions
218 lines (184 loc) · 6.41 KB
/
Copy pathclusterprocess.js
File metadata and controls
218 lines (184 loc) · 6.41 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
* clusterprocess module
*
* @author deyhle@entrecode.de
*
* Used for startup and managing worker processes.
* Loosely based on https://gist.github.com/dickeyxxx/0f535be1ada0ea964cae – more info:
* http://blog.carbonfive.com/2014/06/02/node-js-in-production/
*
* This script will boot a given app.js with the number of available CPUs or NODE_CLUSTER_MAX_WORKER.
* The master will respond to SIGHUP, which will trigger restarting all the workers and reloading
* the app.
*/
const cluster = require('cluster');
const os = require('os');
const path = require('path');
let numWorker = os.cpus().length;
let logger = console;
if (process.env.NODE_CLUSTER_MAX_WORKER) {
numWorker = Number.parseInt(process.env.NODE_CLUSTER_MAX_WORKER, 10);
}
const DEFAULT_KILL_TIMEOUT = 10000;
function resolveKillTimeout(optionValue) {
const candidates = [optionValue, process.env.NODE_CLUSTER_KILL_TIMEOUT];
for (let i = 0; i < candidates.length; i += 1) {
const value = Number.parseInt(candidates[i], 10);
if (Number.isFinite(value) && value >= 0) {
return value;
}
}
return DEFAULT_KILL_TIMEOUT;
}
const ClusterProcess = {
run(executable, name, options = {}) {
if (!executable) {
logger.error('No executable specified (first parameter)');
process.exit(1);
}
let processname = name;
if (!processname) {
const packageJsonOfParentProject = require(path.resolve(__dirname, '../../package'));
if (packageJsonOfParentProject.name) {
processname = packageJsonOfParentProject.name;
} else {
logger.error('No project name found. Specify as second parameter or as "name" in your project\'s package.json');
// eslint-disable-next-line no-process-exit
process.exit(1);
}
}
process.title = `${processname}_cp`;
// Time a worker gets after SIGTERM before it is killed with SIGKILL
const killTimeout = resolveKillTimeout(options.killTimeout);
// Defines what each worker needs to run
cluster.setupMaster({ exec: path.resolve(__dirname, '../../', executable) });
// Gets the count of active workers
function numWorkers() {
return Object.keys(cluster.workers).length;
}
let stopping = false;
// Forks off the workers unless the server is stopping
function forkNewWorkers(worker) {
if (!stopping) {
const timeout = worker && !worker.exitedAfterDisconnect ? 2000 : 0;
setTimeout(() => {
// the master may have been stopped while waiting
for (let i = numWorkers(); i < numWorker && !stopping; i += 1) {
cluster.fork();
}
}, timeout);
}
}
// A list of workers queued for a restart
let workersToStop = [];
// Stops a single worker
// Gives killTimeout ms after SIGTERM before SIGKILL
function stopWorker(worker) {
logger.info('stopping worker', worker.process.pid);
worker.kill('SIGTERM');
const killTimer = setTimeout(() => {
logger.warn('worker did not stop in time, killing it', worker.process.pid);
worker.kill('SIGKILL');
}, killTimeout);
// Ensure we don't stay up just for this setTimeout
killTimer.unref();
}
// Tell the next worker queued to restart to stop
// This will allow the process to finish its work
// for killTimeout ms before it is killed
function stopNextWorker() {
const i = workersToStop.pop();
if (cluster.workers[i]) {
stopWorker(cluster.workers[i]);
}
}
// Runs options.onStop once all workers are gone, then exits the master
let stopped = false;
function finishStop() {
if (stopped || typeof options.onStop !== 'function' || numWorkers() > 0) {
return;
}
stopped = true;
Promise.resolve()
.then(() => options.onStop())
.catch((error) => logger.error('onStop failed', error))
// eslint-disable-next-line no-process-exit
.then(() => process.exit(0));
}
// Stops all the works at once
function stopAllWorkers() {
if (stopping) {
return;
}
stopping = true;
logger.info('stopping all workers');
Object.keys(cluster.workers).forEach((id) => {
stopWorker(cluster.workers[id]);
});
finishStop();
}
// Worker is now listening on a port
// Once it is ready, we can signal the next worker to restart
cluster.on('listening', stopNextWorker);
// A worker has disconnected either because the process was killed
// or we are processing the workersToStop array restarting each process
// In either case, we will fork any workers needed
// cluster.on('disconnect', forkNewWorkers);
cluster.on('exit', forkNewWorkers); // only after disconnect AND exit have fired, the worker
// count is correct. So better safe than sorry
cluster.on('exit', () => {
if (stopping) {
finishStop();
}
});
// HUP signal sent to the master process to start restarting all the workers sequentially
process.on('SIGHUP', () => {
logger.info('restarting all workers');
workersToStop = Object.keys(cluster.workers);
stopNextWorker();
});
// Kill all the workers at once
process.on('SIGTERM', stopAllWorkers);
// Fork off the initial workers
forkNewWorkers();
logger.info(
`${process.title} booted. pid is ${process.pid} scheduling is ${
cluster.schedulingPolicy === cluster.SCHED_RR ? 'round-robin' : 'off (system)'
}`,
);
return this;
},
setLogger(loggingInstance) {
if (
typeof loggingInstance.log === 'function' &&
typeof loggingInstance.info === 'function' &&
typeof loggingInstance.warn === 'function' &&
typeof loggingInstance.error === 'function'
) {
logger = loggingInstance;
return this;
}
logger.error('Could not attach new logger because of missing methods.');
return this;
},
handleSignals(cleanFunc) {
const func = cleanFunc || (() => undefined);
if (typeof func !== 'function') {
throw new Error('cleanFunc must be a function');
}
process.on('SIGHUP', () => {
logger.info(`${process.pid} got SIGHUP`);
func();
});
process.on('SIGINT', () => {
logger.info(`${process.pid} got SIGINT`);
func();
});
process.on('SIGTERM', () => {
logger.info(`${process.pid} got SIGTERM`);
func();
});
return this;
},
};
module.exports = ClusterProcess;