forked from actionhero/actionhero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
actionhero.js
323 lines (276 loc) · 10.7 KB
/
actionhero.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
'use strict';
////////////////////////////////////////////////////////////////////////////
// actionhero framework in node.js
// http://www.actionherojs.com
// https://github.com/evantahler/actionhero
var path = require('path');
var async = require('async');
// HELPERS ///
var fatalError = function(api, errors, type){
if(errors && !(errors instanceof Array)){ errors = [errors]; }
if(errors){
if(api.log){
api.log(['Error with initializer step: %s', type], 'emerg');
errors.forEach(function(error){ api.log(error.stack, 'emerg'); });
}else{
console.error('Error with initializer step: ' + type);
errors.forEach(function(error){ console.error(error.stack); });
}
api.commands.stop.call(api, function(){
process.exit(1);
});
}
};
var sortNumber = function(a, b){
return a - b;
};
var flattenOrderedInitialzer = function(collection){
var output = [];
var keys = [];
for(var key in collection){
keys.push(parseInt(key));
}
keys.sort(sortNumber);
keys.forEach(function(key){
collection[key].forEach(function(d){
output.push(d);
});
});
return output;
};
// ACTIONHERO //
var actionhero = function(){
var self = this;
self.initializers = {};
self.api = {
running: false,
initialized: false,
shuttingDown: false
};
};
actionhero.prototype.initialize = function(params, callback){
var self = this;
if(this._self){ self = this._self; }
self.api._self = self;
self.api.commands = {
initialize: function(params, callback){ self.initialize.call(self, params, callback); },
start: function(params, callback){ self.start.call(self, params, callback); },
stop: function(callback){ self.stop.call(self, callback); },
restart: function(callback){ self.restart.call(self, callback); }
};
self.api.projectRoot = process.cwd();
if(process.env.project_root){
self.api.projectRoot = process.env.project_root;
}else if(process.env.projectRoot){
self.api.projectRoot = process.env.projectRoot;
}else if(process.env.PROJECT_ROOT){
self.api.projectRoot = process.env.PROJECT_ROOT;
}
if(!callback && typeof params === 'function'){
callback = params; params = {};
}
if(params === null){ params = {}; }
self.startingParams = params;
self.api._startingParams = self.startingParams;
self.api.initializerDefaults = {
load: 1000,
start: 1000,
stop: 1000
};
var loadInitializerRankings = {};
var startInitializerRankings = {};
var stopInitializerRankings = {};
self.configInitializers = [];
self.loadInitializers = [];
self.startInitializers = [];
self.stopInitializers = [];
// we need to load the config first
[
path.resolve(__dirname + '/initializers/' + 'utils.js'),
path.resolve(__dirname + '/initializers/' + 'config.js'),
].forEach(function(file){
var filename = file.replace(/^.*[\\\/]/, '');
var initializer = filename.split('.')[0];
delete require.cache[require.resolve(file)];
self.initializers[initializer] = require(file);
self.configInitializers.push(function(next){
self.initializers[initializer].initialize(self.api, next);
});
});
self.configInitializers.push(function(){
var customInitializers = [];
self.api.config.general.paths.initializer.forEach(function(startPath){
customInitializers = customInitializers.concat(self.api.utils.recursiveDirectoryGlob(startPath));
});
// load all other initializers
self.api.utils.arrayUniqueify(
self.api.utils.recursiveDirectoryGlob(__dirname + path.sep + 'initializers')
.sort()
.concat(
customInitializers
.sort()
)
).forEach(function(f){
var file = path.normalize(f);
var initializer = path.basename(f).split('.')[0];
var fileParts = file.split('.');
var ext = fileParts[(fileParts.length - 1)];
if(ext === 'js'){
delete require.cache[require.resolve(file)];
self.initializers[initializer] = require(file);
var loadFunction = function(next){
self.api.watchFileAndAct(file, function(){
self.api.log(['*** Rebooting due to initializer change (%s) ***', file], 'info');
self.api.commands.restart();
});
if(typeof self.initializers[initializer].initialize === 'function'){
if(typeof self.api.log === 'function'){ self.api.log(['Loading initializer: %s', initializer], 'debug', file); }
self.initializers[initializer].initialize(self.api, function(error){
try{ self.api.log(['Loaded initializer: %s', initializer], 'debug', file); }catch(e){ }
next(error);
});
}else{
next();
}
};
var startFunction = function(next){
if(typeof self.initializers[initializer].start === 'function'){
if(typeof self.api.log === 'function'){ self.api.log(['Starting initializer: %s', initializer], 'debug', file); }
self.initializers[initializer].start(self.api, function(error){
self.api.log(['Started initializer: %s', initializer], 'debug', file);
next(error);
});
}else{
next();
}
};
var stopFunction = function(next){
if(typeof self.initializers[initializer].stop === 'function'){
if(typeof self.api.log === 'function'){ self.api.log(['Stopping initializer: %s', initializer], 'debug', file); }
self.initializers[initializer].stop(self.api, function(error){
self.api.log(['Stopped initializer: %s', initializer], 'debug', file);
next(error);
});
}else{
next();
}
};
if(self.initializers[initializer].loadPriority === undefined){
self.initializers[initializer].loadPriority = self.api.initializerDefaults.load;
}
if(self.initializers[initializer].startPriority === undefined){
self.initializers[initializer].startPriority = self.api.initializerDefaults.start;
}
if(self.initializers[initializer].stopPriority === undefined){
self.initializers[initializer].stopPriority = self.api.initializerDefaults.stop;
}
if(loadInitializerRankings[self.initializers[initializer].loadPriority] === undefined){
loadInitializerRankings[self.initializers[initializer].loadPriority] = [];
}
if(startInitializerRankings[self.initializers[initializer].startPriority] === undefined){
startInitializerRankings[self.initializers[initializer].startPriority] = [];
}
if(stopInitializerRankings[self.initializers[initializer].stopPriority] === undefined){
stopInitializerRankings[self.initializers[initializer].stopPriority] = [];
}
if(self.initializers[initializer].loadPriority > 0){
loadInitializerRankings[self.initializers[initializer].loadPriority].push(loadFunction);
}
if(self.initializers[initializer].startPriority > 0){
startInitializerRankings[self.initializers[initializer].startPriority].push(startFunction);
}
if(self.initializers[initializer].stopPriority > 0){
stopInitializerRankings[self.initializers[initializer].stopPriority].push(stopFunction);
}
}
});
// flatten all the ordered initializer methods
self.loadInitializers = flattenOrderedInitialzer(loadInitializerRankings);
self.startInitializers = flattenOrderedInitialzer(startInitializerRankings);
self.stopInitializers = flattenOrderedInitialzer(stopInitializerRankings);
self.loadInitializers.push(function(){
process.nextTick(function(){
self.api.initialized = true;
callback(null, self.api);
});
});
async.series(self.loadInitializers, function(errors){ fatalError(self.api, errors, 'initialize'); });
});
async.series(self.configInitializers, function(errors){ fatalError(self.api, errors, 'config'); });
};
actionhero.prototype.start = function(params, callback){
var self = this;
if(this._self){ self = this._self; }
if(!callback && typeof params === 'function'){
callback = params; params = {};
}
var _start = function(){
self.api.running = true;
if(self.startInitializers[(self.startInitializers.length - 1)].name === 'finalStartInitializer'){
self.startInitializers.pop();
}
self.startInitializers.push(function finalStartInitializer(){
self.api.bootTime = new Date().getTime();
self.api.log(['*** ActionHero Started ***'], 'notice');
callback(null, self.api);
});
async.series(self.startInitializers, function(errors){ fatalError(self.api, errors, 'start'); });
};
if(self.api.initialized === true){
_start();
}else{
self.initialize(params, function(){
_start();
});
}
};
actionhero.prototype.stop = function(callback){
var self = this;
if(this._self){ self = this._self; }
if(self.api.running === true){
self.api.shuttingDown = true;
self.api.running = false;
self.api.initialized = false;
self.api.log('Shutting down open servers and stopping task processing...', 'alert');
if(self.stopInitializers[(self.stopInitializers.length - 1)].name === 'finalStopInitializer'){
self.stopInitializers.pop();
}
self.stopInitializers.push(function finalStopInitializer(){
self.api.unWatchAllFiles();
self.api.pids.clearPidFile();
self.api.log('*** ActionHero Stopped ***', 'alert');
self.api.log('***', 'debug');
delete self.api.shuttingDown;
process.nextTick(function(){
if(typeof callback === 'function'){ callback(null, self.api); }
});
});
async.series(self.stopInitializers, function(errors){ fatalError(self.api, errors, 'stop'); });
}else if(self.api.shuttingDown === true){
// double sigterm; ignore it
}else{
if(self.api.log){ self.api.log('Cannot shut down actionhero, not running', 'error'); }
if(typeof callback === 'function'){ callback(null, self.api); }
}
};
actionhero.prototype.restart = function(callback){
var self = this;
if(this._self){ self = this._self; }
if(self.api.running === true){
self.stop(function(error){
if(error){ self.api.log(error, 'error'); }
self.start(self.startingParams, function(error){
if(error){ self.api.log(error, 'error'); }
self.api.log('*** ActionHero Restarted ***', 'info');
if(typeof callback === 'function'){ callback(null, self.api); }
});
});
}else{
self.start(self.startingParams, function(error){
if(error){ self.api.log(error, 'error'); }
self.api.log('*** ActionHero Restarted ***', 'info');
if(typeof callback === 'function'){ callback(null, self.api); }
});
}
};
exports.actionheroPrototype = actionhero;