fix: remote server search add long buffering

This commit is contained in:
Mauricio Siu
2024-12-15 19:18:41 -06:00
parent 676082fc5b
commit c2fe1eed01
2 changed files with 25 additions and 9 deletions

View File

@@ -56,9 +56,11 @@ export const setupDockerContainerLogsWebSocketServer = (
const baseCommand = `docker container logs --timestamps --tail ${tail} ${
since === "all" ? "" : `--since ${since}`
} --follow ${containerId}`;
const escapedSearch = search ? search.replace(/'/g, "'\\''") : "";
const command = search
? `${baseCommand} 2>&1 | grep -iF '${search}'`
? `${baseCommand} 2>&1 | grep --line-buffered -iF "${escapedSearch}"`
: baseCommand;
console.log("Executing SSH command:", command);
client.exec(command, (err, stream) => {
if (err) {
console.error("Execution error:", err);
@@ -66,15 +68,24 @@ export const setupDockerContainerLogsWebSocketServer = (
client.end();
return;
}
let dataReceived = false;
stream
.on("close", () => {
console.log("Stream closed, dataReceived:", dataReceived);
if (!dataReceived) {
ws.send("No matching logs found");
}
client.end();
ws.close();
})
.on("data", (data: string) => {
dataReceived = true;
ws.send(data.toString());
})
.stderr.on("data", (data) => {
console.error("Stream stderr:", data.toString());
ws.send(data.toString());
});
});
@@ -100,7 +111,7 @@ export const setupDockerContainerLogsWebSocketServer = (
since === "all" ? "" : `--since ${since}`
} --follow ${containerId}`;
const command = search
? `${baseCommand} 2>&1 | grep -iF '${search}'`
? `${baseCommand} 2>&1 | grep --line-buffered -iF '${search}'`
: baseCommand;
const ptyProcess = spawn(shell, ["-c", command], {
name: "xterm-256color",