-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp.js
158 lines (135 loc) · 3.23 KB
/
app.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
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
#!/usr/bin/env node
// Require environment
require('./lib/env');
// Require dependencies
const cluster = require('cluster');
const winston = require('winston');
// Require local dependencies
const log = require('lib/utilities/log');
const config = require('config');
// require local
const pack = require('./package.json');
/**
* Create App class
*/
class App {
/**
* Construct App class
*/
constructor() {
// Bind private variables
this._master = cluster.isMaster;
this._logger = false;
this._workers = {};
// Bind public methods
this.run = this.run.bind(this);
this.exit = this.exit.bind(this);
this.spawn = this.spawn.bind(this);
this.logger = this.logger.bind(this);
this.children = this.children.bind(this);
// Build logger
this.logger();
// Spawn children
if (this._master) {
this.children();
} else {
this.run();
}
}
/**
* Runs Eden
*/
run() {
// Load eden
const eden = require('eden'); // eslint-disable-line global-require
// Log spawning threads
this._logger.log('info', `Spawned new "${process.env.cluster}" cluster`, {
class : 'Eden',
});
// Run single Eden instance
eden.start({
id : process.env.id,
port : parseInt(process.env.port, 10),
host : process.env.host,
logger : this._logger,
cluster : process.env.cluster,
});
}
/**
* On cluster worker exit
*
* @param {object} worker
*/
exit(worker) {
// Set id
const { id } = worker.process.env;
const { port } = worker.process.env;
const thread = worker.process.env.cluster;
// Spawn new thread
this.spawn(parseInt(id, 10), thread, port);
}
/**
* Spawns new App thread
*
* @param {number} id
* @param {String} label
* @param {number} port
*/
spawn(id, label, port = null) {
// Clone environment and set thread id
const env = {
...process.env,
id,
cluster : label,
};
// Set if port
if (port !== null) {
env.port = port;
}
// Fork new thread
this._workers[`${label}:${id}`] = cluster.fork(env);
this._workers[`${label}:${id}`].process.env = env;
}
/**
* Builds logger
*/
logger() {
// Set logger
this._logger = winston.createLogger({
level : config.get('logLevel') || 'info',
format : log,
transports : [
new winston.transports.Console(),
],
});
}
/**
* Spawns child processes
*/
children() {
// Log running Eden
this._logger.log('info', `running edenJS v.${pack.version}`, {
class : 'Eden',
});
// Set process name
try {
// Set process name
process.title = `edenjs v.${pack.version} - ${config.get('domain')} - master`;
} catch (e) { /* */ }
// spawn threads
(config.get('clusters') || ['front', 'back']).forEach((label) => {
// check count
for (let i = 0; i < (config.get('count') || 1); i += 1) {
this.spawn(i, label, (config.get('router') || label === 'front') ? (parseInt(config.get('port'), 10) + i) : null);
}
});
// On cluster exit
cluster.on('exit', this.exit);
}
}
/**
* Export Eden App class
*
* @type {App}
*/
module.exports = App;