From ee7aa12fd033dcb83f59346a3c3fca85fbcb7817 Mon Sep 17 00:00:00 2001 From: Borislav Pantaleev Date: Thu, 27 Feb 2025 00:06:01 +0200 Subject: [PATCH] burst cache for status, lock, notifications if command is running --- src/components/etke.cc/ServerCommandsPanel.tsx | 9 +++++++-- .../etke.cc/ServerNotificationsBadge.tsx | 2 +- src/components/etke.cc/ServerStatusBadge.tsx | 10 ++++++---- src/synapse/dataProvider.ts | 18 ++++++++++++++---- 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/components/etke.cc/ServerCommandsPanel.tsx b/src/components/etke.cc/ServerCommandsPanel.tsx index 3ec85d5..9713499 100644 --- a/src/components/etke.cc/ServerCommandsPanel.tsx +++ b/src/components/etke.cc/ServerCommandsPanel.tsx @@ -31,7 +31,6 @@ const ServerCommandsPanel = () => { if (serverCommandsResponse) { const serverCommands = serverCommandsResponse; Object.keys(serverCommandsResponse).forEach((command: string) => { - // serverCommands[command] = serverCommandsResponse[command]; serverCommands[command].additionalArgs = ""; }); setServerCommands(serverCommands); @@ -41,6 +40,12 @@ const ServerCommandsPanel = () => { fetchIsAdmin(); }, []); + useEffect(() => { + if (serverProcess.command === "") { + setCommandIsRunning(false); + } + }, [serverProcess]); + const setCommandAdditionalArgs = (command: string, additionalArgs: string) => { const updatedServerCommands = {...serverCommands}; updatedServerCommands[command].additionalArgs = additionalArgs; @@ -53,7 +58,6 @@ const ServerCommandsPanel = () => { const response = await dataProvider.runServerCommand(etkeccAdmin, command); - setCommandIsRunning(false); if (!response.success) { return; } @@ -108,6 +112,7 @@ const ServerCommandsPanel = () => { {args && { setCommandAdditionalArgs(command, e.target.value); }} diff --git a/src/components/etke.cc/ServerNotificationsBadge.tsx b/src/components/etke.cc/ServerNotificationsBadge.tsx index 6e3d26d..a1e2d1d 100644 --- a/src/components/etke.cc/ServerNotificationsBadge.tsx +++ b/src/components/etke.cc/ServerNotificationsBadge.tsx @@ -21,7 +21,7 @@ const useServerNotifications = () => { const { notifications, success } = serverNotifications; const fetchNotifications = async () => { - const notificationsResponse: ServerNotificationsResponse = await dataProvider.getServerNotifications(etkeccAdmin); + const notificationsResponse: ServerNotificationsResponse = await dataProvider.getServerNotifications(etkeccAdmin, command !== ""); setServerNotifications({ ...notificationsResponse, notifications: notificationsResponse.notifications, diff --git a/src/components/etke.cc/ServerStatusBadge.tsx b/src/components/etke.cc/ServerStatusBadge.tsx index 4dd31c7..e82588d 100644 --- a/src/components/etke.cc/ServerStatusBadge.tsx +++ b/src/components/etke.cc/ServerStatusBadge.tsx @@ -52,13 +52,15 @@ const SERVER_CURRENT_PROCCESS_INTERVAL_TIME = 5 * 1000; const useServerStatus = () => { const [serverStatus, setServerStatus] = useStore("serverStatus", { ok: false, success: false, host: "", results: [] }); + const [serverProcess, setServerProcess] = useStore("serverProcess", { command: "", locked_at: "" }); + const { command, locked_at } = serverProcess; const { etkeccAdmin } = useAppContext(); const dataProvider = useDataProvider(); const isOkay = serverStatus.ok; const successCheck = serverStatus.success; const checkServerStatus = async () => { - const serverStatus: ServerStatusResponse = await dataProvider.getServerStatus(etkeccAdmin); + const serverStatus: ServerStatusResponse = await dataProvider.getServerStatus(etkeccAdmin, command !== ""); setServerStatus({ ok: serverStatus.ok, success: serverStatus.success, @@ -89,7 +91,7 @@ const useServerStatus = () => { clearInterval(serverStatusInterval); } } - }, [etkeccAdmin]); + }, [etkeccAdmin, command]); return { isOkay, successCheck }; }; @@ -101,7 +103,7 @@ const useCurrentServerProcess = () => { const { command, locked_at } = serverProcess; const checkServerRunningProcess = async () => { - const serverProcess: ServerProcessResponse = await dataProvider.getServerRunningProcess(etkeccAdmin); + const serverProcess: ServerProcessResponse = await dataProvider.getServerRunningProcess(etkeccAdmin, command !== ""); setServerProcess({ ...serverProcess, command: serverProcess.command, @@ -130,7 +132,7 @@ const useCurrentServerProcess = () => { clearInterval(serverCheckInterval); } } - }, [etkeccAdmin]); + }, [etkeccAdmin, command]); return { command, locked_at }; }; diff --git a/src/synapse/dataProvider.ts b/src/synapse/dataProvider.ts index 83d46ba..e3e5d88 100644 --- a/src/synapse/dataProvider.ts +++ b/src/synapse/dataProvider.ts @@ -1042,9 +1042,14 @@ const baseDataProvider: SynapseDataProvider = { return { locked_at, command }; }, - getServerStatus: async (etkeAdminUrl: string): Promise => { + getServerStatus: async (etkeAdminUrl: string, burstCache: boolean = false): Promise => { + let serverURL = `${etkeAdminUrl}/status`; + if (burstCache) { + serverURL += `?time=${new Date().getTime()}`; + } + try { - const response = await fetch(`${etkeAdminUrl}/status`, { + const response = await fetch(serverURL, { headers: { "Authorization": `Bearer ${localStorage.getItem("access_token")}` } @@ -1066,9 +1071,14 @@ const baseDataProvider: SynapseDataProvider = { return { success: false, ok: false, host: "", results: [] }; }, - getServerNotifications: async (serverNotificationsUrl: string): Promise => { + getServerNotifications: async (serverNotificationsUrl: string, burstCache: boolean = false): Promise => { + let serverURL = `${serverNotificationsUrl}/notifications`; + if (burstCache) { + serverURL += `?time=${new Date().getTime()}`; + } + try { - const response = await fetch(`${serverNotificationsUrl}/notifications`, { + const response = await fetch(serverURL, { headers: { "Authorization": `Bearer ${localStorage.getItem("access_token")}` }