-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathapp_worker.js
102 lines (90 loc) · 3.15 KB
/
app_worker.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
'use strict';
const fs = require('fs');
const debug = require('debug')('egg-cluster');
const gracefulExit = require('graceful-process');
const ConsoleLogger = require('egg-logger').EggConsoleLogger;
const consoleLogger = new ConsoleLogger({ level: process.env.EGG_APP_WORKER_LOGGER_LEVEL });
// $ node app_worker.js options
const options = JSON.parse(process.argv[2]);
const Application = require(options.framework).Application;
debug('new Application with options %j', options);
const app = new Application(options);
const clusterConfig = app.config.cluster || /* istanbul ignore next */ {};
const listenConfig = clusterConfig.listen || /* istanbul ignore next */ {};
const port = options.port = options.port || listenConfig.port;
process.send({ to: 'master', action: 'realport', data: port });
app.ready(startServer);
// exit if worker start timeout
app.once('startTimeout', startTimeoutHandler);
function startTimeoutHandler() {
consoleLogger.error('[app_worker] start timeout, exiting with code:1');
process.exit(1);
}
function startServer(err) {
if (err) {
consoleLogger.error(err);
consoleLogger.error('[app_worker] start error, exiting with code:1');
process.exit(1);
}
app.removeListener('startTimeout', startTimeoutHandler);
let server;
let httpsOptions;
if (options.https) {
httpsOptions = Object.assign({}, options.https, {
key: fs.readFileSync(options.https.key),
cert: fs.readFileSync(options.https.cert),
});
}
if (options.http2) {
try {
const http2 = require('http2');
server = options.https ?
http2.createSecureServer(httpsOptions, app.callback()) :
http2.createServer(app.callback());
} catch (err) {
consoleLogger.error('[app_worker] server got error: %s, code: %s', err.message, err.code);
process.exit(1);
}
} else {
server = options.https ?
require('https').createServer(httpsOptions, app.callback()) :
require('http').createServer(app.callback());
}
server.once('error', err => {
consoleLogger.error('[app_worker] server got error: %s, code: %s', err.message, err.code);
process.exit(1);
});
// emit `server` event in app
app.emit('server', server);
if (options.sticky) {
server.listen(0, '127.0.0.1');
// Listen to messages sent from the master. Ignore everything else.
process.on('message', (message, connection) => {
if (message !== 'sticky-session:connection') {
return;
}
// Emulate a connection event on the server by emitting the
// event with the connection the master sent us.
server.emit('connection', connection);
connection.resume();
});
} else {
if (listenConfig.path) {
server.listen(listenConfig.path);
} else {
if (typeof port !== 'number') {
consoleLogger.error('[app_worker] port should be number, but got %s(%s)', port, typeof port);
process.exit(1);
}
const args = [ port ];
if (listenConfig.hostname) args.push(listenConfig.hostname);
debug('listen options %s', args);
server.listen(...args);
}
}
}
gracefulExit({
logger: consoleLogger,
label: 'app_worker',
beforeExit: () => app.close(),
});