forked from sergeyksv/tinyhook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspawn.js
More file actions
175 lines (142 loc) · 3.78 KB
/
spawn.js
File metadata and controls
175 lines (142 loc) · 3.78 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
var hookio = require('./hook'),
async = require('async'),
path = require('path');
exports.spawn = function (hooks, callback) {
var self = this,
connections = 0,
local,
names;
if (self.children==null)
self.children=[];
function onError (err) {
self.emit('error::spawn', err);
if (callback) {
callback(err);
}
}
if (!this.listening) {
return onError(new Error('Cannot spawn child hooks without calling `.listen()`'));
}
if (typeof hooks === "string") {
hooks = new Array(hooks);
}
types = {};
if (typeof hookio.forever === 'undefined') {
try {
hookio.forever = require('forever');
}
catch (ex) {
try {
hookio.forever = require('tinyforever');
}
catch (ex) {
hookio.forever = ex;
}
}
}
local = self.local || !hookio.forever || hookio.forever instanceof Error;
function cliOptions(options) {
var cli = [];
var reserved_cli = ['port', 'host', 'name', 'type'];
Object.keys(options).forEach(function (key) {
var value = options[key];
if (typeof value === 'object') {
value = JSON.stringify(value);
}
//
// TODO: Some type inspection to ensure that only
// literal values are accepted here.
//
if (reserved_cli.indexOf(key) === -1) {
cli.push('--' + key, value);
} else {
cli.push('--hook-' + key, value);
}
});
return cli;
};
function spawnHook (hook, next) {
var hookPath,
hookBin = __dirname + '/bin/forever-shim',
options,
child,
keys;
hook['host'] = hook['host'] || self['hook-host'];
hook['port'] = hook['port'] || self['hook-port'];
if (hook.src) {
// 1'st guess, this is path to file or module, i.e. just existent path
hookPath = path.resolve(hook.src);
if (!path.existsSync(hookPath)) {
// 2'nd guess, process module?
hookPath = process.cwd() + '/node_modules/' + hook.src;
if (!path.existsSync(hookPath)) {
// 3'nd guess, no idea, let require to resoolve it
hookPath = hook.src;
}
}
}
self.emit('hook::spawning', hook.name);
if (local) {
self.children[hook.name] = {
module: require(hookPath)
};
//
// Here we assume that the `module.exports` of any given `hook.io-*` module
// has **exactly** one key. We extract this Hook prototype and instantiate it.
//
keys = Object.keys(self.children[hook.name].module);
var mysun = self.children[hook.name];
mysun.Hook = mysun.module[keys[0]];
mysun._hook = new (mysun.Hook)(hook);
mysun._hook.start();
//
// When the hook has fired the `hook::ready` event then continue.
//
mysun._hook.once('hook::ready', next.bind(null, null));
}
else {
//
// TODO: Make `max` and `silent` configurable through the `hook.config`
// or another global config.
//
options = {
max: 10,
silent: false,
};
options.options = cliOptions(hook);
child = new (hookio.forever.Monitor)(hookBin, options);
child.on('start', function onStart (_, data) {
// Bind the child into the children and move on to the next hook
self.children[hook.name] = {
bin: hookBin,
monitor: child
};
self.emit('child::start', hook.name, self.children[hook.name]);
next();
});
child.on('restart', function () {
self.emit('child::restart', hook.name, self.children[hook.name]);
});
child.on('exit', function (err) {
self.emit('child::exit', hook.name, self.children[hook.name]);
});
child.start();
}
}
self.many('*::hook::ready', hooks.length, function () {
connections++;
if (connections === hooks.length) {
self.emit('children::ready', hooks);
}
});
async.forEach(hooks, spawnHook, function (err) {
if (err) {
return onError(err);
}
self.emit('children::spawned', hooks);
if (callback) {
callback();
}
});
return this;
};