This repository has been archived by the owner on Feb 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.ts
131 lines (124 loc) · 4.42 KB
/
server.ts
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
'use strict';
require('dotenv').config({ path: __dirname + '/../.env' });
require('module-alias')(__dirname);
// Include the cluster module
import * as cluster from 'cluster';
import * as log4js from 'log4js';
import { cpus } from 'os';
log4js.configure({
appenders: {
out: { type: 'stdout' },
},
categories: { default: { appenders: ['out'], level: 'debug' } },
});
import schedule from '@lib/schedule';
import { AppMessage, AppMessageTypes } from '@lib/interfaces';
let restartWorkers = true;
const workers: cluster.Worker[] = [];
if (cluster.isMaster) {
// Code to run if we're in the master process
process.title = 'M:WdesStats';
let logger = log4js.getLogger('server');
const stdin = process.openStdin();
const stopServer = (allowRestart: boolean = false): void => {
logger.debug('Received stop command !');
restartWorkers = allowRestart;
for (const workerId in cluster.workers) {
const worker = cluster.workers[workerId];
if (worker !== undefined) {
if (worker.isDead() === false && worker.isConnected()) {
try {
const appMessage: AppMessage = {
topic: AppMessageTypes.APP_STOP,
};
worker.send(appMessage);
// cluster.workers[workerId].kill();
} catch (error) {}
}
}
}
};
process.on('SIGTERM', () => {
logger.debug('Received SIGTERM');
stopServer();
});
process.on('SIGINT', () => {
logger.debug('Received SIGINT');
stopServer();
});
stdin.addListener('data', (d) => {
// note: d is an object, and when converted to a string it will
// end with a linefeed. so we (rather crudely) account for that
// with toString() and then trim()
const inputStr = d.toString().trim();
const args = inputStr.split(' ');
switch (args[0]) {
case 'reload':
stopServer(true);
break;
case 'stop':
stopServer();
break;
}
});
logger = log4js.getLogger('master');
for (const _ of cpus()) {
const thread = cluster.fork();
thread.on('message', (data: AppMessage) => {
if (typeof data === 'object') {
switch (data.topic) {
case AppMessageTypes.SCHEDULE_SERVER:
logger.debug('Received ', data.topic, ' from ', thread.id);
schedule.scheduleServer(data.body);
break;
case AppMessageTypes.UNSCHEDULE_SERVER:
logger.debug('Received ', data.topic, ' from ', thread.id);
schedule.unScheduleServer(data.body);
break;
case AppMessageTypes.ASK_TASKS_LIST:
logger.debug('Received ', data.topic, ' from ', thread.id);
const worker = cluster.workers[thread.id];
if (worker !== undefined) {
if (worker.isDead() === false && worker.isConnected()) {
worker.send(schedule.getTasks());
}
}
break;
}
}
});
workers.push(thread);
}
cluster.on('exit', (worker, code, signal) => {
logger.info(
'worker %d died (%s). ' + (restartWorkers ? 'restarting...' : ''),
worker.process.pid,
signal || code
);
if (restartWorkers) {
workers.push(cluster.fork());
}
let nbr = 0;
for (const workerId in workers) {
// Count alive workers
if (workers[workerId].isDead() === false) {
nbr++;
}
}
if (nbr === 0) {
// Everybody dead, kill master process.
cluster.disconnect(() => {
logger.info('Exit main process');
log4js.shutdown(() => {
process.exit(0);
});
});
}
});
logger.info('master is done', process.pid);
schedule.init();
} else if (cluster.isWorker) {
process.title = 'W' + cluster.worker.id + ':WdesStats';
// Workers code
require('@src/main');
}