LogServer: retry starting the web server if it fails due to EADDRINUSE (probably old instance of the bot still shutting down)

This commit is contained in:
Dragory 2018-08-05 00:45:35 +03:00
parent 39c2b1af81
commit 21c713255c

View file

@ -1,6 +1,7 @@
import http, { ServerResponse } from "http";
import { GlobalPlugin } from "knub";
import { GuildSpamLogs } from "../data/GuildSpamLogs";
import { sleep } from "../utils";
const DEFAULT_PORT = 9920;
const logUrlRegex = /^\/spam-logs\/([a-z0-9\-]+)\/?$/i;
@ -17,7 +18,7 @@ export class LogServerPlugin extends GlobalPlugin {
protected spamLogs: GuildSpamLogs;
protected server: http.Server;
onLoad() {
async onLoad() {
this.spamLogs = new GuildSpamLogs(null);
this.server = http.createServer(async (req, res) => {
@ -33,6 +34,19 @@ export class LogServerPlugin extends GlobalPlugin {
}
});
let retried = false;
this.server.on("error", async (err: any) => {
if (err.code === "EADDRINUSE" && !retried) {
console.log("Got EADDRINUSE, retrying in 2 sec...");
retried = true;
await sleep(2000);
this.server.listen(this.configValue("port", DEFAULT_PORT));
} else {
throw err;
}
});
this.server.listen(this.configValue("port", DEFAULT_PORT));
}