refactor: streamline log clearing process and enhance error handling in DebugContext and DebugService

This commit is contained in:
Julien Froidefond
2025-10-07 21:12:10 +02:00
parent df6a30b226
commit 7da4439b8c
3 changed files with 55 additions and 13 deletions

View File

@@ -234,12 +234,54 @@ export class DebugService {
try {
const userId = await this.getCurrentUserId();
const filePath = this.getLogFilePath(userId);
await this.writeLogs(filePath, []);
await this.clearFile(filePath);
} catch (error) {
if (error instanceof AppError) throw error;
}
}
private static async clearFile(filePath: string): Promise<void> {
try {
// Obtenir la queue existante ou créer une nouvelle
const existingQueue = this.writeQueues.get(filePath);
// Créer une nouvelle promesse qui attend la queue précédente
const newQueue = existingQueue
? existingQueue.then(() => this.performClear(filePath))
: this.performClear(filePath);
// Mettre à jour la queue
this.writeQueues.set(filePath, newQueue);
try {
await newQueue;
} finally {
// Nettoyer la queue si c'est la dernière opération
if (this.writeQueues.get(filePath) === newQueue) {
this.writeQueues.delete(filePath);
}
}
} catch (error) {
console.error(`Erreur lors du vidage du fichier ${filePath}:`, error);
}
}
private static async performClear(filePath: string): Promise<void> {
try {
// Créer une sauvegarde avant de vider
try {
await fs.copyFile(filePath, filePath + '.backup');
} catch {
// Ignorer si le fichier n'existe pas encore
}
// Écrire un tableau vide pour vider le fichier
await fs.writeFile(filePath, JSON.stringify([], null, 2), { flag: 'w' });
} catch (error) {
console.error(`Erreur lors du vidage du fichier ${filePath}:`, error);
}
}
static async logPageRender(page: string, duration: number) {
try {
if (!(await this.isDebugEnabled())) return;