UNPKG

3.22 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3// Require environment
4require('./lib/env');
5
6// Require dependencies
7const cluster = require('cluster');
8const winston = require('winston');
9
10// Require local dependencies
11const log = require('lib/utilities/log');
12const config = require('config');
13
14/**
15 * Create App class
16 */
17class App {
18 /**
19 * Construct App class
20 */
21 constructor() {
22 // Bind private variables
23 this._master = cluster.isMaster;
24 this._logger = false;
25 this._workers = {};
26
27 // Bind public methods
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 // Build logger
35 this.logger();
36
37 // Spawn children
38 if (this._master) {
39 this.children();
40 } else {
41 this.run();
42 }
43 }
44
45 /**
46 * Runs Eden
47 */
48 run() {
49 // Load eden
50 const eden = require('eden'); // eslint-disable-line global-require
51
52 // Log spawning threads
53 this._logger.log('info', `Spawned new "${process.env.cluster}" cluster`, {
54 class : 'Eden',
55 });
56
57 // Run single Eden instance
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 * On cluster worker exit
69 *
70 * @param {object} worker
71 */
72 exit(worker) {
73 // Set id
74 const { id } = worker.process.env;
75 const thread = worker.process.env.cluster;
76
77 // Spawn new thread
78 this.spawn(parseInt(id, 10), thread, (parseInt(config.get('port'), 10) + parseInt(id, 10)));
79 }
80
81 /**
82 * Spawns new App thread
83 *
84 * @param {number} id
85 * @param {String} label
86 * @param {number} port
87 */
88 spawn(id, label, port = null) {
89 // Clone environment and set thread id
90 const env = {
91 ...process.env,
92
93 id,
94 cluster : label,
95 };
96
97 // Set if port
98 if (port !== null) {
99 env.port = port;
100 }
101
102 // Fork new thread
103 this._workers[`${label}:${id}`] = cluster.fork(env);
104 this._workers[`${label}:${id}`].process.env = env;
105 }
106
107 /**
108 * Builds logger
109 */
110 logger() {
111 // Set logger
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 * Spawns child processes
123 */
124 children() {
125 // Log running Eden
126 this._logger.log('info', 'Running Eden', {
127 class : 'Eden',
128 });
129
130 // Set process name
131 try {
132 // Set process name
133 process.title = `edenjs - ${config.get('domain')} - master`;
134 } catch (e) { /* */ }
135
136 // spawn threads
137 (config.get('clusters') || ['front', 'back']).forEach((label) => {
138 // check count
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 // On cluster exit
145 cluster.on('exit', this.exit);
146 }
147}
148
149/**
150 * Export Eden App class
151 *
152 * @type {App}
153 */
154module.exports = App;