Skip to content

Commit

Permalink
refactor: use streams
Browse files Browse the repository at this point in the history
  • Loading branch information
Siumauricio committed Jan 2, 2025
1 parent e3d4280 commit 3ec3ff8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 130 deletions.
1 change: 0 additions & 1 deletion apps/dokploy/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ void app.prepare().then(async () => {
});

// WEBSOCKET
setupMonitoringWebSocketServer(server);
setupDeploymentLogsWebSocketServer(server);
setupDockerContainerLogsWebSocketServer(server);
setupDockerContainerTerminalWebSocketServer(server);
Expand Down
122 changes: 0 additions & 122 deletions apps/dokploy/server/wss/monitoring.ts

This file was deleted.

25 changes: 22 additions & 3 deletions apps/monitoring/src/containers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const REFRESH_RATE_CONTAINER = Number(
process.env.CONTAINER_REFRESH_RATE || 10000,
);

const logStreams = new Map<string, fs.WriteStream>();
export const logContainerMetrics = () => {
console.log("Refresh rate:", REFRESH_RATE_CONTAINER);

Expand All @@ -28,6 +29,11 @@ export const logContainerMetrics = () => {
const cleanup = async () => {
if (job) {
job.cancel();

for (const stream of logStreams.values()) {
stream.end();
}
// logStreams.forEach((stream) => stream.end());
}
};

Expand Down Expand Up @@ -96,9 +102,23 @@ export const logContainerMetrics = () => {
}
}

// Escribir la nueva línea
await fs.promises.appendFile(containerPath, logLine);
if (!logStreams.has(serviceName)) {
console.log(logStreams.size);
logStreams.set(
serviceName,
fs.createWriteStream(containerPath, { flags: "a" }),
);
}

const stream = logStreams.get(serviceName);
if (stream) {
if (!stream.write(logLine)) {
stream.once("drain", () => stream.write(logLine));
}
}

// Escribir la nueva línea
// await fs.promises.appendFile(containerPath, logLine);
} catch (error) {
console.error(
`Error writing metrics for container ${container.Name}:`,
Expand All @@ -121,7 +141,6 @@ export const logContainerMetrics = () => {
Math.floor(REFRESH_RATE_CONTAINER / 1000),
);

console.log(rule);
job = schedule.scheduleJob(rule, runMetricsCollection);

process.on("SIGTERM", cleanup);
Expand Down
14 changes: 10 additions & 4 deletions apps/monitoring/src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ const getServerMetrics = async () => {
};

const REFRESH_RATE_SERVER = Number(process.env.REFRESH_RATE_SERVER || 10000);
const MAX_FILE_SIZE_MB = Number(process.env.MAX_FILE_SIZE_MB || 10); // 10 MB por defecto
const MAX_FILE_SIZE_MB = Number(process.env.MAX_FILE_SIZE_MB || 10);

// Crear el WriteStream (mantener abierto para reutilizarlo)
const logStream = fs.createWriteStream(serverLogFile, { flags: "a" });

export const logServerMetrics = () => {
const rule = new schedule.RecurrenceRule();
Expand Down Expand Up @@ -87,9 +90,12 @@ export const logServerMetrics = () => {
}
}

fs.appendFile(serverLogFile, logLine, (err) => {
if (err) console.error("Error to write server metrics:", err);
});
if (!logStream.write(logLine)) {
console.log("Escribiendo....");
logStream.once("drain", () => {
logStream.write(logLine);
});
}
});

// Cleanup function
Expand Down

0 comments on commit 3ec3ff8

Please sign in to comment.