-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
151 lines (123 loc) · 3.92 KB
/
index.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
'use strict';
// Override .babelrc
require("babel-register")({
presets: ["node6", "react", "stage-1"],
plugins: ["transform-decorators-legacy"]
});
var config = require('config.js');
var mongoose = require('mongoose');
var Promise = require('bluebird');
mongoose.Promise = Promise;
var throttle = require('lodash.throttle');
var receivedSigInt = false;
// MAIN ROUTINE
function startServer(){
var server = require('server.jsx');
return Promise.try(() => {
initTerminationHandlers();
return startDatabase();
})
.then(() => server.listen(config.HTTP_PORT, function() {
console.log((new Date()).toJSON() + " | " + config.APP_NAME + " listening on port " + config.HTTP_PORT)
})
);
}
// MONGODB START
function startDatabase(){
if(!config.MONGODB_URI) return Promise.resolve();
// Check that the server is listening
return new Promise(function(resolve, reject){
var net = require('net');
var socket = new net.Socket();
var mongoUri = require('url').parse(config.MONGODB_URI);
var timeout = 4000;
socket.setTimeout(timeout, function() {
socket.destroy();
reject();
});
socket.connect(mongoUri.port, mongoUri.hostname, function() {
// THE PORT IS LISTENING
socket.destroy();
resolve();
});
socket.on('data', function() {
socket.destroy();
resolve();
});
socket.on('error', function(e) {
console.error("-----");
console.error("ERROR:\nThe MongoDB Server is not available");
console.error(e.message || e);
console.error("-----");
reject();
socket.destroy();
process.exit(1);
});
})
.then(function(){
// MongoDB Event Handlers
mongoose.connection.once('open', onDbConnectionOpen);
mongoose.connection.on('connecting', onDbConnecting);
mongoose.connection.on('error', onDbConnectionError);
mongoose.connection.on('connected', onDbConnected);
mongoose.connection.on('reconnected', onDbReconnected);
mongoose.connection.on('disconnected', throttle(onDbDisconnected, 1000, {leading: true}));
// Connect
return mongoose.connect(config.MONGODB_URI); // , {server: {auto_reconnect:true}}
});
}
// DB EVENT HANDLERS
function onDbConnecting() {
console.log('%s | Connecting to the Database...', (new Date()).toJSON());
}
function onDbConnectionOpen() {
console.log('%s | MongoDB connection opened', (new Date()).toJSON(), "\n");
}
function onDbConnectionError(error) {
console.error('%s | Error on Database connection: ' + error, (new Date()).toJSON());
mongoose.disconnect();
}
function onDbConnected() {
console.log('%s | MongoDB connected', (new Date()).toJSON());
}
function onDbReconnected() {
console.log('%s | MongoDB reconnected', (new Date()).toJSON(), "\n");
}
function onDbDisconnected() {
console.error('%s | MongoDB disconnected', (new Date()).toJSON(), "\n");
if(receivedSigInt) return;
else mongoose.connect(config.MONGODB_URI); // , {server: {auto_reconnect:true}}
}
// SIGNAL HANDLERS
function initTerminationHandlers(){
process.on('exit', terminator);
var signals = [
'SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP', 'SIGABRT',
'SIGBUS', 'SIGFPE', 'SIGUSR1', 'SIGSEGV', 'SIGTERM' //, 'SIGUSR2'
// Removed 'SIGPIPE' from the list - bugz 852598.
];
signals.forEach(signal => {
switch(signal){
case 'SIGINT':
process.on(signal, () => {
receivedSigInt = true;
mongoose.disconnect().then(() => terminator(signal));
})
break;
default: process.on(signal, () => terminator(signal) );
}
});
}
// TERMINATION HANDLER
function terminator(signal){
if(!signal || typeof signal != "string")
return console.log('%s | The app is terminating...', (new Date()).toJSON());
mongoose.disconnect();
console.error('%s | Received %s...', (new Date()).toJSON(), signal);
process.exit(1);
}
// SERVER INIT
startServer()
.catch(err => {
console.error("ERROR STARTING THE SERVER:", err);
})