-
Notifications
You must be signed in to change notification settings - Fork 1
/
ecosystem.js
252 lines (220 loc) · 7.2 KB
/
ecosystem.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// =================================================
// ecosystem - lifecycle control and dependency
// injection for node.js apps. This will help you.
// =================================================
var util = require("util");
var events = require("events");
var path = require("path");
var _ = require("underscore");
var lifecycle = {
STATUS_NEW: "new",
STATUS_INITIALISING: "initialising",
STATUS_INITIALISED: "initialised",
STATUS_STARTING: "starting",
STATUS_STARTED: "started",
STATUS_STOPPING: "stopping",
STATUS_STOPPED: "stopped",
};
function Lifecycle(name) {
this._lifecycle_name = name;
this._lifecycle = {
status: lifecycle.STATUS_UNINITIALISED,
dependencies: {},
depending: [],
};
}
util.inherits(Lifecycle, events.EventEmitter);
_.extend(Lifecycle.prototype, {
dependencies: function() {
return [];
},
_lifecycle_dependencies: function() {
if (_.isFunction(this.lifecycle_dependencies)) {
return this.lifecycle_dependencies();
}
return this.dependencies();
},
lifecycle_dependency: function(name) {
var m = null;
// TODO: make this resolution smarter
var re = new RegExp(name + "$");
_(this._lifecycle.dependencies).each(function(dep, name) {
if (!m && name.match(re)) {
m = dep;
}
});
return m;
},
dependency: function(name) {
return this.lifecycle_dependency(name);
},
_init: function(config, all, next) {
var self = this;
if (self.lifecycleStatus() === lifecycle.STATUS_INITIALISING) {
throw new Error("Error: circular dependency discovered.");
}
if (self.lifecycleStatus() === lifecycle.STATUS_INITIALISED) {
// already initialised, we can continue
return next();
}
self._lifecycle_resolveDependencies(all);
// console.log("Initialising " + self._lifecycle_name);
self.setLifecycleStatus(lifecycle.STATUS_INITIALISING);
self._lifecycle_whenAll(_(self._lifecycle.dependencies).values(),
"_init",
[config, all],
function() {
var forward = function() {
self.setLifecycleStatus(lifecycle.STATUS_INITIALISED);
self.emit("init");
// console.log("Initialised " + self._lifecycle_name);
next();
};
if (_.isFunction(self.init)) {
self.init(config, all, forward);
} else {
forward();
}
}
);
},
_start: function(next) {
var self = this;
if (self.lifecycleStatus() === lifecycle.STATUS_STARTED) {
// already started, we can continue
return next();
}
// console.log("Starting " + self._lifecycle_name);
self.setLifecycleStatus(lifecycle.STATUS_STARTING);
self._lifecycle_whenAll(_(self._lifecycle.dependencies).values(),
"_start", [],
function() {
var forward = function () {
self.setLifecycleStatus(lifecycle.STATUS_STARTED);
self.emit("start");
// console.log("Started " + self._lifecycle_name);
next();
};
if (_.isFunction(self.start)) {
self.start(forward);
} else {
forward();
}
}
);
},
_stop: function(next) {
var self = this;
if (self.lifecycleStatus() === lifecycle.STATUS_STOPPED) {
return next();
}
// console.log("Stopping " + self._lifecycle_name);
self.setLifecycleStatus(lifecycle.STATUS_STOPPING);
self._lifecycle_whenAll(_(self._lifecycle.dependencies).values(),
"_stop", [],
function() {
var forward = function() {
self.setLifecycleStatus(lifecycle.STATUS_STOPPED);
self.emit("stop");
// console.log("Stopped " + self._lifecycle_name);
next();
};
if (_.isFunction(self.stop)) {
self.stop(forward);
} else {
forward();
}
}
);
},
_lifecycle_resolveDependencies: function(modules) {
var self = this;
_(this._lifecycle_dependencies()).each(function(name) {
var m = modules[name];
if (!m) {
throw new Error("No such module: " + name);
}
self._lifecycle.dependencies[name] = m;
self._lifecycle.depending.push(m);
});
},
_lifecycle_whenAll: function(modules, selector, args, next) {
var self = this;
var _mods = _(modules).clone();
var _drain = function() {
var mod = _mods.shift();
if (!mod) {
return next();
}
if (_.isFunction(mod[selector])) {
mod[selector].apply(mod, args);
}
};
args.push(_drain);
_drain();
},
lifecycleStatus: function() {
return this._lifecycleStatus;
},
setLifecycleStatus: function(v) {
this._lifecycleStatus = v;
},
});
var self = {
_load: function(moduleName, root) {
var canonical = moduleName;
if (root) {
canonical = path.resolve(root, moduleName);
}
// later allow for more advanced path searching, maybe?
var _m = require(canonical);
if (!_m || !_m.Lifecycle) {
log.error('Failed to load %s', moduleName);
throw('Failed to load ' + moduleName);
}
return _m;
},
loadAll: function(moduleNames, root) {
var modules = {};
_(moduleNames).each(function(name) {
var _m = self._load(name, root);
modules[name] = new _m.Lifecycle(name);
});
return modules;
},
initAll: function(config, modules, next) {
var _modules = _(modules).values();
var _init = function() {
var m = _modules.shift();
if (!m) { return next() }
m._init(config, modules, _init);
};
_init();
return self;
},
startAll: function(modules, next) {
next = next || function() {};
var _modules = _(modules).values();
var _start = function() {
var m = _modules.shift();
if (!m) { return next() }
m._start(_start);
};
_start();
return self;
},
stopAll: function(modules, next) {
next = next || function() {};
var _modules = _(modules).values();
var _stop = function() {
var m = _modules.shift();
if (!m) { return next() }
m._stop(_stop);
};
_stop();
return self;
}
};
module.exports = _.extend({
Lifecycle: Lifecycle,
}, self, lifecycle);