-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathapp.js
More file actions
176 lines (156 loc) · 4.71 KB
/
app.js
File metadata and controls
176 lines (156 loc) · 4.71 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
'use strict';
const cluster = require('cluster');
const cfork = require('cfork');
const sendmessage = require('sendmessage');
const gracefulExit = require('graceful-process');
const { BaseAppWorker, BaseAppUtils } = require('../../base/app');
const terminate = require('../../../terminate');
class AppWorker extends BaseAppWorker {
get id() {
return this.instance.id;
}
get workerId() {
return this.instance.process.pid;
}
get state() {
return this.instance.state;
}
get exitedAfterDisconnect() {
return this.instance.exitedAfterDisconnect;
}
get exitCode() {
return this.instance.exitCode;
}
send(...args) {
sendmessage(this.instance, ...args);
}
clean() {
this.instance.removeAllListeners();
}
static on(event, callback) {
process.on(event, callback);
}
static send(message) {
if (message && message.action === 'listening' && message.reusePort) {
// cluster won't get `listening` event when reusePort is true, use cluster `message` event instead
// rewrite message.action to 'reuse-port-listening'
message.action = 'reuse-port-listening';
cluster.worker.send(message);
return;
}
process.send(message);
}
static kill() {
process.exitCode = 1;
process.kill(process.pid);
}
static gracefulExit(options) {
gracefulExit(options);
}
}
class AppUtils extends BaseAppUtils {
fork() {
this.startTime = Date.now();
this.startSuccessCount = 0;
const args = [ JSON.stringify(this.options) ];
this.log('[master] start appWorker with args %j (process)', args);
cfork({
exec: this.getAppWorkerFile(),
args,
silent: false,
count: this.options.workers,
// don't refork in local env
refork: this.isProduction,
windowsHide: process.platform === 'win32',
});
let debugPort = process.debugPort;
cluster.on('fork', worker => {
const appWorker = new AppWorker(worker);
this.emit('worker_forked', appWorker);
appWorker.disableRefork = true;
worker.on('message', msg => {
if (typeof msg === 'string') {
msg = {
action: msg,
data: msg,
};
}
msg.from = 'app';
this.messenger.send(msg);
});
this.log('[master] app_worker#%s:%s start, state: %s, current workers: %j',
appWorker.id, appWorker.workerId, appWorker.state, Object.keys(cluster.workers));
// send debug message, due to `brk` scence, send here instead of app_worker.js
if (this.options.isDebug) {
debugPort++;
this.messenger.send({
to: 'parent',
from: 'app',
action: 'debug',
data: {
debugPort,
pid: appWorker.workerId,
},
});
}
});
cluster.on('disconnect', worker => {
const appWorker = new AppWorker(worker);
this.logger.info('[master] app_worker#%s:%s disconnect, suicide: %s, state: %s, current workers: %j',
appWorker.id, appWorker.workerId, appWorker.exitedAfterDisconnect, appWorker.state, Object.keys(cluster.workers));
});
cluster.on('exit', (worker, code, signal) => {
const appWorker = new AppWorker(worker);
this.messenger.send({
action: 'app-exit',
data: {
workerId: appWorker.workerId,
code,
signal,
},
to: 'master',
from: 'app',
});
});
cluster.on('listening', (worker, address) => {
const appWorker = new AppWorker(worker);
this.logger.info('[master] app_worker#%s:%s listening on %j', appWorker.id, appWorker.workerId, address);
this.messenger.send({
action: 'app-start',
data: {
workerId: appWorker.workerId,
address,
},
to: 'master',
from: 'app',
});
});
// handle 'reuse-port-listening' message: { action: 'reuse-port-listening', data: { port: 3000 } }
cluster.on('message', (worker, message) => {
if (!message || message.action !== 'reuse-port-listening') {
return;
}
const address = message.data;
const appWorker = new AppWorker(worker);
this.logger.info('[master] app_worker#%s:%s reuse-port listening on %j', appWorker.id, appWorker.workerId, address);
this.messenger.send({
action: 'app-start',
data: {
workerId: appWorker.workerId,
address,
},
to: 'master',
from: 'app',
});
});
return this;
}
async kill(timeout) {
await Promise.all(Object.keys(cluster.workers).map(id => {
const worker = cluster.workers[id];
worker.disableRefork = true;
return terminate(worker, timeout);
}));
}
}
module.exports = { AppWorker, AppUtils };