mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-03-15 05:41:51 +00:00
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:
parent
39c2b1af81
commit
21c713255c
1 changed files with 15 additions and 1 deletions
|
@ -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));
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue