fix: enable soft reconnect (#119)
Try to reconnect softly to the ts3 server instead of crashing hard in case of closed connection or errors. Also tracks if a re-connection attempt is currently in progress. Reviewed-on: #119 Reviewed-by: branch-buddy <branch-buddy@t00n.de> Co-authored-by: Timo Behrendt <t.behrendt@t00n.de> Co-committed-by: Timo Behrendt <t.behrendt@t00n.de>
This commit was merged in pull request #119.
This commit is contained in:
+23
-4
@@ -1,13 +1,13 @@
|
|||||||
import type { Gotify } from "gotify";
|
import type { Gotify } from "gotify";
|
||||||
import type { Logger } from "pino";
|
import type { Logger } from "pino";
|
||||||
import type { TeamSpeak, TextMessageTargetMode } from "ts3-nodejs-library";
|
import type { TeamSpeak, TextMessageTargetMode } from "ts3-nodejs-library";
|
||||||
import type { GotifyConfig, Mode } from "./types";
|
|
||||||
import {
|
import {
|
||||||
ClientConnect,
|
ClientConnect,
|
||||||
ClientDisconnect,
|
ClientDisconnect,
|
||||||
ClientMoved,
|
ClientMoved,
|
||||||
TextMessage,
|
TextMessage,
|
||||||
} from "ts3-nodejs-library/lib/types/Events";
|
} from "ts3-nodejs-library/lib/types/Events";
|
||||||
|
import type { GotifyConfig, Mode } from "./types";
|
||||||
|
|
||||||
function resolveMessageTarget(target: TextMessageTargetMode): string {
|
function resolveMessageTarget(target: TextMessageTargetMode): string {
|
||||||
if (target === 1) {
|
if (target === 1) {
|
||||||
@@ -45,6 +45,8 @@ export function ts3gotifyFactory(
|
|||||||
gotifyConfig: GotifyConfig,
|
gotifyConfig: GotifyConfig,
|
||||||
logger: Logger
|
logger: Logger
|
||||||
) {
|
) {
|
||||||
|
let reconnectInProgress = false;
|
||||||
|
|
||||||
function sendNotification(message: string) {
|
function sendNotification(message: string) {
|
||||||
gotifyClient
|
gotifyClient
|
||||||
.send({
|
.send({
|
||||||
@@ -87,15 +89,32 @@ export function ts3gotifyFactory(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ts3Client.on("close", async () => {
|
async function reconnectWithBackoff() {
|
||||||
|
if (reconnectInProgress) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
reconnectInProgress = true;
|
||||||
logger.info("disconnected, trying to reconnect...");
|
logger.info("disconnected, trying to reconnect...");
|
||||||
await ts3Client.reconnect(5, 1000);
|
|
||||||
|
try {
|
||||||
|
await ts3Client.reconnect(10, 2000);
|
||||||
logger.info("reconnected!");
|
logger.info("reconnected!");
|
||||||
|
} catch (error) {
|
||||||
|
const message = error instanceof Error ? error.message : String(error);
|
||||||
|
logger.error(`Error reconnecting to TS3 server: ${message}`);
|
||||||
|
} finally {
|
||||||
|
reconnectInProgress = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ts3Client.on("close", async () => {
|
||||||
|
await reconnectWithBackoff();
|
||||||
});
|
});
|
||||||
|
|
||||||
ts3Client.on("error", (error: Error) => {
|
ts3Client.on("error", (error: Error) => {
|
||||||
logger.error(`Error connecting to TS3 server: ${error.message}`);
|
logger.error(`Error connecting to TS3 server: ${error.message}`);
|
||||||
process.exit(1);
|
reconnectWithBackoff();
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
Reference in New Issue
Block a user