Browse Source

fix: re-enabled users list functionality, added isQuiet option for backend jobs

Signed-off-by: Jonathan <theflametrooper@gmail.com>
Jonathan 4 years ago
parent
commit
70174f340f

+ 7 - 5
backend/core.js

@@ -134,16 +134,16 @@ class CoreClass {
         });
     }
 
-    runJob(name, payload, options = {}) {
+    runJob(name, payload, options = { isQuiet: false, bypassQueue: false }) {
         let deferredPromise = new DeferredPromise();
-        const job = { name, payload, onFinish: deferredPromise };
+        const job = { name, payload, options, onFinish: deferredPromise };
 
         if (config.debug && config.debug.stationIssue === true && config.debug.captureJobs && config.debug.captureJobs.indexOf(name) !== -1) {
             this.moduleManager.debugJobs.all.push(job);
         }
 
         if (options.bypassQueue) {
-            this._runJob(job, () => {});
+            this._runJob(job, () => {}, false);
         } else {
             const priority = this.priorities[name] ? this.priorities[name] : 10;
             this.jobQueue.push(job, priority);
@@ -157,7 +157,9 @@ class CoreClass {
     }
 
     _runJob(job, cb) {
-        this.log("INFO", `Running job ${job.name}`);
+        const isQuiet = job.options.isQuiet;
+
+        if (!isQuiet) this.log("INFO", `Running job ${job.name}`);
         const startTime = Date.now();
         this.runningJobs.push(job);
         const newThis = Object.assign(
@@ -172,7 +174,7 @@ class CoreClass {
         this[job.name]
             .apply(newThis, [job.payload])
             .then((response) => {
-                this.log("INFO", `Ran job ${job.name} successfully`);
+                if (!isQuiet) this.log("INFO", `Ran job ${job.name} successfully`);
                 this.jobStatistics[job.name].successful++;
                 if (config.debug && config.debug.stationIssue === true && config.debug.captureJobs && config.debug.captureJobs.indexOf(job.name) !== -1) {
                     this.moduleManager.debugJobs.completed.push({ status: "success", job, response });

+ 130 - 131
backend/logic/actions/stations.js

@@ -1,9 +1,7 @@
 "use strict";
 
-const async = require("async"),
-    request = require("request"),
-    config = require("config"),
-    _ = require("underscore")._;
+const async = require("async");
+const _ = require("underscore")._;
 
 const hooks = require("./hooks");
 
@@ -14,6 +12,7 @@ const utils = require("../utils");
 const stations = require("../stations");
 const songs = require("../songs");
 const activities = require("../activities");
+const user = require("../db/schemas/user");
 
 // const logger = moduleManager.modules["logger"];
 
@@ -22,133 +21,133 @@ let usersPerStation = {};
 let usersPerStationCount = {};
 
 // Temporarily disabled until the messages in console can be limited
-// setInterval(async () => {
-//     let stationsCountUpdated = [];
-//     let stationsUpdated = [];
-
-//     let oldUsersPerStation = usersPerStation;
-//     usersPerStation = {};
-
-//     let oldUsersPerStationCount = usersPerStationCount;
-//     usersPerStationCount = {};
-
-//     const userModel = await db.runJob("GET_MODEL", {
-//         modelName: "user",
-//     });
-//
-//     async.each(
-//         Object.keys(userList),
-//         function(socketId, next) {
-//             utils.runJob("SOCKET_FROM_SESSION", { socketId }).then((socket) => {
-//                 let 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]++;
-//                 if (!usersPerStation[stationId])
-//                     usersPerStation[stationId] = [];
-
-//                 async.waterfall(
-//                     [
-//                         (next) => {
-//                             if (!socket.session || !socket.session.sessionId)
-//                                 return next("No session found.");
-//                             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.");
-//                             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.");
-//                             next(null, user.username);
-//                         },
-//                     ],
-//                     (err, username) => {
-//                         if (!err) {
-//                             usersPerStation[stationId].push(username);
-//                         }
-//                         next();
-//                     }
-//                 );
-//             });
-//             //TODO Code to show users
-//         },
-//         (err) => {
-//             for (let stationId in usersPerStationCount) {
-//                 if (
-//                     oldUsersPerStationCount[stationId] !==
-//                     usersPerStationCount[stationId]
-//                 ) {
-//                     if (stationsCountUpdated.indexOf(stationId) === -1)
-//                         stationsCountUpdated.push(stationId);
-//                 }
-//             }
-
-//             for (let stationId in usersPerStation) {
-//                 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 () => {
+    let stationsCountUpdated = [];
+    let stationsUpdated = [];
+
+    let oldUsersPerStation = usersPerStation;
+    usersPerStation = {};
+
+    let oldUsersPerStationCount = usersPerStationCount;
+    usersPerStationCount = {};
+
+    const userModel = await db.runJob("GET_MODEL", {
+        modelName: "user",
+    });
+
+    async.each(
+        Object.keys(userList),
+        function(socketId, next) {
+            utils.runJob("SOCKET_FROM_SESSION", { socketId }, { isQuiet: true }).then((socket) => {
+                let 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]++;
+                if (!usersPerStation[stationId])
+                    usersPerStation[stationId] = [];
+
+                async.waterfall(
+                    [
+                        (next) => {
+                            if (!socket.session || !socket.session.sessionId)
+                                return next("No session found.");
+                            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.");
+                            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.");
+                            next(null, user.username);
+                        },
+                    ],
+                    (err, username) => {
+                        if (!err) {
+                            usersPerStation[stationId].push(username);
+                        }
+                        next();
+                    }
+                );
+            });
+            //TODO Code to show users
+        },
+        (err) => {
+            for (let stationId in usersPerStationCount) {
+                if (
+                    oldUsersPerStationCount[stationId] !==
+                    usersPerStationCount[stationId]
+                ) {
+                    if (stationsCountUpdated.indexOf(stationId) === -1)
+                        stationsCountUpdated.push(stationId);
+                }
+            }
+
+            for (let stationId in usersPerStation) {
+                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",

+ 6 - 3
frontend/src/pages/Admin/tabs/Users.vue

@@ -81,9 +81,10 @@ export default {
 			this.openModal({ sector: "admin", modal: "editUser" });
 		},
 		init() {
-			this.socket.emit("users.index", result => {
-				if (result.status === "success") {
-					this.users = result.data;
+			this.socket.emit("users.index", res => {
+				console.log(res);
+				if (res.status === "success") {
+					this.users = res.data;
 					if (this.$route.query.userId) {
 						const user = this.users.find(
 							user => user._id === this.$route.query.userId
@@ -98,6 +99,8 @@ export default {
 		...mapActions("modals", ["openModal"])
 	},
 	mounted() {
+		console.log("mounted");
+
 		io.getSocket(socket => {
 			this.socket = socket;
 			if (this.socket.connected) this.init();

+ 2 - 0
frontend/src/pages/Station/index.vue

@@ -1005,6 +1005,8 @@ export default {
 		},
 		join() {
 			this.socket.emit("stations.join", this.stationName, res => {
+				console.log(res.data);
+
 				if (res.status === "success") {
 					this.loading = false;