/
hub
executable file
·86 lines (70 loc) · 2 KB
/
hub
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
#!/usr/bin/env node
(function () {
'use strict';
module.exports = {};
var
fs = require('fs'),
cluster = require('cluster'),
workers = require('os').cpus().length,
worker,
app;
module.exports.require = function (path) {
var
stats = fs.statSync(path),
part;
if (stats.isFile()) {
require(process.cwd() + '/' + path)(module.exports);
} else if (stats.isDirectory()) {
fs.readdir(path, function (err, files) {
files.forEach(function (file) {
stats = fs.statSync(path + '/' + file);
if (stats.isFile()) {
if (file.indexOf('.js') !== -1) {
part = require(process.cwd() + '/' + path + '/' + file.replace('.js', ''));
if (typeof part === 'function') {
part(module.exports);
}
}
} else if (stats.isDirectory()) {
part = require(process.cwd() + '/' +path + '/' + file);
if (typeof part === 'function') {
part(module.exports);
}
}
});
});
}
};
module.exports.commander = require('commander');
module.exports.commander.option('-w, --workers [number]', 'Number of workers');
try {
app = require(process.cwd() + '/index');
} catch(e) {}
if (app && typeof app.init === 'function') {
app.init(module.exports);
}
module.exports.commander.parse(process.argv);
if (module.exports.commander.workers) {
module.exports.commander.workers = parseInt(module.exports.commander.workers, 10);
if (isNaN(module.exports.commander.workers) || module.exports.commander.workers < 0) {
console.log('Bad number of workers');
} else {
workers = module.exports.commander.workers;
}
}
if (!app || typeof app.master !== 'function' || typeof app.worker !== 'function') {
console.log('Application not found at ' + process.cwd());
process.exit(1);
}
if (app && typeof app.common === 'function') {
app.common(module.exports);
}
if (cluster.isMaster) {
for (worker = 0; worker < workers; worker++) {
cluster.fork();
}
app.master(module.exports);
} else {
app.worker(module.exports);
}
}());