|
@@ -18,23 +18,35 @@ const songs = require('./logic/songs');
|
|
const playlists = require('./logic/playlists');
|
|
const playlists = require('./logic/playlists');
|
|
const cache = require('./logic/cache');
|
|
const cache = require('./logic/cache');
|
|
const notifications = require('./logic/notifications');
|
|
const notifications = require('./logic/notifications');
|
|
|
|
+const punishments = require('./logic/punishments');
|
|
const logger = require('./logic/logger');
|
|
const logger = require('./logic/logger');
|
|
const tasks = require('./logic/tasks');
|
|
const tasks = require('./logic/tasks');
|
|
const config = require('config');
|
|
const config = require('config');
|
|
|
|
|
|
let currentComponent;
|
|
let currentComponent;
|
|
|
|
+let initializedComponents = [];
|
|
|
|
+let lockdownB = false;
|
|
|
|
|
|
process.on('uncaughtException', err => {
|
|
process.on('uncaughtException', err => {
|
|
- //console.log(`ERROR: ${err.message}`);
|
|
|
|
- console.log(`ERROR: ${err.stack}`);
|
|
|
|
|
|
+ if (lockdownB || err.code === 'ECONNREFUSED' || err.code === 'UNCERTAIN_STATE') return;
|
|
|
|
+ console.log(`UNCAUGHT EXCEPTION: ${err.stack}`);
|
|
});
|
|
});
|
|
|
|
|
|
|
|
+const getError = (err) => {
|
|
|
|
+ let error = 'An error occurred.';
|
|
|
|
+ if (typeof err === "string") error = err;
|
|
|
|
+ else if (err.message) {
|
|
|
|
+ if (err.message !== 'Validation failed') error = err.message;
|
|
|
|
+ else error = err.errors[Object.keys(err.errors)].message;
|
|
|
|
+ }
|
|
|
|
+ return error;
|
|
|
|
+};
|
|
|
|
+
|
|
client.on('ready', () => {
|
|
client.on('ready', () => {
|
|
discordClientCBS.forEach((cb) => {
|
|
discordClientCBS.forEach((cb) => {
|
|
cb();
|
|
cb();
|
|
});
|
|
});
|
|
- console.log(`DISCORD Logged in as ${client.user.username}!`);
|
|
|
|
- //this.logToDiscord("Lost connection to MongoDB. Crashing server.", "Database error", true, [{name: 'Error', value: 'MONGOERR: ERRCNNCT 127.0.0.1 FAILED TO CONNECT', inline: false}], (err) => {console.log(err);});
|
|
|
|
|
|
+ console.log(`Logged in to Discord as ${client.user.username}#${client.user.discriminator}`);
|
|
});
|
|
});
|
|
|
|
|
|
client.login(config.get('apis.discord.token'));
|
|
client.login(config.get('apis.discord.token'));
|
|
@@ -62,92 +74,126 @@ const logToDiscord = (message, color, type, critical, extraFields, cb = ()=>{})
|
|
extraFields.forEach((extraField) => {
|
|
extraFields.forEach((extraField) => {
|
|
richEmbed.addField(extraField.name, extraField.value, extraField.inline);
|
|
richEmbed.addField(extraField.name, extraField.value, extraField.inline);
|
|
});
|
|
});
|
|
- /*client.channels.get(config.get('apis.discord.loggingChannel')).sendEmbed(richEmbed).then(() => {
|
|
|
|
|
|
+ client.channels.get(config.get('apis.discord.loggingChannel')).sendEmbed(richEmbed).then(() => {
|
|
cb();
|
|
cb();
|
|
}).then((reason) => {
|
|
}).then((reason) => {
|
|
cb(reason);
|
|
cb(reason);
|
|
- });*/
|
|
|
|
|
|
+ });
|
|
});
|
|
});
|
|
};
|
|
};
|
|
|
|
|
|
|
|
+function lockdown() {
|
|
|
|
+ if (lockdownB) return;
|
|
|
|
+ lockdownB = true;
|
|
|
|
+ initializedComponents.forEach((component) => {
|
|
|
|
+ component._lockdown();
|
|
|
|
+ });
|
|
|
|
+ console.log("Backend locked down.");
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+function errorCb(message, err, component) {
|
|
|
|
+ err = getError(err);
|
|
|
|
+ lockdown();
|
|
|
|
+ logToDiscord(message, "#FF0000", message, true, [{name: "Error:", value: err, inline: false}, {name: "Component:", value: component, inline: true}]);
|
|
|
|
+}
|
|
|
|
+
|
|
async.waterfall([
|
|
async.waterfall([
|
|
|
|
|
|
// setup our Redis cache
|
|
// setup our Redis cache
|
|
(next) => {
|
|
(next) => {
|
|
currentComponent = 'Cache';
|
|
currentComponent = 'Cache';
|
|
- cache.init(config.get('redis').url, config.get('redis').password, () => {
|
|
|
|
|
|
+ cache.init(config.get('redis').url, config.get('redis').password, errorCb, () => {
|
|
next();
|
|
next();
|
|
});
|
|
});
|
|
},
|
|
},
|
|
|
|
|
|
// setup our MongoDB database
|
|
// setup our MongoDB database
|
|
(next) => {
|
|
(next) => {
|
|
|
|
+ initializedComponents.push(cache);
|
|
currentComponent = 'DB';
|
|
currentComponent = 'DB';
|
|
- db.init(config.get("mongo").url, next);
|
|
|
|
|
|
+ db.init(config.get("mongo").url, errorCb, next);
|
|
},
|
|
},
|
|
|
|
|
|
// setup the express server
|
|
// setup the express server
|
|
(next) => {
|
|
(next) => {
|
|
|
|
+ initializedComponents.push(db);
|
|
currentComponent = 'App';
|
|
currentComponent = 'App';
|
|
app.init(next);
|
|
app.init(next);
|
|
},
|
|
},
|
|
|
|
|
|
// setup the mail
|
|
// setup the mail
|
|
(next) => {
|
|
(next) => {
|
|
|
|
+ initializedComponents.push(app);
|
|
currentComponent = 'Mail';
|
|
currentComponent = 'Mail';
|
|
mail.init(next);
|
|
mail.init(next);
|
|
},
|
|
},
|
|
|
|
|
|
// setup the socket.io server (all client / server communication is done over this)
|
|
// setup the socket.io server (all client / server communication is done over this)
|
|
(next) => {
|
|
(next) => {
|
|
|
|
+ initializedComponents.push(mail);
|
|
currentComponent = 'IO';
|
|
currentComponent = 'IO';
|
|
io.init(next);
|
|
io.init(next);
|
|
},
|
|
},
|
|
|
|
|
|
|
|
+ // setup the punishment system
|
|
|
|
+ (next) => {
|
|
|
|
+ initializedComponents.push(io);
|
|
|
|
+ currentComponent = 'Punishments';
|
|
|
|
+ punishments.init(next);
|
|
|
|
+ },
|
|
|
|
+
|
|
// setup the notifications
|
|
// setup the notifications
|
|
(next) => {
|
|
(next) => {
|
|
|
|
+ initializedComponents.push(punishments);
|
|
currentComponent = 'Notifications';
|
|
currentComponent = 'Notifications';
|
|
- notifications.init(config.get('redis').url, config.get('redis').password, next);
|
|
|
|
|
|
+ notifications.init(config.get('redis').url, config.get('redis').password, errorCb, next);
|
|
},
|
|
},
|
|
|
|
|
|
// setup the stations
|
|
// setup the stations
|
|
(next) => {
|
|
(next) => {
|
|
|
|
+ initializedComponents.push(notifications);
|
|
currentComponent = 'Stations';
|
|
currentComponent = 'Stations';
|
|
stations.init(next)
|
|
stations.init(next)
|
|
},
|
|
},
|
|
|
|
|
|
// setup the songs
|
|
// setup the songs
|
|
(next) => {
|
|
(next) => {
|
|
|
|
+ initializedComponents.push(stations);
|
|
currentComponent = 'Songs';
|
|
currentComponent = 'Songs';
|
|
songs.init(next)
|
|
songs.init(next)
|
|
},
|
|
},
|
|
|
|
|
|
// setup the playlists
|
|
// setup the playlists
|
|
(next) => {
|
|
(next) => {
|
|
|
|
+ initializedComponents.push(songs);
|
|
currentComponent = 'Playlists';
|
|
currentComponent = 'Playlists';
|
|
playlists.init(next)
|
|
playlists.init(next)
|
|
},
|
|
},
|
|
|
|
|
|
// setup the API
|
|
// setup the API
|
|
(next) => {
|
|
(next) => {
|
|
|
|
+ initializedComponents.push(playlists);
|
|
currentComponent = 'API';
|
|
currentComponent = 'API';
|
|
api.init(next)
|
|
api.init(next)
|
|
},
|
|
},
|
|
|
|
|
|
// setup the logger
|
|
// setup the logger
|
|
(next) => {
|
|
(next) => {
|
|
|
|
+ initializedComponents.push(api);
|
|
currentComponent = 'Logger';
|
|
currentComponent = 'Logger';
|
|
logger.init(next)
|
|
logger.init(next)
|
|
},
|
|
},
|
|
|
|
|
|
// setup the tasks system
|
|
// setup the tasks system
|
|
(next) => {
|
|
(next) => {
|
|
|
|
+ initializedComponents.push(logger);
|
|
currentComponent = 'Tasks';
|
|
currentComponent = 'Tasks';
|
|
tasks.init(next)
|
|
tasks.init(next)
|
|
},
|
|
},
|
|
|
|
|
|
// setup the frontend for local setups
|
|
// setup the frontend for local setups
|
|
(next) => {
|
|
(next) => {
|
|
|
|
+ initializedComponents.push(tasks);
|
|
currentComponent = 'Windows';
|
|
currentComponent = 'Windows';
|
|
if (!config.get("isDocker")) {
|
|
if (!config.get("isDocker")) {
|
|
const express = require('express');
|
|
const express = require('express');
|
|
@@ -166,17 +212,16 @@ async.waterfall([
|
|
});
|
|
});
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
+ if (lockdownB) return;
|
|
next();
|
|
next();
|
|
}
|
|
}
|
|
], (err) => {
|
|
], (err) => {
|
|
if (err && err !== true) {
|
|
if (err && err !== true) {
|
|
- logToDiscord("An error occurred while initializing the backend server.", "#FF0000", "Startup error", true, [{name: "Error:", value: err, inline: false}, {name: "Component:", value: currentComponent, inline: true}])
|
|
|
|
|
|
+ lockdown();
|
|
|
|
+ logToDiscord("An error occurred while initializing the backend server.", "#FF0000", "Startup error", true, [{name: "Error:", value: err, inline: false}, {name: "Component:", value: currentComponent, inline: true}]);
|
|
console.error('An error occurred while initializing the backend server');
|
|
console.error('An error occurred while initializing the backend server');
|
|
- console.error(err);
|
|
|
|
-
|
|
|
|
- process.exit();
|
|
|
|
} else {
|
|
} else {
|
|
- logToDiscord("The backend server started successfully.", "#00AA00", "Startup", false);
|
|
|
|
|
|
+ logToDiscord("The backend server started successfully.", "#00AA00", "Startup", false, []);
|
|
console.info('Backend server has been successfully started');
|
|
console.info('Backend server has been successfully started');
|
|
}
|
|
}
|
|
});
|
|
});
|