-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.js
69 lines (54 loc) · 1.54 KB
/
start.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
#!/usr/bin/env node
'use strict';
var forever = require('forever-monitor');
var argv = require('minimist')(process.argv.slice());
var _ = require('lodash');
var script = argv._[2];
var restricted_props = ["_", "env"];
function getEnvironment (){
var key = argv.env;
if(key === "production"){
return "production";
}
return "development"
}
function getRestarts(){
var env = getEnvironment();
if(env == "development"){
return 0;
}
return 2;
}
var child = new (forever.Monitor)(script,{
max: getRestarts() + 1, //Number of times script is allowed to execute
killTree: true,
cwd: __dirname,
env: (function() {
process.env.NODE_ENV = getEnvironment();
process.env.NODE_PATH = "."; // Enables require() calls relative to the cwd :)
return process.env;
})(),
options: (function(){
var args = _.clone(argv);
restricted_props.forEach(function(prop){
delete args[prop];
});
var opts = [];
_.forIn(args, function(value, key){
opts.push("--" + key +"=" +value);
});
return opts;
})()
});
child.on('restart', function() {
console.error(script + ' is restarting. This script has now been executed: ' + child.times);
});
child.on('exit', function () {
var restarts = getRestarts();
if(restarts){
console.log(script + ' has exited after restarting the server %d additional times', getRestarts());
}else {
console.log(script + ' has exited');
}
});
child.start();