Forráskód Böngészése

Added special log to identify issues with stations.

KrisVos130 8 éve
szülő
commit
f423274c91
3 módosított fájl, 34 hozzáadás és 9 törlés
  1. 23 3
      backend/logic/logger.js
  2. 7 2
      backend/logic/notifications.js
  3. 4 4
      backend/logic/stations.js

+ 23 - 3
backend/logic/logger.js

@@ -70,18 +70,23 @@ let twoDigits = (num) => {
 	return (num < 10) ? '0' + num : num;
 };
 
-let getTime = (cb) => {
+let getTime = () => {
 	let time = new Date();
-	return cb ({
+	return {
 		year: time.getFullYear(),
 		month: time.getMonth() + 1,
 		day: time.getDate(),
 		hour: time.getHours(),
 		minute: time.getMinutes(),
 		second: time.getSeconds()
-	});
+	}
 };
 
+let getTimeFormatted = () => {
+	let time = getTime();
+	return `${time.year}-${twoDigits(time.month)}-${twoDigits(time.day)} ${twoDigits(time.hour)}:${twoDigits(time.minute)}:${twoDigits(time.second)}`;
+}
+
 module.exports = {
 	init: function(cb) {
 		utils = require('./utils');
@@ -91,6 +96,13 @@ module.exports = {
 		setTimeout(calculateHourUnits, 1000 * 60 * 60);
 		setTimeout(this.calculate, 1000 * 30);
 
+		let time = getTimeFormatted();
+		fs.appendFile(dir + '/all.log', `${time} BACKEND_RESTARTED\n`, ()=>{});
+		fs.appendFile(dir + '/success.log', `${time} BACKEND_RESTARTED\n`, ()=>{});
+		fs.appendFile(dir + '/error.log', `${time} BACKEND_RESTARTED\n`, ()=>{});
+		fs.appendFile(dir + '/info.log', `${time} BACKEND_RESTARTED\n`, ()=>{});
+		fs.appendFile(dir + '/debugStation.log', `${time} BACKEND_RESTARTED\n`, ()=>{});
+
 		cb();
 	},
 	success: (type, message) => {
@@ -127,6 +139,14 @@ module.exports = {
 			console.info('\x1b[36m', timeString, 'INFO', '-', type, '-', message, '\x1b[0m');
 		});
 	},
+	stationIssue: (string) => {
+		getTime((time) => {
+			let timeString = `${time.year}-${twoDigits(time.month)}-${twoDigits(time.day)} ${twoDigits(time.hour)}:${twoDigits(time.minute)}:${twoDigits(time.second)}`;
+			fs.appendFile(dir + '/debugStation.log', `${timeString} - ${string}\n`, ()=>{});
+
+			console.info('\x1b[35m', timeString, '-', string, '\x1b[0m');
+		});
+	},
 	calculatePerSecond: function(number) {
 		let secondsRunning = Math.floor((Date.now() - started) / 1000);
 		let perSecond = number / secondsRunning;

+ 7 - 2
backend/logic/notifications.js

@@ -2,6 +2,7 @@
 
 const crypto = require('crypto');
 const redis = require('redis');
+const logger = require('./logger');
 
 let pub = null;
 let sub = null;
@@ -24,6 +25,7 @@ const lib = {
 			process.exit();
 		});
 		sub.on('pmessage', (pattern, channel, expiredKey) => {
+			logger.stationIssue(`PMESSAGE - Pattern: ${pattern}; Channel: ${channel}; ExpiredKey: ${expiredKey}`);
 			subscriptions.forEach((sub) => {
 				if (sub.name !== expiredKey) return;
 				sub.cb();
@@ -42,8 +44,9 @@ const lib = {
 	 * @param {Integer} time - how long in milliseconds until the notification should be fired
 	 * @param {Function} cb - gets called when the notification has been scheduled
 	 */
-	schedule: (name, time, cb) => {
+	schedule: (name, time, cb, station) => {
 		time = Math.round(time);
+		logger.stationIssue(`SCHEDULE - Time: ${time}; Name: ${name}; Key: ${crypto.createHash('md5').update(`_notification:${name}_`).digest('hex')}; StationId: ${station._id}; StationName: ${station.name}`);
 		pub.set(crypto.createHash('md5').update(`_notification:${name}_`).digest('hex'), '', 'PX', time, 'NX', cb);
 	},
 
@@ -55,8 +58,9 @@ const lib = {
 	 * @param {Boolean} unique - only subscribe if another subscription with the same name doesn't already exist
 	 * @return {Object} - the subscription object
 	 */
-	subscribe: (name, cb, unique = false) => {
+	subscribe: (name, cb, unique = false, station) => {
 		if (unique && subscriptions.find((subscription) => subscription.originalName == name)) return;
+		logger.stationIssue(`SUBSCRIBE - Name: ${name}; Key: ${crypto.createHash('md5').update(`_notification:${name}_`).digest('hex')}, StationId: ${station._id}; StationName: ${station.name}`);
 		let subscription = { originalName: name, name: crypto.createHash('md5').update(`_notification:${name}_`).digest('hex'), cb };
 		subscriptions.push(subscription);
 		return subscription;
@@ -73,6 +77,7 @@ const lib = {
 	},
 
 	unschedule: (name) => {
+		logger.stationIssue(`UNSCHEDULE - Name: ${name}; Key: ${crypto.createHash('md5').update(`_notification:${name}_`).digest('hex')}`);
 		pub.del(crypto.createHash('md5').update(`_notification:${name}_`).digest('hex'));
 	},
 };

+ 4 - 4
backend/logic/stations.js

@@ -91,9 +91,9 @@ module.exports = {
 			},
 			(station, next) => {
 				if (!station) return next('Station not found.');
-				notifications.subscribe(`stations.nextSong?id=${station._id}`, _this.skipStation(station._id), true);
+				notifications.subscribe(`stations.nextSong?id=${station._id}`, _this.skipStation(station._id), true, station);
 				if (station.paused) {
-					notifications.unschedule(`stations.nextSong?id${station._id}`);
+					notifications.unschedule(`stations.nextSong?id=${station._id}`);
 					return next(true, station);
 				}
 				next(null, station);
@@ -112,7 +112,7 @@ module.exports = {
 						next(err, station);
 					});
 				} else {
-					notifications.schedule(`stations.nextSong?id=${station._id}`, timeLeft);
+					notifications.schedule(`stations.nextSong?id=${station._id}`, timeLeft, null, station);
 					next(null, station);
 				}
 			}
@@ -444,7 +444,7 @@ module.exports = {
 					if (station.currentSong !== null && station.currentSong.songId !== undefined) {
 						utils.socketsJoinSongRoom(utils.getRoomSockets(`station.${station._id}`), `song.${station.currentSong.songId}`);
 						if (!station.paused) {
-							notifications.schedule(`stations.nextSong?id=${station._id}`, station.currentSong.duration * 1000);
+							notifications.schedule(`stations.nextSong?id=${station._id}`, station.currentSong.duration * 1000, null, station);
 						}
 					} else {
 						utils.socketsLeaveSongRooms(utils.getRoomSockets(`station.${station._id}`));