refactor(dokploy): terminate connections

This commit is contained in:
Mauricio Siu 2024-10-27 15:09:27 -06:00
parent 9872c4fee3
commit ae5df82887

View File

@ -5,6 +5,11 @@ import { Client } from "ssh2";
import { WebSocketServer } from "ws";
import { getShell } from "./utils";
function heartbeat() {
// @ts-ignore
this.isAlive = true;
}
export const setupDockerContainerLogsWebSocketServer = (
server: http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>,
) => {
@ -44,14 +49,10 @@ export const setupDockerContainerLogsWebSocketServer = (
return;
}
const pingInterval = setInterval(() => {
if (ws.readyState === ws.OPEN) {
ws.ping();
}
}, 30000);
ws.on("close", () => clearInterval(pingInterval));
// @ts-ignore
ws.isAlive = true;
ws.on("error", console.error);
ws.on("pong", heartbeat);
try {
if (serverId) {
const server = await findServerById(serverId);
@ -147,4 +148,18 @@ export const setupDockerContainerLogsWebSocketServer = (
ws.send(errorMessage);
}
});
const interval = setInterval(function ping() {
for (const ws of wssTerm.clients) {
// @ts-ignore
if (ws.isAlive === false) return ws.terminate();
// @ts-ignore
ws.isAlive = false;
ws.ping();
}
}, 30000);
wssTerm.on("close", function close() {
clearInterval(interval);
});
};