Skip to content

Commit

Permalink
Move rotateLogFiles to utils (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
huchenlei authored Nov 15, 2024
1 parent 5d6a081 commit 24ad996
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
17 changes: 1 addition & 16 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { graphics } from 'systeminformation';
import { ComfyServerConfig } from './config/comfyServerConfig';
import todesktop from '@todesktop/runtime';
import { DownloadManager } from './models/DownloadManager';
import { findAvailablePort, getModelsDirectory } from './utils';
import { findAvailablePort, getModelsDirectory, rotateLogFiles } from './utils';
import { ComfySettings } from './config/comfySettings';
import dotenv from 'dotenv';
import { buildMenu } from './menu/menu';
Expand Down Expand Up @@ -498,18 +498,3 @@ async function serverStart() {
loadComfyIntoMainWindow();
}
}

/**
* Rotate old log files by adding a timestamp to the end of the file.
* @param logDir The directory to rotate the logs in.
* @param baseName The base name of the log file.
*/
const rotateLogFiles = (logDir: string, baseName: string) => {
const currentLogPath = path.join(logDir, `${baseName}.log`);
if (fs.existsSync(currentLogPath)) {
const stats = fs.statSync(currentLogPath);
const timestamp = stats.birthtime.toISOString().replace(/[:.]/g, '-');
const newLogPath = path.join(logDir, `${baseName}_${timestamp}.log`);
fs.renameSync(currentLogPath, newLogPath);
}
};
16 changes: 16 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as net from 'net';
import * as fsPromises from 'node:fs/promises';
import path from 'node:path';
import fs from 'fs';

export async function pathAccessible(path: string): Promise<boolean> {
try {
Expand Down Expand Up @@ -38,3 +39,18 @@ export function findAvailablePort(host: string, startPort: number, endPort: numb
tryPort(startPort);
});
}

/**
* Rotate old log files by adding a timestamp to the end of the file.
* @param logDir The directory to rotate the logs in.
* @param baseName The base name of the log file.
*/
export function rotateLogFiles(logDir: string, baseName: string) {
const currentLogPath = path.join(logDir, `${baseName}.log`);
if (fs.existsSync(currentLogPath)) {
const stats = fs.statSync(currentLogPath);
const timestamp = stats.birthtime.toISOString().replace(/[:.]/g, '-');
const newLogPath = path.join(logDir, `${baseName}_${timestamp}.log`);
fs.renameSync(currentLogPath, newLogPath);
}
}

0 comments on commit 24ad996

Please sign in to comment.