Merge pull request #614 from Dokploy/611-websocket-pending-for-minutes-after-container-starts

fix(dokploy): prevent to have pending connections docker logs
This commit is contained in:
Mauricio Siu 2024-10-27 14:14:30 -06:00 committed by GitHub
commit 9872c4fee3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 17 deletions

View File

@ -18,24 +18,28 @@ export const DockerLogsId: React.FC<Props> = ({
}) => {
const [term, setTerm] = React.useState<Terminal>();
const [lines, setLines] = React.useState<number>(40);
const wsRef = useRef<WebSocket | null>(null); // Ref to hold WebSocket instance
const wsRef = useRef<WebSocket | null>(null);
useEffect(() => {
// if (containerId === "select-a-container") {
// return;
// }
if (containerId === "select-a-container") {
return;
}
if (
wsRef.current &&
(wsRef.current.readyState === WebSocket.OPEN ||
wsRef.current.readyState === WebSocket.CONNECTING)
) {
wsRef.current.close();
wsRef.current = null;
}
const container = document.getElementById(id);
if (container) {
container.innerHTML = "";
}
if (wsRef.current) {
console.log(wsRef.current);
if (wsRef.current.readyState === WebSocket.OPEN) {
wsRef.current.close();
}
wsRef.current = null;
}
// Crear nueva instancia de Terminal
const termi = new Terminal({
cursorBlink: true,
cols: 80,
@ -45,7 +49,6 @@ export const DockerLogsId: React.FC<Props> = ({
fontSize: 14,
fontFamily:
'ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace',
convertEol: true,
theme: {
cursor: "transparent",
@ -54,10 +57,10 @@ export const DockerLogsId: React.FC<Props> = ({
});
const protocol = window.location.protocol === "https:" ? "wss:" : "ws:";
const wsUrl = `${protocol}//${window.location.host}/docker-container-logs?containerId=${containerId}&tail=${lines}${serverId ? `&serverId=${serverId}` : ""}`;
const ws = new WebSocket(wsUrl);
wsRef.current = ws;
const fitAddon = new FitAddon();
termi.loadAddon(fitAddon);
// @ts-ignore
@ -76,17 +79,17 @@ export const DockerLogsId: React.FC<Props> = ({
ws.onclose = (e) => {
console.log(e.reason);
termi.write(`Connection closed!\nReason: ${e.reason}\n`);
wsRef.current = null;
};
return () => {
if (wsRef.current?.readyState === WebSocket.OPEN) {
ws.close();
wsRef.current = null;
wsRef.current.close();
}
wsRef.current = null;
};
}, [lines, containerId]);
}, [containerId, lines]);
useEffect(() => {
term?.clear();

View File

@ -43,6 +43,15 @@ export const setupDockerContainerLogsWebSocketServer = (
ws.close();
return;
}
const pingInterval = setInterval(() => {
if (ws.readyState === ws.OPEN) {
ws.ping();
}
}, 30000);
ws.on("close", () => clearInterval(pingInterval));
try {
if (serverId) {
const server = await findServerById(serverId);