Browse Source

Worked on fixing notifications.

KrisVos130 8 years ago
parent
commit
fdf8aceb4a
2 changed files with 21 additions and 22 deletions
  1. 9 6
      backend/logic/notifications.js
  2. 12 16
      backend/logic/stations.js

+ 9 - 6
backend/logic/notifications.js

@@ -3,7 +3,8 @@
 const crypto = require('crypto');
 const redis = require('redis');
 
-let client = null;
+let pub = null;
+let sub = null;
 
 const subscriptions = [];
 
@@ -16,15 +17,16 @@ const lib = {
 	 * @param {Function} cb - gets called once we're done initializing
 	 */
 	init: (url, cb) => {
-		client = redis.createClient({ url: url });
-		client.on('error', (err) => console.error);
-		client.on('message', (pattern, channel, expiredKey) => {
+		pub = redis.createClient({ url: url });
+		sub = redis.createClient({ url: url });
+		sub.on('error', (err) => console.error);
+		sub.on('pmessage', (pattern, channel, expiredKey) => {
 			subscriptions.forEach((sub) => {
 				if (sub.name !== expiredKey) return;
 				sub.cb();
 			});
 		});
-		client.psubscribe('__keyevent@0__:expired');
+		sub.psubscribe('__keyevent@0__:expired');
 		cb();
 	},
 
@@ -38,7 +40,8 @@ const lib = {
 	 * @param {Function} cb - gets called when the notification has been scheduled
 	 */
 	schedule: (name, time, cb) => {
-		client.set(crypto.createHash('md5').update(`_notification:${name}_`).digest('hex'), '', 'PX', 'NX', time, cb);
+		console.log(time);
+		pub.set(crypto.createHash('md5').update(`_notification:${name}_`).digest('hex'), '', 'PX', time, 'NX', cb);
 	},
 
 	/**

+ 12 - 16
backend/logic/stations.js

@@ -11,8 +11,7 @@ let skipTimeout = null;
 
 //TEMP
 cache.sub('station.pause', (stationId) => {
-	clearTimeout(skipTimeout);
-	skipTimeout = null;
+	notifications.remove(`stations.nextSong?id=${stationId}`);
 });
 
 cache.sub('station.resume', (stationId) => {
@@ -99,8 +98,9 @@ module.exports = {
 			if (err && err !== true) return cb(err);
 
 			// get notified when the next song for this station should play, so that we can notify our sockets
-			/*let notification = notifications.subscribe(`stations.nextSong?id=${station._id}`, () => {*/
-			function skipSongTemp() {
+			let notification = notifications.subscribe(`stations.nextSong?id=${station._id}`, () => {
+				console.log("NOTIFICATION!!!");
+			//function skipSongTemp() {
 				// get the station from the cache
 				//TODO Recalculate songs if the last song of the station playlist is getting played
 				cache.hget('stations', station._id, (err, station) => {
@@ -219,7 +219,7 @@ module.exports = {
 							utils.socketsJoinSongRoom(io.io.to(`station.${stationId}`).sockets, `song.${station.currentSong._id}`);
 							// schedule a notification to be dispatched when the next song ends
 							notifications.schedule(`stations.nextSong?id=${station.id}`, station.currentSong.duration * 1000);
-							skipTimeout = setTimeout(skipSongTemp, station.currentSong.duration * 1000);
+							//skipTimeout = setTimeout(skipSongTemp, station.currentSong.duration * 1000);
 						});
 					}
 					// the station doesn't exist anymore, unsubscribe from it
@@ -227,7 +227,7 @@ module.exports = {
 						notifications.remove(notification);
 					}
 				});
-			}//, true);
+			}, true);
 
 			if (!station.paused) {
 				if (!station.startedAt) {
@@ -236,17 +236,13 @@ module.exports = {
 					cache.hset('stations', stationId, station);
 				}
 				let timeLeft = ((station.currentSong.duration * 1000) - (Date.now() - station.startedAt - station.timePaused));
-				console.log(timeLeft, 1234);
-				console.log((station.currentSong.duration * 1000), Date.now(), station.startedAt, station.timePaused);
-				//setTimeout(skipSongTemp, station.currentSong.duration * 1000);
-				if (skipTimeout === null) {
-					skipTimeout = setTimeout(skipSongTemp, timeLeft);
+				console.log(timeLeft);
+				if (station.currentSong.duration * 1000 < timeLeft || timeLeft < 0) {
+					console.log("Test");
+					notifications.schedule(`stations.nextSong?id=${station.id}`, 1);
+				} else {
+					notifications.schedule(`stations.nextSong?id=${station.id}`, timeLeft);
 				}
-				if (station.currentSong.duration * 1000 < timeLeft) {
-					clearTimeout(skipTimeout);
-					skipSongTemp();
-				}
-				notifications.schedule(`stations.nextSong?id=${station.id}`, ((station.currentSong.duration * 1000) - (Date.now() - station.startedAt - station.timePaused)));
 			}
 
 			return cb(null, station);