forked from tvrbo-pro/TvrboReact
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
123 lines (106 loc) · 2.74 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
"use strict";
// Override .babelrc
require("babel-register")({
presets: ["es2017-node7", "react", "stage-1"],
plugins: ["transform-decorators-legacy"]
});
const config = require("./app/config/server");
const throttle = require("lodash.throttle");
const mongoose = require("mongoose");
mongoose.Promise = require("bluebird");
const app = require("./app/server.jsx");
const { initSocket } = require("./app/socket");
const { initIntervals } = require("./app/lib/intervals");
const { log, logError } = require("./app/lib/util");
// MAIN ROUTINE
function startServer() {
initTerminationHandlers();
startDatabase()
.then(() => {
// Start the web server
const server = app.listen(config.HTTP_PORT, function() {
log(config.APP_NAME + " listening on port " + config.HTTP_PORT);
});
// Start the web socket server
initSocket(server);
// Start the interval tasks
initIntervals();
})
.catch(err => {
logError("The server could not be started");
logError(err);
mongoose.disconnect();
process.exit(1);
});
}
// MONGODB START
function startDatabase() {
if (!config.MONGODB_URI) return Promise.resolve();
// MongoDB Event Handlers
mongoose.connection.on("connecting", onDbConnecting);
mongoose.connection.on("error", onDbConnectionError);
mongoose.connection.on("connected", onDbConnected);
mongoose.connection.once("open", onDbConnectionOpen);
mongoose.connection.on("reconnected", onDbReconnected);
mongoose.connection.on(
"disconnected",
throttle(onDbDisconnected, 1000, { leading: true })
);
// Connect
return mongoose.connect(config.MONGODB_URI);
}
function onDbConnecting() {
log("Connecting to MongoDB...");
}
function onDbConnectionError(error) {
logError("Error in MongoDB connection: " + error);
mongoose.disconnect();
}
function onDbConnected() {
log("MongoDB connected");
}
function onDbConnectionOpen() {
log("MongoDB connection opened", "\n");
}
function onDbReconnected() {
log("MongoDB reconnected", "\n");
}
function onDbDisconnected() {
log("MongoDB disconnected!", "\n");
mongoose.connect(config.MONGODB_URI);
}
// TERMINATION HANDLERS
function initTerminationHandlers() {
process.on("exit", function() {
terminator();
});
var signals = [
"SIGHUP",
"SIGINT",
"SIGQUIT",
"SIGILL",
"SIGTRAP",
"SIGABRT",
"SIGBUS",
"SIGFPE",
"SIGUSR1",
"SIGSEGV",
"SIGTERM" //, 'SIGUSR2'
];
// Removed 'SIGPIPE' from the list - bugz 852598.
signals.forEach(function(element) {
process.on(element, function() {
terminator(element);
});
});
}
// TERMINATION CALLBACK
function terminator(signal) {
if (!signal || typeof signal != "string")
return log("The app is terminating...");
mongoose.disconnect(); // graceful shutdown
log(`Received ${signal}...`);
process.exit(0);
}
// INIT
startServer();