Browse Source

fix: fixed issues with module initialize waiting and with tasks module

Kristian Vos 5 years ago
parent
commit
40e320bdcc
5 changed files with 35 additions and 23 deletions
  1. 3 1
      backend/core.js
  2. 1 1
      backend/index.js
  3. 8 3
      backend/logic/io.js
  4. 9 11
      backend/logic/tasks.js
  5. 14 7
      backend/logic/utils.js

+ 3 - 1
backend/core.js

@@ -2,6 +2,8 @@ const EventEmitter = require('events');
 
 const bus = new EventEmitter();
 
+bus.setMaxListeners(1000);
+
 module.exports = class {
 	constructor(name, moduleManager) {
 		this.name = name;
@@ -69,7 +71,7 @@ module.exports = class {
 	}
 
 	_validateHook() {
-		return Promise.race([this._onInitialize, this._isInitialized]).then(
+		return Promise.race([this._onInitialize(), this._isInitialized()]).then(
 			() => this._isNotLocked()
 		);
 	}

+ 1 - 1
backend/index.js

@@ -66,7 +66,7 @@ class ModuleManager {
 	}
 
 	async printStatus() {
-		try { await Promise.race([this.logger._onInitialize, this.logger._isInitialized]); } catch { return; }
+		try { await Promise.race([this.logger._onInitialize(), this.logger._isInitialized()]); } catch { return; }
 		if (!this.fancyConsole) return;
 		
 		let colors = this.logger.colors;

+ 8 - 3
backend/logic/io.js

@@ -31,9 +31,9 @@ module.exports = class extends coreClass {
 			const SIDname = config.get("cookie.SIDname");
 
 			// TODO: Check every 30s/60s, for all sockets, if they are still allowed to be in the rooms they are in, and on socket at all (permission changing/banning)
-			this.io = socketio(app.server);
+			this._io = socketio(app.server);
 
-			this.io.use(async (socket, next) => {
+			this._io.use(async (socket, next) => {
 				try { await this._validateHook(); } catch { return; }
 
 				let SID;
@@ -95,7 +95,7 @@ module.exports = class extends coreClass {
 				});
 			});
 
-			this.io.on('connection', async socket => {
+			this._io.on('connection', async socket => {
 				try { await this._validateHook(); } catch { return; }
 
 				let sessionInfo = '';
@@ -186,4 +186,9 @@ module.exports = class extends coreClass {
 			resolve();
 		});
 	}
+
+	async io () {
+		try { await this._validateHook(); } catch { return; }
+		return this._io;
+	}
 }

+ 9 - 11
backend/logic/tasks.js

@@ -53,13 +53,15 @@ module.exports = class extends coreClass {
 		try { await this._validateHook(); } catch { return; }
 
 		if (task.timer) task.timer.pause();
-
-		task.fn(() => {
-			task.lastRan = Date.now();
-			task.timer = new utils.Timer(() => {
-				this.handleTask(task);
-			}, task.timeout, false);
-		});
+		
+		task.fn.apply(this, [
+			() => {
+				task.lastRan = Date.now();
+				task.timer = new this.utils.Timer(() => {
+					this.handleTask(task);
+				}, task.timeout, false);
+			}
+		]);
 	}
 
 	/*testTask(callback) {
@@ -72,8 +74,6 @@ module.exports = class extends coreClass {
 	}*/
 
 	async checkStationSkipTask(callback) {
-		try { await this._validateHook(); } catch { return; }
-
 		this.logger.info("TASK_STATIONS_SKIP_CHECK", `Checking for stations to be skipped.`, false);
 		async.waterfall([
 			(next) => {
@@ -99,8 +99,6 @@ module.exports = class extends coreClass {
 	}
 
 	async sessionClearingTask(callback) {
-		try { await this._validateHook(); } catch { return; }
-	
 		this.logger.info("TASK_SESSION_CLEAR", `Checking for sessions to be cleared.`, false);
 		async.waterfall([
 			(next) => {

+ 14 - 7
backend/logic/utils.js

@@ -172,7 +172,8 @@ module.exports = class extends coreClass {
 	async socketFromSession(socketId) {
 		try { await this._validateHook(); } catch { return; }
 
-		let ns = this.io.io.of("/");
+		let io = await this.io.io();
+		let ns = io.of("/");
 		if (ns) {
 			return ns.connected[socketId];
 		}
@@ -181,7 +182,8 @@ module.exports = class extends coreClass {
 	async socketsFromSessionId(sessionId, cb) {
 		try { await this._validateHook(); } catch { return; }
 
-		let ns = this.io.io.of("/");
+		let io = await this.io.io();
+		let ns = io.of("/");
 		let sockets = [];
 		if (ns) {
 			async.each(Object.keys(ns.connected), (id, next) => {
@@ -197,7 +199,8 @@ module.exports = class extends coreClass {
 	async socketsFromUser(userId, cb) {
 		try { await this._validateHook(); } catch { return; }
 
-		let ns = this.io.io.of("/");
+		let io = await this.io.io();
+		let ns = io.of("/");
 		let sockets = [];
 		if (ns) {
 			async.each(Object.keys(ns.connected), (id, next) => {
@@ -215,7 +218,8 @@ module.exports = class extends coreClass {
 	async socketsFromIP(ip, cb) {
 		try { await this._validateHook(); } catch { return; }
 
-		let ns = this.io.io.of("/");
+		let io = await this.io.io();
+		let ns = io.of("/");
 		let sockets = [];
 		if (ns) {
 			async.each(Object.keys(ns.connected), (id, next) => {
@@ -233,7 +237,8 @@ module.exports = class extends coreClass {
 	async socketsFromUserWithoutCache(userId, cb) {
 		try { await this._validateHook(); } catch { return; }
 
-		let ns = this.io.io.of("/");
+		let io = await this.io.io();
+		let ns = io.of("/");
 		let sockets = [];
 		if (ns) {
 			async.each(Object.keys(ns.connected), (id, next) => {
@@ -306,7 +311,8 @@ module.exports = class extends coreClass {
 	async emitToRoom(room, ...args) {
 		try { await this._validateHook(); } catch { return; }
 
-		let sockets = this.io.io.sockets.sockets;
+		let io = await this.io.io();
+		let sockets = io.sockets.sockets;
 		for (let id in sockets) {
 			let socket = sockets[id];
 			if (socket.rooms[room]) {
@@ -318,7 +324,8 @@ module.exports = class extends coreClass {
 	async getRoomSockets(room) {
 		try { await this._validateHook(); } catch { return; }
 
-		let sockets = this.io.io.sockets.sockets;
+		let io = await this.io.io();
+		let sockets = io.sockets.sockets;
 		let roomSockets = [];
 		for (let id in sockets) {
 			let socket = sockets[id];