1 | #!/usr/bin/env node
|
2 |
|
3 |
|
4 | require('./lib/env');
|
5 |
|
6 |
|
7 | const cluster = require('cluster');
|
8 | const winston = require('winston');
|
9 |
|
10 |
|
11 | const log = require('lib/utilities/log');
|
12 | const config = require('config');
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | class App {
|
18 | |
19 |
|
20 |
|
21 | constructor() {
|
22 |
|
23 | this._master = cluster.isMaster;
|
24 | this._logger = false;
|
25 | this._workers = {};
|
26 |
|
27 |
|
28 | this.run = this.run.bind(this);
|
29 | this.exit = this.exit.bind(this);
|
30 | this.spawn = this.spawn.bind(this);
|
31 | this.logger = this.logger.bind(this);
|
32 | this.children = this.children.bind(this);
|
33 |
|
34 |
|
35 | this.logger();
|
36 |
|
37 |
|
38 | if (this._master) {
|
39 | this.children();
|
40 | } else {
|
41 | this.run();
|
42 | }
|
43 | }
|
44 |
|
45 | |
46 |
|
47 |
|
48 | run() {
|
49 |
|
50 | const eden = require('eden');
|
51 |
|
52 |
|
53 | this._logger.log('info', `Spawned new "${process.env.cluster}" cluster`, {
|
54 | class : 'Eden',
|
55 | });
|
56 |
|
57 |
|
58 | eden.start({
|
59 | id : process.env.id,
|
60 | port : parseInt(process.env.port, 10),
|
61 | host : process.env.host,
|
62 | logger : this._logger,
|
63 | cluster : process.env.cluster,
|
64 | });
|
65 | }
|
66 |
|
67 | |
68 |
|
69 |
|
70 |
|
71 |
|
72 | exit(worker) {
|
73 |
|
74 | const { id } = worker.process.env;
|
75 | const thread = worker.process.env.cluster;
|
76 |
|
77 |
|
78 | this.spawn(parseInt(id, 10), thread, (parseInt(config.get('port'), 10) + parseInt(id, 10)));
|
79 | }
|
80 |
|
81 | |
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | spawn(id, label, port = null) {
|
89 |
|
90 | const env = {
|
91 | ...process.env,
|
92 |
|
93 | id,
|
94 | cluster : label,
|
95 | };
|
96 |
|
97 |
|
98 | if (port !== null) {
|
99 | env.port = port;
|
100 | }
|
101 |
|
102 |
|
103 | this._workers[`${label}:${id}`] = cluster.fork(env);
|
104 | this._workers[`${label}:${id}`].process.env = env;
|
105 | }
|
106 |
|
107 | |
108 |
|
109 |
|
110 | logger() {
|
111 |
|
112 | this._logger = winston.createLogger({
|
113 | level : config.get('logLevel') || 'info',
|
114 | format : log,
|
115 | transports : [
|
116 | new winston.transports.Console(),
|
117 | ],
|
118 | });
|
119 | }
|
120 |
|
121 | |
122 |
|
123 |
|
124 | children() {
|
125 |
|
126 | this._logger.log('info', 'Running Eden', {
|
127 | class : 'Eden',
|
128 | });
|
129 |
|
130 |
|
131 | try {
|
132 |
|
133 | process.title = `edenjs - ${config.get('domain')} - master`;
|
134 | } catch (e) { }
|
135 |
|
136 |
|
137 | (config.get('clusters') || ['front', 'back']).forEach((label) => {
|
138 |
|
139 | for (let i = 0; i < (config.get('count') || 1); i += 1) {
|
140 | this.spawn(i, label, (config.get('router') || label === 'front') ? (parseInt(config.get('port'), 10) + i) : null);
|
141 | }
|
142 | });
|
143 |
|
144 |
|
145 | cluster.on('exit', this.exit);
|
146 | }
|
147 | }
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 | module.exports = App;
|