feat(ops-dashboard): stream user service logs to the ops dashboard and resolve service containers for Docker log streaming
This commit is contained in:
@@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@serve.zone/onebox',
|
||||
version: '1.19.12',
|
||||
version: '1.20.0',
|
||||
description: 'Self-hosted container platform with automatic SSL and DNS - a mini Heroku for single servers'
|
||||
}
|
||||
|
||||
@@ -1019,7 +1019,23 @@ export class OneboxDockerManager {
|
||||
callback: (line: string, isError: boolean) => void
|
||||
): Promise<void> {
|
||||
try {
|
||||
const container = await this.dockerClient!.getContainerById(containerID);
|
||||
let container: any = null;
|
||||
try {
|
||||
container = await this.dockerClient!.getContainerById(containerID);
|
||||
} catch {
|
||||
// Not a direct container ID — try Swarm service lookup
|
||||
}
|
||||
|
||||
if (!container) {
|
||||
const serviceContainerId = await this.getContainerIdForService(containerID);
|
||||
if (serviceContainerId) {
|
||||
try {
|
||||
container = await this.dockerClient!.getContainerById(serviceContainerId);
|
||||
} catch {
|
||||
// Service container also not found
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!container) {
|
||||
throw new Error(`Container not found: ${containerID}`);
|
||||
|
||||
@@ -15,30 +15,54 @@ export class PlatformHandler {
|
||||
}
|
||||
|
||||
/**
|
||||
* Start streaming logs from all running platform service containers
|
||||
* Start streaming logs from all running containers (platform + user services)
|
||||
* and push new entries to connected dashboard clients via TypedSocket
|
||||
*/
|
||||
private async startLogStreaming(): Promise<void> {
|
||||
// Poll for running platform services every 10s and start streams for new ones
|
||||
const checkAndStream = async () => {
|
||||
const services = this.opsServerRef.oneboxRef.database.getAllPlatformServices();
|
||||
for (const service of services) {
|
||||
// Stream platform service containers
|
||||
const platformServices = this.opsServerRef.oneboxRef.database.getAllPlatformServices();
|
||||
for (const service of platformServices) {
|
||||
if (service.status !== 'running' || !service.containerId) continue;
|
||||
if (this.activeLogStreams.has(service.type)) continue;
|
||||
const key = `platform:${service.type}`;
|
||||
if (this.activeLogStreams.has(key)) continue;
|
||||
|
||||
this.activeLogStreams.set(service.type, true);
|
||||
this.activeLogStreams.set(key, true);
|
||||
logger.info(`Starting log stream for platform service: ${service.type}`);
|
||||
|
||||
try {
|
||||
await this.opsServerRef.oneboxRef.docker.streamContainerLogs(
|
||||
service.containerId,
|
||||
(line: string, isError: boolean) => {
|
||||
this.pushLogToClients(service.type as interfaces.data.TPlatformServiceType, line, isError);
|
||||
this.pushPlatformLogToClients(service.type as interfaces.data.TPlatformServiceType, line, isError);
|
||||
}
|
||||
);
|
||||
} catch (err) {
|
||||
logger.warn(`Log stream failed for ${service.type}: ${(err as Error).message}`);
|
||||
this.activeLogStreams.delete(service.type);
|
||||
this.activeLogStreams.delete(key);
|
||||
}
|
||||
}
|
||||
|
||||
// Stream user service containers
|
||||
const userServices = this.opsServerRef.oneboxRef.services.listServices();
|
||||
for (const service of userServices) {
|
||||
if (service.status !== 'running' || !service.containerID) continue;
|
||||
const key = `service:${service.name}`;
|
||||
if (this.activeLogStreams.has(key)) continue;
|
||||
|
||||
this.activeLogStreams.set(key, true);
|
||||
logger.info(`Starting log stream for user service: ${service.name}`);
|
||||
|
||||
try {
|
||||
await this.opsServerRef.oneboxRef.docker.streamContainerLogs(
|
||||
service.containerID,
|
||||
(line: string, isError: boolean) => {
|
||||
this.pushServiceLogToClients(service.name, line, isError);
|
||||
}
|
||||
);
|
||||
} catch (err) {
|
||||
logger.warn(`Log stream failed for ${service.name}: ${(err as Error).message}`);
|
||||
this.activeLogStreams.delete(key);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -49,15 +73,7 @@ export class PlatformHandler {
|
||||
setInterval(() => checkAndStream(), 15000);
|
||||
}
|
||||
|
||||
private pushLogToClients(
|
||||
serviceType: interfaces.data.TPlatformServiceType,
|
||||
line: string,
|
||||
isError: boolean,
|
||||
): void {
|
||||
const typedsocket = (this.opsServerRef.server as any)?.typedserver?.typedsocket;
|
||||
if (!typedsocket) return;
|
||||
|
||||
// Parse timestamp from Docker log line
|
||||
private parseLogLine(line: string, isError: boolean): { timestamp: string; level: string; message: string } {
|
||||
const tsMatch = line.match(/^(\d{4}-\d{2}-\d{2}T[\d:.]+Z?)\s+(.*)/);
|
||||
const timestamp = tsMatch ? tsMatch[1] : new Date().toISOString();
|
||||
const message = tsMatch ? tsMatch[2] : line;
|
||||
@@ -67,21 +83,51 @@ export class PlatformHandler {
|
||||
: msgLower.includes('warn')
|
||||
? 'warn'
|
||||
: 'info';
|
||||
return { timestamp, level, message };
|
||||
}
|
||||
|
||||
private pushPlatformLogToClients(
|
||||
serviceType: interfaces.data.TPlatformServiceType,
|
||||
line: string,
|
||||
isError: boolean,
|
||||
): void {
|
||||
const typedsocket = (this.opsServerRef.server as any)?.typedserver?.typedsocket;
|
||||
if (!typedsocket) return;
|
||||
|
||||
const entry = this.parseLogLine(line, isError);
|
||||
|
||||
// Find all dashboard clients and push
|
||||
typedsocket.findAllTargetConnectionsByTag('role', 'ops_dashboard')
|
||||
.then((connections: any[]) => {
|
||||
for (const conn of connections) {
|
||||
typedsocket.createTypedRequest<interfaces.requests.IReq_PushPlatformServiceLog>(
|
||||
'pushPlatformServiceLog',
|
||||
conn,
|
||||
).fire({
|
||||
serviceType,
|
||||
entry: { timestamp, level, message },
|
||||
}).catch(() => {}); // fire-and-forget
|
||||
).fire({ serviceType, entry }).catch(() => {});
|
||||
}
|
||||
})
|
||||
.catch(() => {}); // no connections, ignore
|
||||
.catch(() => {});
|
||||
}
|
||||
|
||||
private pushServiceLogToClients(
|
||||
serviceName: string,
|
||||
line: string,
|
||||
isError: boolean,
|
||||
): void {
|
||||
const typedsocket = (this.opsServerRef.server as any)?.typedserver?.typedsocket;
|
||||
if (!typedsocket) return;
|
||||
|
||||
const entry = this.parseLogLine(line, isError);
|
||||
|
||||
typedsocket.findAllTargetConnectionsByTag('role', 'ops_dashboard')
|
||||
.then((connections: any[]) => {
|
||||
for (const conn of connections) {
|
||||
typedsocket.createTypedRequest<interfaces.requests.IReq_PushServiceLog>(
|
||||
'pushServiceLog',
|
||||
conn,
|
||||
).fire({ serviceName, entry }).catch(() => {});
|
||||
}
|
||||
})
|
||||
.catch(() => {});
|
||||
}
|
||||
|
||||
private registerHandlers(): void {
|
||||
|
||||
Reference in New Issue
Block a user