From f4558676a6c80d8e212fa18075520438ce04a2c1 Mon Sep 17 00:00:00 2001 From: Felix Date: Tue, 16 Aug 2016 22:46:33 +0200 Subject: [PATCH] Update - Handle db errors and try to reconnect instead of crash --- package.json | 2 +- src/db/mongo.js | 6 ++++++ src/db/mysql.js | 5 +++++ src/setup.js | 5 ----- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index f1b24d5..8892bd9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "POGOServer", - "version": "0.2.5", + "version": "0.2.6", "description": "", "repository": { "type": "git", diff --git a/src/db/mongo.js b/src/db/mongo.js index d3d023a..e2c6814 100644 --- a/src/db/mongo.js +++ b/src/db/mongo.js @@ -11,12 +11,18 @@ export function setupConnection() { if (error) { this.print(error, 31); this.retry("Retrying again in ", () => this.setupConnection().then(resolve), 5); + return void 0; } else { this.db.instance = db; this.loadCollection(CFG.SERVER_MONGO_COLLECTION_USERS).then(() => { + this.print(`\x1b[36;1mMongoDB\x1b[0m\x1b[32;1m connection established\x1b[0m`); resolve(); }); } + db.on("close", (error) => { + this.print(error, 31); + this.retry("Trying to reconnect in ", () => this.setupConnection().then(resolve), 5); + }); }); }); diff --git a/src/db/mysql.js b/src/db/mysql.js index 3e192f0..898f4e6 100644 --- a/src/db/mysql.js +++ b/src/db/mysql.js @@ -21,9 +21,14 @@ export function setupConnection() { } this.db.instance = connection; this.createTableIfNoExists().then(() => { + this.print(`\x1b[36;1mMySQL\x1b[0m\x1b[32;1m connection established\x1b[0m`); resolve(); }); }); + connection.on("error", (error) => { + this.print(error, 31); + this.retry("Trying to reconnect in ", () => this.setupConnection().then(resolve), 5); + }); }); } diff --git a/src/setup.js b/src/setup.js index 173b4a0..0b4bd67 100644 --- a/src/setup.js +++ b/src/setup.js @@ -9,11 +9,6 @@ export function setup() { this.createAssetDownloadSession().then(() => { this.setupDatabaseConnection().then(() => { - let dbType = String(CFG.SERVER_USE_DATABASE).toLowerCase(); - let name = dbType === "mongo" ? "MongoDB" : "MySQL"; - - this.print(`\x1b[36;1m${name}\x1b[0m\x1b[${CFG.SERVER_DEFAULT_CONSOLE_COLOR};1m connection established\x1b[0m`); - if (CFG.SERVER_PORT < 1) { this.print("Invalid port!", 31); return void 0;