From b46051820107ea6ec14f97b36d68d54c6516d557 Mon Sep 17 00:00:00 2001 From: toast-ts <96593068+toast-ts@users.noreply.github.com> Date: Fri, 15 Mar 2024 15:12:56 +1100 Subject: [PATCH] Improve MPModule's failure rate --- .pnp.cjs | 50 ++++++++++++++++++++--------------------- package.json | 8 +++---- src/modules/MPModule.ts | 33 ++++++++++++++++++--------- yarn.lock | 44 ++++++++++++++++++------------------ 4 files changed, 73 insertions(+), 62 deletions(-) diff --git a/.pnp.cjs b/.pnp.cjs index 1621c5f..1effc5e 100644 --- a/.pnp.cjs +++ b/.pnp.cjs @@ -30,7 +30,7 @@ const RAW_RUNTIME_STATE = ["@octokit/rest", "npm:20.0.2"],\ ["@toast/tokenservice-client", "npm:1.0.16::__archiveUrl=https%3A%2F%2Fgit.toast-server.net%2Fapi%2Fpackages%2Ftoast%2Fnpm%2F%2540toast%252Ftokenservice-client%2F-%2F1.0.16%2Ftokenservice-client-1.0.16.tgz"],\ ["@types/ms", "npm:0.7.34"],\ - ["@types/node", "npm:20.11.25"],\ + ["@types/node", "npm:20.11.27"],\ ["@types/node-cron", "npm:3.0.11"],\ ["@types/pg", "npm:8.11.2"],\ ["@types/sequelize", "npm:4.28.20"],\ @@ -38,7 +38,7 @@ const RAW_RUNTIME_STATE = ["dayjs", "npm:1.11.10"],\ ["discord.js", "npm:14.14.1"],\ ["fast-xml-parser", "npm:4.3.5"],\ - ["mathjs", "npm:12.4.0"],\ + ["mathjs", "npm:12.4.1"],\ ["ms", "npm:2.1.3"],\ ["node-cron", "npm:3.0.3"],\ ["pg", "virtual:20c353e2d6536e37339997f03975c6a660f4d296e664d291bd43620c6162cca8eb5ef90b0998dc9db75ff6862e5da587d0530bae26805f5fadc8f17aaa4ff794#npm:8.11.3"],\ @@ -46,9 +46,9 @@ const RAW_RUNTIME_STATE = ["redis", "npm:4.6.13"],\ ["sequelize", "virtual:20c353e2d6536e37339997f03975c6a660f4d296e664d291bd43620c6162cca8eb5ef90b0998dc9db75ff6862e5da587d0530bae26805f5fadc8f17aaa4ff794#npm:6.37.1"],\ ["simple-git", "npm:3.22.0"],\ - ["systeminformation", "npm:5.22.0"],\ + ["systeminformation", "npm:5.22.2"],\ ["typescript", "patch:typescript@npm%3A5.4.2#optional!builtin::version=5.4.2&hash=5adc0c"],\ - ["undici", "npm:6.7.1"]\ + ["undici", "npm:6.9.0"]\ ],\ "linkType": "SOFT"\ }]\ @@ -632,7 +632,7 @@ const RAW_RUNTIME_STATE = "packageLocation": "./.yarn/cache/@types-continuation-local-storage-npm-3.2.7-763e4dc12b-1f272b53bc.zip/node_modules/@types/continuation-local-storage/",\ "packageDependencies": [\ ["@types/continuation-local-storage", "npm:3.2.7"],\ - ["@types/node", "npm:20.11.25"]\ + ["@types/node", "npm:20.11.27"]\ ],\ "linkType": "HARD"\ }]\ @@ -666,10 +666,10 @@ const RAW_RUNTIME_STATE = }]\ ]],\ ["@types/node", [\ - ["npm:20.11.25", {\ - "packageLocation": "./.yarn/cache/@types-node-npm-20.11.25-b6ac39dd46-861265f1bb.zip/node_modules/@types/node/",\ + ["npm:20.11.27", {\ + "packageLocation": "./.yarn/cache/@types-node-npm-20.11.27-63306d6ffa-4be53485d4.zip/node_modules/@types/node/",\ "packageDependencies": [\ - ["@types/node", "npm:20.11.25"],\ + ["@types/node", "npm:20.11.27"],\ ["undici-types", "npm:5.26.5"]\ ],\ "linkType": "HARD"\ @@ -689,7 +689,7 @@ const RAW_RUNTIME_STATE = "packageLocation": "./.yarn/cache/@types-pg-npm-8.11.2-bdf321bea4-7c05cf63be.zip/node_modules/@types/pg/",\ "packageDependencies": [\ ["@types/pg", "npm:8.11.2"],\ - ["@types/node", "npm:20.11.25"],\ + ["@types/node", "npm:20.11.27"],\ ["pg-protocol", "npm:1.6.0"],\ ["pg-types", "npm:4.0.2"]\ ],\ @@ -723,7 +723,7 @@ const RAW_RUNTIME_STATE = "packageLocation": "./.yarn/cache/@types-ws-npm-8.5.10-a877a38f71-9b414dc5e0.zip/node_modules/@types/ws/",\ "packageDependencies": [\ ["@types/ws", "npm:8.5.10"],\ - ["@types/node", "npm:20.11.25"]\ + ["@types/node", "npm:20.11.27"]\ ],\ "linkType": "HARD"\ }],\ @@ -731,7 +731,7 @@ const RAW_RUNTIME_STATE = "packageLocation": "./.yarn/cache/@types-ws-npm-8.5.9-91d1b2ab07-7cf66383b8.zip/node_modules/@types/ws/",\ "packageDependencies": [\ ["@types/ws", "npm:8.5.9"],\ - ["@types/node", "npm:20.11.25"]\ + ["@types/node", "npm:20.11.27"]\ ],\ "linkType": "HARD"\ }]\ @@ -799,7 +799,7 @@ const RAW_RUNTIME_STATE = ["@octokit/rest", "npm:20.0.2"],\ ["@toast/tokenservice-client", "npm:1.0.16::__archiveUrl=https%3A%2F%2Fgit.toast-server.net%2Fapi%2Fpackages%2Ftoast%2Fnpm%2F%2540toast%252Ftokenservice-client%2F-%2F1.0.16%2Ftokenservice-client-1.0.16.tgz"],\ ["@types/ms", "npm:0.7.34"],\ - ["@types/node", "npm:20.11.25"],\ + ["@types/node", "npm:20.11.27"],\ ["@types/node-cron", "npm:3.0.11"],\ ["@types/pg", "npm:8.11.2"],\ ["@types/sequelize", "npm:4.28.20"],\ @@ -807,7 +807,7 @@ const RAW_RUNTIME_STATE = ["dayjs", "npm:1.11.10"],\ ["discord.js", "npm:14.14.1"],\ ["fast-xml-parser", "npm:4.3.5"],\ - ["mathjs", "npm:12.4.0"],\ + ["mathjs", "npm:12.4.1"],\ ["ms", "npm:2.1.3"],\ ["node-cron", "npm:3.0.3"],\ ["pg", "virtual:20c353e2d6536e37339997f03975c6a660f4d296e664d291bd43620c6162cca8eb5ef90b0998dc9db75ff6862e5da587d0530bae26805f5fadc8f17aaa4ff794#npm:8.11.3"],\ @@ -815,9 +815,9 @@ const RAW_RUNTIME_STATE = ["redis", "npm:4.6.13"],\ ["sequelize", "virtual:20c353e2d6536e37339997f03975c6a660f4d296e664d291bd43620c6162cca8eb5ef90b0998dc9db75ff6862e5da587d0530bae26805f5fadc8f17aaa4ff794#npm:6.37.1"],\ ["simple-git", "npm:3.22.0"],\ - ["systeminformation", "npm:5.22.0"],\ + ["systeminformation", "npm:5.22.2"],\ ["typescript", "patch:typescript@npm%3A5.4.2#optional!builtin::version=5.4.2&hash=5adc0c"],\ - ["undici", "npm:6.7.1"]\ + ["undici", "npm:6.9.0"]\ ],\ "linkType": "SOFT"\ }]\ @@ -1015,10 +1015,10 @@ const RAW_RUNTIME_STATE = }]\ ]],\ ["mathjs", [\ - ["npm:12.4.0", {\ - "packageLocation": "./.yarn/cache/mathjs-npm-12.4.0-4e73cebf2f-ee168a7e4b.zip/node_modules/mathjs/",\ + ["npm:12.4.1", {\ + "packageLocation": "./.yarn/cache/mathjs-npm-12.4.1-ca45bb0559-171cdf4b6a.zip/node_modules/mathjs/",\ "packageDependencies": [\ - ["mathjs", "npm:12.4.0"],\ + ["mathjs", "npm:12.4.1"],\ ["@babel/runtime", "npm:7.24.0"],\ ["complex.js", "npm:2.1.1"],\ ["decimal.js", "npm:10.4.3"],\ @@ -1487,10 +1487,10 @@ const RAW_RUNTIME_STATE = }]\ ]],\ ["systeminformation", [\ - ["npm:5.22.0", {\ - "packageLocation": "./.yarn/unplugged/systeminformation-npm-5.22.0-ae815bb44c/node_modules/systeminformation/",\ + ["npm:5.22.2", {\ + "packageLocation": "./.yarn/unplugged/systeminformation-npm-5.22.2-646ba0b2c9/node_modules/systeminformation/",\ "packageDependencies": [\ - ["systeminformation", "npm:5.22.0"]\ + ["systeminformation", "npm:5.22.2"]\ ],\ "linkType": "HARD"\ }]\ @@ -1575,10 +1575,10 @@ const RAW_RUNTIME_STATE = ],\ "linkType": "HARD"\ }],\ - ["npm:6.7.1", {\ - "packageLocation": "./.yarn/cache/undici-npm-6.7.1-cc741edc29-7e66eb7fb6.zip/node_modules/undici/",\ + ["npm:6.9.0", {\ + "packageLocation": "./.yarn/cache/undici-npm-6.9.0-9cc1c4dd88-ee22751758.zip/node_modules/undici/",\ "packageDependencies": [\ - ["undici", "npm:6.7.1"]\ + ["undici", "npm:6.9.0"]\ ],\ "linkType": "HARD"\ }]\ @@ -1624,7 +1624,7 @@ const RAW_RUNTIME_STATE = "packageLocation": "./.yarn/cache/wkx-npm-0.5.0-fca5152cd8-b8975e33f9.zip/node_modules/wkx/",\ "packageDependencies": [\ ["wkx", "npm:0.5.0"],\ - ["@types/node", "npm:20.11.25"]\ + ["@types/node", "npm:20.11.27"]\ ],\ "linkType": "HARD"\ }]\ diff --git a/package.json b/package.json index 2e02daa..0895d5e 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ "dayjs": "1.11.10", "discord.js": "14.14.1", "fast-xml-parser": "4.3.5", - "mathjs": "12.4.0", + "mathjs": "12.4.1", "ms": "2.1.3", "node-cron": "3.0.3", "pg": "8.11.3", @@ -42,12 +42,12 @@ "redis": "4.6.13", "sequelize": "6.37.1", "simple-git": "3.22.0", - "systeminformation": "5.22.0", - "undici": "6.7.1" + "systeminformation": "5.22.2", + "undici": "6.9.0" }, "devDependencies": { "@types/ms": "0.7.34", - "@types/node": "20.11.25", + "@types/node": "20.11.27", "@types/node-cron": "3.0.11", "@types/pg": "8.11.2", "@types/sequelize": "4.28.20", diff --git a/src/modules/MPModule.ts b/src/modules/MPModule.ts index bf44bcf..f027d3d 100644 --- a/src/modules/MPModule.ts +++ b/src/modules/MPModule.ts @@ -8,7 +8,6 @@ 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; @@ -17,6 +16,10 @@ let refreshIntrvlTxt:string = `Refreshes every ${refreshTimerSecs/1000} seconds. let offlineStatus:string = 'Server is offline'; let unavailableStatus:string = 'Server didn\'t respond'; +interface IServerExt extends IServer { + failureCount?:number; +} + export default async(client:TClient)=>{ const message = await (client.channels.resolve(isBotInDevMode ? '1091300529696673792' : '543494084363288637') as Discord.TextChannel).messages.fetch(isBotInDevMode ? '1104563309451161742' : '1149141188079779900'); if (!client.config.botSwitches.mpSys) return message.edit({content: null, embeds: [mpModuleDisabled(client)]}); @@ -123,25 +126,33 @@ async function multifarmWebhook(client:TClient, server:IServer, webhookId:string }) } -export async function requestServerData(client:TClient, server:IServer):Promise<{dss:FSData, csg:FSCareerSavegame}|undefined>{ +export async function requestServerData(client:TClient, server:IServerExt):Promise<{dss:FSData, csg:FSCareerSavegame}|undefined>{ async function retryReqs(url:string) { // Attempt to reduce the failure rate of the requests before giving up and retrying in next refresh. - let maxRetries:number = 3; + let maxRetries:number = 4; + let delay:number = 500; + for (let i = 0; i < maxRetries; i++) { try { const data = await Undici.fetch(url, {keepalive: true, signal: AbortSignal.timeout(12000), headers: {'User-Agent': `${client.user.username} - MPModule/undici`}}); - if (data.status === 200 ?? 204) return data; - 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`) + if (data.status === 200 ?? 204) { + server.failureCount = 0; + delay = 500; + return data + } 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`); + else if (data.status === 400) Logger.console('log', loggingPrefix, `(${i+1}/${maxRetries}) ${server.serverName} responded with an error (400), bad endpoint or ratelimited`); // Seems to be taking L's left and right hourly... } 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`); + server.failureCount++; + if (server.failureCount >= 6 && server.isActive) { + Logger.console('log', loggingPrefix, `Maximum failed requests (${server.failureCount}) reached for ${server.serverName}, silenced server for 10 minutes`); silenceServer(client, server, 600000); } return null; } - await new Promise(resolve=>setTimeout(resolve, 500)) + await new Promise(resolve=>setTimeout(resolve, delay)); + delay *= 2; + console.log('Request delayed by %d for %s', delay, server.serverName); } return null; } @@ -189,8 +200,8 @@ 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 { +function silenceServer(client:TClient, server:IServerExt, time:number):void { client.MPServer.toggleServerUsability(server.serverName, false); setTimeout(()=>client.MPServer.toggleServerUsability(server.serverName, true), time) - failedAttempts = 0; + server.failureCount = 0; } diff --git a/yarn.lock b/yarn.lock index 000f929..36852b3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5,7 +5,7 @@ __metadata: version: 8 cacheKey: 10 -"@babel/runtime@npm:^7.23.9": +"@babel/runtime@npm:^7.24.0": version: 7.24.0 resolution: "@babel/runtime@npm:7.24.0" dependencies: @@ -482,12 +482,12 @@ __metadata: languageName: node linkType: hard -"@types/node@npm:*, @types/node@npm:20.11.25": - version: 20.11.25 - resolution: "@types/node@npm:20.11.25" +"@types/node@npm:*, @types/node@npm:20.11.27": + version: 20.11.27 + resolution: "@types/node@npm:20.11.27" dependencies: undici-types: "npm:~5.26.4" - checksum: 10/861265f1bbb151404bd8842b595f027a4ff067c61ecff9a37b9f7f53922c18dd532c8e795e8e7675dd8dba056645623fd2b9848d5ef72863ec3609096cd2923e + checksum: 10/4be53485d499dd7c7896190e76a0ce1f6c6917d1f4d0b4b240b3670160fcbc548daed32beaac0fc92429b37dbeaa2496fc56f460acaab969bddb77394318a89b languageName: node linkType: hard @@ -589,7 +589,7 @@ __metadata: "@octokit/rest": "npm:20.0.2" "@toast/tokenservice-client": "npm:1.0.16" "@types/ms": "npm:0.7.34" - "@types/node": "npm:20.11.25" + "@types/node": "npm:20.11.27" "@types/node-cron": "npm:3.0.11" "@types/pg": "npm:8.11.2" "@types/sequelize": "npm:4.28.20" @@ -597,7 +597,7 @@ __metadata: dayjs: "npm:1.11.10" discord.js: "npm:14.14.1" fast-xml-parser: "npm:4.3.5" - mathjs: "npm:12.4.0" + mathjs: "npm:12.4.1" ms: "npm:2.1.3" node-cron: "npm:3.0.3" pg: "npm:8.11.3" @@ -605,9 +605,9 @@ __metadata: redis: "npm:4.6.13" sequelize: "npm:6.37.1" simple-git: "npm:3.22.0" - systeminformation: "npm:5.22.0" + systeminformation: "npm:5.22.2" typescript: "npm:5.4.2" - undici: "npm:6.7.1" + undici: "npm:6.9.0" languageName: unknown linkType: soft @@ -763,11 +763,11 @@ __metadata: languageName: node linkType: hard -"mathjs@npm:12.4.0": - version: 12.4.0 - resolution: "mathjs@npm:12.4.0" +"mathjs@npm:12.4.1": + version: 12.4.1 + resolution: "mathjs@npm:12.4.1" dependencies: - "@babel/runtime": "npm:^7.23.9" + "@babel/runtime": "npm:^7.24.0" complex.js: "npm:^2.1.1" decimal.js: "npm:^10.4.3" escape-latex: "npm:^1.2.0" @@ -778,7 +778,7 @@ __metadata: typed-function: "npm:^4.1.1" bin: mathjs: bin/cli.js - checksum: 10/ee168a7e4be0bebe9fbd69292462f162d6aae973500e00dbf00f44392ccf80f4de86883819576df2434e9b3e4d6c222a14613e2c0450db8316c373f130bee2b6 + checksum: 10/171cdf4b6ad4786f902d112d4208a2e5b02381cd08a2b974d957b56d0102808064ddfcc7b953bef6022ad0302f3181b7f6146b3be6b54a240579a36b90c463ff languageName: node linkType: hard @@ -1146,12 +1146,12 @@ __metadata: languageName: node linkType: hard -"systeminformation@npm:5.22.0": - version: 5.22.0 - resolution: "systeminformation@npm:5.22.0" +"systeminformation@npm:5.22.2": + version: 5.22.2 + resolution: "systeminformation@npm:5.22.2" bin: systeminformation: lib/cli.js - checksum: 10/a739041bf0ef572eaebd06819ed54bb5f4a785793e3d542f16f39914ae5bbbf7d44b204ec936dcf560056709e97e2e55c11ee3ceda7b9585e778979f63ab7e3b + checksum: 10/ec74e682d77e86997d76460e8b74fa7a22c8f0a9adc3c9f46fec8bbb13f76fe86f590c3c52cf20f7cfbb0cf51e9c07f9a8170515a9d7cfd0dd087b99edaf6659 conditions: (os=darwin | os=linux | os=win32 | os=freebsd | os=openbsd | os=netbsd | os=sunos | os=android) languageName: node linkType: hard @@ -1243,10 +1243,10 @@ __metadata: languageName: node linkType: hard -"undici@npm:6.7.1": - version: 6.7.1 - resolution: "undici@npm:6.7.1" - checksum: 10/7e66eb7fb6b3641efc9381b335334a7fe0d69d4b8004147b2a7658f279adfd392fb908acf71627e2cd1f863d9ab4d5b1470139364468d486ab69aee43a8246e5 +"undici@npm:6.9.0": + version: 6.9.0 + resolution: "undici@npm:6.9.0" + checksum: 10/ee227517586f96b73db3b7a24387f101d191df6a6395ec0199688d6e56195dbe8c9d3456be2fa4a70af30fb1a9a905142826f9bf89c328dec842f00b55e9e5c2 languageName: node linkType: hard