Jelajahi Sumber

Some backend fixes

Kristian Vos 4 tahun lalu
induk
melakukan
f4ac10794d
4 mengubah file dengan 125 tambahan dan 122 penghapusan
  1. 2 2
      backend/core.js
  2. 2 4
      backend/index.js
  3. 9 4
      backend/logic/actions/apis.js
  4. 112 112
      backend/logic/actions/stations.js

+ 2 - 2
backend/core.js

@@ -525,10 +525,10 @@ export default class CoreClass {
 				);
 				job.childJobs.forEach(childJob => {
 					if (childJob.responseType === "RESOLVE") {
-						childJob.onFinish.resolve(childJob.payload);
+						childJob.onFinish.resolve(childJob.response);
 						childJob.responseType = "RESOLVED";
 					} else if (childJob.responseType === "REJECT") {
-						childJob.onFinish.reject(childJob.payload);
+						childJob.onFinish.reject(childJob.response);
 						childJob.responseType = "REJECTED";
 					}
 				});

+ 2 - 4
backend/index.js

@@ -328,8 +328,7 @@ class ModuleManager {
 
 			this.log(
 				"INFO",
-				`Initialized: ${Object.keys(this.modules).length - this.modulesNotInitialized.length}/${
-					Object.keys(this.modules).length
+				`Initialized: ${Object.keys(this.modules).length - this.modulesNotInitialized.length}/${Object.keys(this.modules).length
 				}.`
 			);
 
@@ -427,8 +426,7 @@ process.stdin.on("data", data => {
 			console.log(
 				`${moduleName.toUpperCase()}${Array(tabsNeeded).join(
 					"\t"
-				)}${module.getStatus()}. Jobs in queue: ${module.jobQueue.lengthQueue()}. Jobs in progress: ${module.jobQueue.lengthRunning()}. Concurrency: ${
-					module.jobQueue.concurrency
+				)}${module.getStatus()}. Jobs in queue: ${module.jobQueue.lengthQueue()}. Jobs in progress: ${module.jobQueue.lengthRunning()}. Concurrency: ${module.jobQueue.concurrency
 				}. Stage: ${module.getStage()}`
 			);
 		});

+ 9 - 4
backend/logic/actions/apis.js

@@ -151,10 +151,15 @@ export default {
 	 */
 	joinRoom: (session, page, cb) => {
 		if (page === "home") {
-			utils.runJob("SOCKET_JOIN_ROOM", {
-				socketId: session.socketId,
-				room: page
-			});
+			utils
+				.runJob("SOCKET_JOIN_ROOM", {
+					socketId: session.socketId,
+					room: page
+				})
+				.then()
+				.catch(err => {
+					console.log("ERROR", `Joining room failed: ${err.message}`);
+				});
 		}
 		cb({});
 	},

+ 112 - 112
backend/logic/actions/stations.js

@@ -16,120 +16,120 @@ const { _ } = underscore;
 // const logger = moduleManager.modules["logger"];
 
 const userList = {};
-let usersPerStation = {};
-let usersPerStationCount = {};
+const usersPerStation = {};
+const usersPerStationCount = {};
 
 // Temporarily disabled until the messages in console can be limited
-setInterval(async () => {
-	const stationsCountUpdated = [];
-	const stationsUpdated = [];
-
-	const oldUsersPerStation = usersPerStation;
-	usersPerStation = {};
-
-	const oldUsersPerStationCount = usersPerStationCount;
-	usersPerStationCount = {};
-
-	const userModel = await db.runJob("GET_MODEL", {
-		modelName: "user"
-	});
-
-	async.each(
-		Object.keys(userList),
-		(socketId, next) => {
-			utils.runJob("SOCKET_FROM_SESSION", { socketId }, { isQuiet: true }).then(socket => {
-				const stationId = userList[socketId];
-				if (!socket || Object.keys(socket.rooms).indexOf(`station.${stationId}`) === -1) {
-					if (stationsCountUpdated.indexOf(stationId) === -1) stationsCountUpdated.push(stationId);
-					if (stationsUpdated.indexOf(stationId) === -1) stationsUpdated.push(stationId);
-					delete userList[socketId];
-					return next();
-				}
-				if (!usersPerStationCount[stationId]) usersPerStationCount[stationId] = 0;
-				usersPerStationCount[stationId] += 1;
-				if (!usersPerStation[stationId]) usersPerStation[stationId] = [];
-
-				return async.waterfall(
-					[
-						next => {
-							if (!socket.session || !socket.session.sessionId) return next("No session found.");
-							return cache
-								.runJob("HGET", {
-									table: "sessions",
-									key: socket.session.sessionId
-								})
-								.then(session => {
-									next(null, session);
-								})
-								.catch(next);
-						},
-
-						(session, next) => {
-							if (!session) return next("Session not found.");
-							return userModel.findOne({ _id: session.userId }, next);
-						},
-
-						(user, next) => {
-							if (!user) return next("User not found.");
-							if (usersPerStation[stationId].indexOf(user.username) !== -1)
-								return next("User already in the list.");
-							return next(null, user.username);
-						}
-					],
-					(err, username) => {
-						if (!err) {
-							usersPerStation[stationId].push(username);
-						}
-						next();
-					}
-				);
-			});
-			// TODO Code to show users
-		},
-		() => {
-			for (
-				let stationId = 0, stationKeys = Object.keys(usersPerStationCount);
-				stationId < stationKeys.length;
-				stationId += 1
-			) {
-				if (oldUsersPerStationCount[stationId] !== usersPerStationCount[stationId]) {
-					if (stationsCountUpdated.indexOf(stationId) === -1) stationsCountUpdated.push(stationId);
-				}
-			}
-
-			for (
-				let stationId = 0, stationKeys = Object.keys(usersPerStation);
-				stationId < stationKeys.length;
-				stationId += 1
-			) {
-				if (
-					_.difference(usersPerStation[stationId], oldUsersPerStation[stationId]).length > 0 ||
-					_.difference(oldUsersPerStation[stationId], usersPerStation[stationId]).length > 0
-				) {
-					if (stationsUpdated.indexOf(stationId) === -1) stationsUpdated.push(stationId);
-				}
-			}
-
-			stationsCountUpdated.forEach(stationId => {
-				console.log("INFO", "UPDATE_STATION_USER_COUNT", `Updating user count of ${stationId}.`);
-				cache.runJob("PUB", {
-					table: "station.updateUserCount",
-					value: stationId
-				});
-			});
-
-			stationsUpdated.forEach(stationId => {
-				console.log("INFO", "UPDATE_STATION_USER_LIST", `Updating user list of ${stationId}.`);
-				cache.runJob("PUB", {
-					table: "station.updateUsers",
-					value: stationId
-				});
-			});
-
-			// console.log("Userlist", usersPerStation);
-		}
-	);
-}, 3000);
+// setInterval(async () => {
+// 	const stationsCountUpdated = [];
+// 	const stationsUpdated = [];
+
+// 	const oldUsersPerStation = usersPerStation;
+// 	usersPerStation = {};
+
+// 	const oldUsersPerStationCount = usersPerStationCount;
+// 	usersPerStationCount = {};
+
+// 	const userModel = await db.runJob("GET_MODEL", {
+// 		modelName: "user"
+// 	});
+
+// 	async.each(
+// 		Object.keys(userList),
+// 		(socketId, next) => {
+// 			utils.runJob("SOCKET_FROM_SESSION", { socketId }, { isQuiet: true }).then(socket => {
+// 				const stationId = userList[socketId];
+// 				if (!socket || Object.keys(socket.rooms).indexOf(`station.${stationId}`) === -1) {
+// 					if (stationsCountUpdated.indexOf(stationId) === -1) stationsCountUpdated.push(stationId);
+// 					if (stationsUpdated.indexOf(stationId) === -1) stationsUpdated.push(stationId);
+// 					delete userList[socketId];
+// 					return next();
+// 				}
+// 				if (!usersPerStationCount[stationId]) usersPerStationCount[stationId] = 0;
+// 				usersPerStationCount[stationId] += 1;
+// 				if (!usersPerStation[stationId]) usersPerStation[stationId] = [];
+
+// 				return async.waterfall(
+// 					[
+// 						next => {
+// 							if (!socket.session || !socket.session.sessionId) return next("No session found.");
+// 							return cache
+// 								.runJob("HGET", {
+// 									table: "sessions",
+// 									key: socket.session.sessionId
+// 								})
+// 								.then(session => {
+// 									next(null, session);
+// 								})
+// 								.catch(next);
+// 						},
+
+// 						(session, next) => {
+// 							if (!session) return next("Session not found.");
+// 							return userModel.findOne({ _id: session.userId }, next);
+// 						},
+
+// 						(user, next) => {
+// 							if (!user) return next("User not found.");
+// 							if (usersPerStation[stationId].indexOf(user.username) !== -1)
+// 								return next("User already in the list.");
+// 							return next(null, user.username);
+// 						}
+// 					],
+// 					(err, username) => {
+// 						if (!err) {
+// 							usersPerStation[stationId].push(username);
+// 						}
+// 						next();
+// 					}
+// 				);
+// 			});
+// 			// TODO Code to show users
+// 		},
+// 		() => {
+// 			for (
+// 				let stationId = 0, stationKeys = Object.keys(usersPerStationCount);
+// 				stationId < stationKeys.length;
+// 				stationId += 1
+// 			) {
+// 				if (oldUsersPerStationCount[stationId] !== usersPerStationCount[stationId]) {
+// 					if (stationsCountUpdated.indexOf(stationId) === -1) stationsCountUpdated.push(stationId);
+// 				}
+// 			}
+
+// 			for (
+// 				let stationId = 0, stationKeys = Object.keys(usersPerStation);
+// 				stationId < stationKeys.length;
+// 				stationId += 1
+// 			) {
+// 				if (
+// 					_.difference(usersPerStation[stationId], oldUsersPerStation[stationId]).length > 0 ||
+// 					_.difference(oldUsersPerStation[stationId], usersPerStation[stationId]).length > 0
+// 				) {
+// 					if (stationsUpdated.indexOf(stationId) === -1) stationsUpdated.push(stationId);
+// 				}
+// 			}
+
+// 			stationsCountUpdated.forEach(stationId => {
+// 				console.log("INFO", "UPDATE_STATION_USER_COUNT", `Updating user count of ${stationId}.`);
+// 				cache.runJob("PUB", {
+// 					table: "station.updateUserCount",
+// 					value: stationId
+// 				});
+// 			});
+
+// 			stationsUpdated.forEach(stationId => {
+// 				console.log("INFO", "UPDATE_STATION_USER_LIST", `Updating user list of ${stationId}.`);
+// 				cache.runJob("PUB", {
+// 					table: "station.updateUsers",
+// 					value: stationId
+// 				});
+// 			});
+
+// 			// console.log("Userlist", usersPerStation);
+// 		}
+// 	);
+// }, 3000);
 
 cache.runJob("SUB", {
 	channel: "station.updateUsers",