Browse Source

Added the task session cleaning.

KrisVos130 8 years ago
parent
commit
c1b7706c86
4 changed files with 90 additions and 8 deletions
  1. 1 0
      backend/logic/cache/schemas/session.js
  2. 21 6
      backend/logic/io.js
  3. 55 2
      backend/logic/tasks.js
  4. 13 0
      backend/logic/utils.js

+ 1 - 0
backend/logic/cache/schemas/session.js

@@ -4,6 +4,7 @@ module.exports = (sessionId, userId) => {
 	return {
 	return {
 		sessionId: sessionId,
 		sessionId: sessionId,
 		userId: userId,
 		userId: userId,
+		refreshDate: Date.now(),
 		created: Date.now()
 		created: Date.now()
 	};
 	};
 };
 };

+ 21 - 6
backend/logic/io.js

@@ -4,6 +4,7 @@
 
 
 const app = require('./app');
 const app = require('./app');
 const actions = require('./actions');
 const actions = require('./actions');
+const async = require('async');
 const cache = require('./cache');
 const cache = require('./cache');
 const utils = require('./utils');
 const utils = require('./utils');
 const db = require('./db');
 const db = require('./db');
@@ -20,12 +21,26 @@ module.exports = {
 			let cookies = socket.request.headers.cookie;
 			let cookies = socket.request.headers.cookie;
 			let SID = utils.cookies.parseCookies(cookies).SID;
 			let SID = utils.cookies.parseCookies(cookies).SID;
 
 
-			if (!SID) SID = "NONE";
-			cache.hget('sessions', SID, (err, session) => {
-				if (err) SID = null;
-				socket.session = (session) ? session : {};
-				socket.session.socketId = socket.id;
-				return next();
+			async.waterfall([
+				(next) => {
+					if (!SID) return next('No SID.');
+					next();
+				},
+				(next) => {
+					cache.hget('sessions', SID, next);
+				},
+				(session, next) => {
+					if (!session) return next('No session found.');
+					session.refreshDate = Date.now();
+					socket.session = session;
+					cache.hset('sessions', SID, session, next);
+				}
+			], () => {
+				if (!socket.session) {
+					socket.session = {socketId: socket.id};
+				} else socket.session.socketId = socket.id;
+				console.log(socket.session);
+				next();
 			});
 			});
 		});
 		});
 
 

+ 55 - 2
backend/logic/tasks.js

@@ -1,6 +1,7 @@
 'use strict';
 'use strict';
 
 
 const cache = require("./cache");
 const cache = require("./cache");
+const logger = require("./logger");
 const Stations = require("./stations");
 const Stations = require("./stations");
 const async = require("async");
 const async = require("async");
 let utils;
 let utils;
@@ -16,7 +17,7 @@ let testTask = (callback) => {
 };
 };
 
 
 let checkStationSkipTask = (callback) => {
 let checkStationSkipTask = (callback) => {
-	console.log(`Checking for stations`);
+	logger.info("TASK_STATIONS_SKIP_CHECK", `Checking for stations to be skipped.`);
 	async.waterfall([
 	async.waterfall([
 		(next) => {
 		(next) => {
 			cache.hgetall('stations', next);
 			cache.hgetall('stations', next);
@@ -27,7 +28,7 @@ let checkStationSkipTask = (callback) => {
 				const timeElapsed = Date.now() - station.startedAt - station.timePaused;
 				const timeElapsed = Date.now() - station.startedAt - station.timePaused;
 				if (timeElapsed <= station.currentSong.duration) return next2();
 				if (timeElapsed <= station.currentSong.duration) return next2();
 				else {
 				else {
-					console.log(`Skipping ${station._id}`);
+					logger.error("TASK_STATIONS_SKIP_CHECK", `Skipping ${station._id} as it should have skipped already.`);
 					stations.skipStation(station._id);
 					stations.skipStation(station._id);
 					next2();
 					next2();
 				}
 				}
@@ -40,11 +41,63 @@ let checkStationSkipTask = (callback) => {
 	});
 	});
 };
 };
 
 
+let sessionClearingTask = (callback) => {
+	logger.info("TASK_SESSION_CLEAR", `Checking for sessions to be cleared.`);
+	async.waterfall([
+		(next) => {
+			cache.hgetall('sessions', next);
+		},
+		(sessions, next) => {
+			if (!sessions) return next();
+			let keys = Object.keys(sessions);
+			async.each(keys, (sessionId, next2) => {
+				let session = sessions[sessionId];
+				console.log(Date.now() - session.refreshDate);
+				if (session && session.refreshDate && (Date.now() - session.refreshDate) < (60 * 60 * 24 * 30 * 1000)) return next2();
+				console.log(2);
+				if (!session) {
+					logger.info("TASK_SESSION_CLEAR", 'Removing an empty session.');
+					cache.hdel('sessions', sessionId, () => {
+						next2();
+					});
+				} else if (!session.refreshDate) {
+					session.refreshDate = Date.now();
+					cache.hset('sessions', sessionId, session, () => {
+						next2();
+					});
+				} else if ((Date.now() - session.refreshDate) > (60 * 60 * 24 * 30 * 1000)) {
+					utils.socketsFromSessionId(session.sessionId, (sockets) => {
+						if (sockets.length > 0) {
+							session.refreshDate = Date.now();
+							cache.hset('sessions', sessionId, session, () => {
+								next2()
+							});
+						} else {
+							logger.info("TASK_SESSION_CLEAR", `Removing session ${sessionId} for user ${session.userId} since inactive for 30 days and not currently in use.`);
+							cache.hdel('sessions', session.sessionId, () => {
+								next2();
+							});
+						}
+					});
+				} else {
+					logger.error("TASK_SESSION_CLEAR", "This should never log.");
+					next2();
+				}
+			}, () => {
+				next();
+			});
+		}
+	], () => {
+		callback();
+	});
+};
+
 module.exports = {
 module.exports = {
 	init: function(cb) {
 	init: function(cb) {
 		utils = require('./utils');
 		utils = require('./utils');
 		this.createTask("testTask", testTask, 5000, true);
 		this.createTask("testTask", testTask, 5000, true);
 		this.createTask("stationSkipTask", checkStationSkipTask, 1000 * 60 * 30);
 		this.createTask("stationSkipTask", checkStationSkipTask, 1000 * 60 * 30);
+		this.createTask("sessionClearTask", sessionClearingTask, 1000 * 60 * 60 * 6);
 
 
 		cb();
 		cb();
 	},
 	},

+ 13 - 0
backend/logic/utils.js

@@ -142,6 +142,19 @@ module.exports = {
 			return ns.connected[socketId];
 			return ns.connected[socketId];
 		}
 		}
 	},
 	},
+	socketsFromSessionId: function(sessionId, cb) {
+		let ns = io.io.of("/");
+		let sockets = [];
+		if (ns) {
+			async.each(Object.keys(ns.connected), (id, next) => {
+				let session = ns.connected[id].session;
+				if (session.sessionId === sessionId) sockets.push(session.sessionId);
+				next();
+			}, () => {
+				cb(sockets);
+			});
+		}
+	},
 	socketsFromUser: function(userId, cb) {
 	socketsFromUser: function(userId, cb) {
 		let ns = io.io.of("/");
 		let ns = io.io.of("/");
 		let sockets = [];
 		let sockets = [];