From 5e7d48d84a6c537848f244905f3110eac6691765 Mon Sep 17 00:00:00 2001 From: Smith Date: Sat, 9 Mar 2024 20:59:26 +0100 Subject: [PATCH] fix duplicating GameServer instances on restarts --- package-lock.json | 4 ++-- package.json | 2 +- src/server.ts | 1 + src/watcher.ts | 42 +++++++++++++++++++++--------------------- 4 files changed, 25 insertions(+), 24 deletions(-) diff --git a/package-lock.json b/package-lock.json index 8fb3849..585d072 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "game-server-watcher", - "version": "3.1.1", + "version": "3.1.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "game-server-watcher", - "version": "3.1.1", + "version": "3.1.2", "license": "AGPL-3.0-or-later", "dependencies": { "@slack/bolt": "^3.17.1", diff --git a/package.json b/package.json index 2f4ef08..be79744 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "game-server-watcher", - "version": "3.1.1", + "version": "3.1.2", "description": "A simple discord/telegram/slack bot that can be hosted on a free service to monitor your game servers and players in style. 😎", "exports": "./dist/server.js", "type": "module", diff --git a/src/server.ts b/src/server.ts index f94a72f..abf7137 100644 --- a/src/server.ts +++ b/src/server.ts @@ -142,6 +142,7 @@ createServer(async (req, res) => { re.error = 'Invalid Request'; } } else if (reqPath[0] === 'flush' && ['servers', 'discord', 'telegram', 'slack'].includes(reqPath[1])) { + //TODO: check for and append host:port if available await watcher.restart(reqPath[1]); re.message = '🗑️ ' + reqPath[1].slice(0, 1).toUpperCase() + reqPath[1].slice(1) + ' data flushed.'; } else { diff --git a/src/watcher.ts b/src/watcher.ts index d5f9af4..fabd97f 100644 --- a/src/watcher.ts +++ b/src/watcher.ts @@ -82,21 +82,6 @@ export class Watcher { private servers: GameServer[] = []; public loop?: NodeJS.Timer; - async init(config: GameServerConfig[]) { - console.log('watcher starting...'); - - if (DISCORD_BOT_TOKEN) await discordBot.init(DISCORD_BOT_TOKEN); - if (TELEGRAM_BOT_TOKEN) await telegramBot.init(TELEGRAM_BOT_TOKEN); - if (SLACK_BOT_TOKEN && SLACK_APP_TOKEN) await slackBot.init(SLACK_BOT_TOKEN, SLACK_APP_TOKEN); - - await initDb(); - - for (const c of config) { - const gs = new GameServer(c); - this.servers.push(gs); - } - } - async check() { if (DBG) console.log('watcher checking...'); @@ -114,9 +99,20 @@ export class Watcher { } async start() { + console.log('watcher starting...'); + + if (DISCORD_BOT_TOKEN) await discordBot.init(DISCORD_BOT_TOKEN); + if (TELEGRAM_BOT_TOKEN) await telegramBot.init(TELEGRAM_BOT_TOKEN); + if (SLACK_BOT_TOKEN && SLACK_APP_TOKEN) await slackBot.init(SLACK_BOT_TOKEN, SLACK_APP_TOKEN); + await initDb(); + await db.read(); - await this.init(db.data); - + this.servers.length = 0; + for (const c of db.data) { + const gs = new GameServer(c); + this.servers.push(gs); + } + console.log('starting loop...'); // Note: pterodactyl depends on this this.loop = setInterval(async () => { await this.check(); @@ -134,10 +130,14 @@ export class Watcher { } if (flush) { - if (DBG) console.log('Deleting ' + flush + ' data'); - try { - fs.unlinkSync(DATA_PATH + flush + '.json'); - } catch (e) { } + if (!host) { + if (DBG) console.log('deleting ' + flush + ' data'); + try { + fs.unlinkSync(DATA_PATH + flush + '.json'); + } catch (e) { } + } else { + //TODO: filter by host, or if port is specified then both + } } await this.start();