forked from Spacebrew/spacebrew
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_server_forever.js
executable file
·143 lines (126 loc) · 3.78 KB
/
node_server_forever.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
#!/usr/bin/env node
/**
* Spacebrew Server with Forever
* ------------------------------
*
* This script runs the Spacebrew server, and optionally the spacebrew live
* persistent router, in forever mode. This means that the server is automatically
* relaunched if it crashes. All standard node_server.js options are supported.
* To find out about the comand line flags just run the script with the '-h' or
* '--help' flag.
*
* Latest Updates:
* - checks if data/log folder already exists, and if not, it creates folder
*
* @author: Quin Kennedy, Julio Terra, and other contributors
* @filename: node_server.js
* @date: June 1st, 2013
* @updated with version: 0.3.1
*
*/
//DANGEROUS DEBUG ONLY
//from https://stackoverflow.com/questions/20433287/node-js-request-cert-has-expired
// process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
var forever = require('forever-monitor')
, fs = require('fs')
, logger = require('./logger')
, argv = process.argv.splice(2, process.argv.length)
, restarts = 0
, date = Date.parse(new Date)
, help = false
, data_dir = __dirname + "/data"
, log_dir = __dirname + "/data/log"
;
/**
* check if data/log directory already exists, and if not, then create it.
*/
var setupLogDirectory = function() {
// check if data folder exists
try {
fs.statSync(data_dir);
}
catch (e) {
fs.mkdir(data_dir, err => { if (err) console.log(err) });
logger.log("info", "creating data directory");
}
// check if data/log folder exists
try {
fs.statSync(log_dir);
}
catch (e) {
fs.mkdir(__dirname + "/data/log", err => { if (err) console.log(err) });
logger.log("info", "creating data/log directory");
}
}
/**
* Parses CLI arguments to confirm if there are any commands that need to occur before
* the app is launched in forever mode
*/
var processArguments = function(){
for(var i = 0; i < argv.length; i++){
switch(argv[i]){
case "-l":
case "--log":
logger.debugLevel = "info";
break;
case "--secure":
logger.log('warn', 'running secure');
case "--loglevel":
logger.debugLevel = argv[(i += 1)];
break; case "-x":
case "--cleanstart":
try {
fs.unlinkSync('./data/routes/live/live_persist_config.json');
}
catch (e) {
logger.log('warn', "[processArguments] not able to delete /data/routes/live/live_persist_config.json")
}
break;
case "-h":
case "--help":
help = true;
break;
}
}
}
var createForeverServer = function() {
/**
* Forever server configurations for launching the spacebrew server in forever mode
* @type {forever.Monitor}
*/
var server = new (forever.Monitor)('node_server.js', {
'silent': false
, 'options': argv
, 'uid': 'spacebrew'
, 'pid': './data/'
, 'logFile': './data/log/spacebrew_forever_' + date + '.log'
, 'outFile': './data/log/spacebrew_info_' + date + '.log'
, 'errFile': './data/log/spacebrew_error_' + date + '.log'
});
/**
* Register event handler for application exit events
* @return {[type]} [description]
*/
server.on('exit', function () {
logger.log('info','[Exit] the spacebrew server will no longer be restarted');
});
/**
* Register event handler for spacebrew server restart events, due to app crashing
*/
server.on('restart', function () {
restarts += 1;
date = Date.parse(new Date);
// if script was run with help flag then stop after first restart
if (help) {
process.exit();
}
// otherwise, print restart count message
else {
logger.log('warn','[Restart] the spacebrew server has been restarted ' + restarts + ' time');
}
});
server.start();
}
setupLogDirectory();
processArguments();
createForeverServer();