forked from HowProgrammingWorks/NodejsStarterKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
36 lines (28 loc) · 818 Bytes
/
server.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
'use strict';
const { Worker } = require('worker_threads');
const path = require('path');
const Config = require('./lib/config.js');
const PATH = process.cwd();
const CFG_PATH = path.join(PATH, 'config');
(async () => {
const config = await new Config(CFG_PATH);
const { sections } = config;
const count = sections.server.ports.length;
const workers = new Array(count);
const start = id => {
const worker = new Worker('./lib/worker.js');
workers[id] = worker;
worker.on('exit', code => {
if (code !== 0) start(id);
});
};
for (let id = 0; id < count; id++) start(id);
const stop = async () => {
console.log();
for (const worker of workers) {
worker.postMessage({ name: 'stop' });
}
};
process.on('SIGINT', stop);
process.on('SIGTERM', stop);
})();