From 46cb955172fc05457fe0b6b7276d9d1333be4cec Mon Sep 17 00:00:00 2001 From: LouisLam Date: Mon, 23 Aug 2021 18:52:55 +0800 Subject: [PATCH] afterLogin change to non blocking --- server/server.js | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/server/server.js b/server/server.js index 7c444118..b187404e 100644 --- a/server/server.js +++ b/server/server.js @@ -150,7 +150,7 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); if (user) { debug("afterLogin") - await afterLogin(socket, user) + afterLogin(socket, user) debug("afterLogin ok") @@ -178,7 +178,7 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); let user = await login(data.username, data.password) if (user) { - await afterLogin(socket, user) + afterLogin(socket, user) callback({ ok: true, @@ -561,7 +561,7 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); debug("check auto login") if (await setting("disableAuth")) { console.log("Disabled Auth: auto login to admin") - await afterLogin(socket, await R.findOne("user")) + afterLogin(socket, await R.findOne("user")) socket.emit("autoLogin") } else { debug("need auth") @@ -621,6 +621,8 @@ async function sendMonitorList(socket) { } async function sendNotificationList(socket) { + const timeLogger = new TimeLogger(); + let result = []; let list = await R.find("notification", " user_id = ? ", [ socket.userID, @@ -631,6 +633,9 @@ async function sendNotificationList(socket) { } io.to(socket.userID).emit("notificationList", result) + + timeLogger.print("Send Notification List"); + return list; } @@ -639,24 +644,27 @@ async function afterLogin(socket, user) { socket.join(user.id) let monitorList = await sendMonitorList(socket) - sendNotificationList(socket) - // Delay a bit, so that it let the main page to query the data first, since SQLite can process one sql at the same time only. - // For example, query the edit data first. - setTimeout(async () => { - for (let monitorID in monitorList) { - sendHeartbeatList(socket, monitorID); - sendImportantHeartbeatList(socket, monitorID); - Monitor.sendStats(io, monitorID, user.id) - } - }, 500); + await sleep(500); + + for (let monitorID in monitorList) { + await sendHeartbeatList(socket, monitorID); + } + + for (let monitorID in monitorList) { + await sendImportantHeartbeatList(socket, monitorID); + } + + for (let monitorID in monitorList) { + await Monitor.sendStats(io, monitorID, user.id) + } } async function getMonitorJSONList(userID) { let result = {}; - let monitorList = await R.find("monitor", " user_id = ? ", [ + let monitorList = await R.find("monitor", " user_id = ? ORDER BY weight DESC, name", [ userID, ]) @@ -786,6 +794,8 @@ async function sendHeartbeatList(socket, monitorID) { } socket.emit("heartbeatList", monitorID, result) + + timeLogger.print(`[Monitor: ${monitorID}] sendHeartbeatList`) } async function sendImportantHeartbeatList(socket, monitorID) {