From 7196917fd3500fa9ad3d2f57f1959086e71d6c5c Mon Sep 17 00:00:00 2001 From: toast-ts <96593068+toast-ts@users.noreply.github.com> Date: Mon, 19 Feb 2024 22:08:24 +1100 Subject: [PATCH] Silence uncontactable server when limit is reached --- src/modules/MPModule.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/modules/MPModule.ts b/src/modules/MPModule.ts index 2692fe6..571e826 100644 --- a/src/modules/MPModule.ts +++ b/src/modules/MPModule.ts @@ -8,6 +8,7 @@ import {IServer} from '../models/MPServer.js'; import {XMLParser} from 'fast-xml-parser'; import {FSPlayer, FSData, FSCareerSavegame} from 'src/interfaces'; +let failedAttempts:number = 0; let loggingPrefix:string = 'MPModule'; let dataUnavailable:string = 'Unavailable'; export let refreshTimerSecs:number = 45000; @@ -131,6 +132,12 @@ export async function requestServerData(client:TClient, server:IServer):Promise< else if (data.status === 404) Logger.console('log', loggingPrefix, `(${i+1}/${maxRetries}) ${server.serverName} responded with an error (404), API is disabled or mismatched code`) } catch(err) { Logger.console('log', loggingPrefix, `Couldn't get data for ${server.serverName}: ${err.message}`); + failedAttempts++; + if (failedAttempts >= 5 && server.isActive) { + Logger.console('log', loggingPrefix, `Maximum failed requests (${failedAttempts}) reached for ${server.serverName}, silenced server for 10 minutes`); + silenceServer(client, server, 600000); + } + return null; } await new Promise(resolve=>setTimeout(resolve, 500)) } @@ -179,3 +186,9 @@ function convertPlayerUptime(playTime:number) { return (Days > 0 ? Days+' d ':'')+(Hours > 0 ? Hours+' h ':'')+(Minutes > 0 ? Minutes+' m':'') } + +function silenceServer(client:TClient, server:IServer, time:number):void { + client.MPServer.toggleServerUsability(server.serverName, false); + setTimeout(()=>client.MPServer.toggleServerUsability(server.serverName, true), time) + failedAttempts = 0; +}