Skip to content

Commit

Permalink
fix duplicating GameServer instances on restarts
Browse files Browse the repository at this point in the history
  • Loading branch information
a-sync committed Mar 9, 2024
1 parent 8490648 commit 5e7d48d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 24 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
1 change: 1 addition & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
42 changes: 21 additions & 21 deletions src/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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...');

Expand All @@ -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();
Expand All @@ -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();
Expand Down

0 comments on commit 5e7d48d

Please sign in to comment.