Browse Source

Fixed small issue and added automatic exiting of the backend when Mongo/Redis connection failed.

KrisVos130 8 years ago
parent
commit
d1b006a68a
3 changed files with 16 additions and 4 deletions
  1. 8 2
      backend/logic/cache/index.js
  2. 4 1
      backend/logic/db/index.js
  3. 4 1
      backend/logic/notifications.js

+ 8 - 2
backend/logic/cache/index.js

@@ -29,7 +29,10 @@ const lib = {
 		lib.url = url;
 
 		lib.client = redis.createClient({ url: lib.url });
-		lib.client.on('error', (err) => console.error(err));
+		lib.client.on('error', (err) => {
+			console.error(err);
+			process.exit();
+		});
 
 		initialized = true;
 		callbacks.forEach((callback) => {
@@ -153,7 +156,10 @@ const lib = {
 		function func() {
 			if (subs[channel] === undefined) {
 				subs[channel] = { client: redis.createClient({ url: lib.url }), cbs: [] };
-				subs[channel].client.on('error', (err) => console.error(err));
+				subs[channel].client.on('error', (err) => {
+					console.error(err);
+					process.exit();
+				});
 				subs[channel].client.on('message', (channel, message) => {
 					if (parseJson) try { message = JSON.parse(message); } catch (e) {}
 					subs[channel].cbs.forEach((cb) => cb(message));

+ 4 - 1
backend/logic/db/index.js

@@ -12,7 +12,10 @@ let lib = {
 
 		lib.connection = mongoose.connect(url).connection;
 
-		lib.connection.on('error', err => console.error('Database error: ' + err.message));
+		lib.connection.on('error', err => {
+			console.error('Database error: ' + err.message)
+			process.exit();
+		});
 
 		lib.connection.once('open', _ => {
 

+ 4 - 1
backend/logic/notifications.js

@@ -19,7 +19,10 @@ const lib = {
 	init: (url, cb) => {
 		pub = redis.createClient({ url: url });
 		sub = redis.createClient({ url: url });
-		sub.on('error', (err) => console.error);
+		sub.on('error', (err) => {
+			console.error(err);
+			process.exit();
+		});
 		sub.on('pmessage', (pattern, channel, expiredKey) => {
 			subscriptions.forEach((sub) => {
 				if (sub.name !== expiredKey) return;