feat: Add log streaming functionality for Docker containers and improve platform service type handling in HTTP server

This commit is contained in:
2025-11-25 08:34:10 +00:00
parent e94906b3bf
commit 76793d512b
3 changed files with 55 additions and 6 deletions

View File

@@ -939,4 +939,49 @@ export class OneboxDockerManager {
}
return this.dockerClient.listContainers();
}
/**
* Stream container logs continuously
* @param containerID The container ID
* @param callback Callback for each log line (line, isError)
*/
async streamContainerLogs(
containerID: string,
callback: (line: string, isError: boolean) => void
): Promise<void> {
try {
const container = await this.dockerClient!.getContainerById(containerID);
if (!container) {
throw new Error(`Container not found: ${containerID}`);
}
const logStream = await container.streamLogs({
stdout: true,
stderr: true,
timestamps: true,
tail: 100,
});
logStream.on('data', (chunk: Uint8Array) => {
// Docker multiplexes stdout/stderr with 8-byte header
// Byte 0: stream type (1=stdout, 2=stderr)
// Bytes 4-7: frame size (big-endian)
// Rest: actual log data
const streamType = chunk[0];
const isError = streamType === 2;
const content = new TextDecoder().decode(chunk.slice(8));
if (content.trim()) {
callback(content.trim(), isError);
}
});
logStream.on('error', (err: Error) => {
logger.error(`Log stream error for ${containerID}: ${err.message}`);
});
} catch (error) {
logger.error(`Failed to stream logs for ${containerID}: ${getErrorMessage(error)}`);
throw error;
}
}
}