优化 素材库新增处理优化,提速,其余bug处理

This commit is contained in:
Chen committed 2025-12-04 10:57:50 +08:00
1 parent ad774d8a22
commit 4bce6f5d72
67 files changed
+4861 -3111

No files matched your search

+19 -38
View File
@@ -19,14 +19,9 @@ class CleanupManager {
* Start routine cleanup
*/
startRoutineCleanup() {
if (this.isRunning) {
console.log('[CleanupManager] Routine cleanup already running');
return;
}
console.log('[CleanupManager] Starting routine cleanup');
console.log(` Interval: ${this.routineInterval}ms`);
if (this.isRunning) return;
console.log('🧹 清理管理器已启动');
this.isRunning = true;
this.routineIntervalId = setInterval(() => {
@@ -38,11 +33,9 @@ class CleanupManager {
* Stop routine cleanup
*/
stopRoutineCleanup() {
if (!this.isRunning) {
return;
}
if (!this.isRunning) return;
console.log('[CleanupManager] Stopping routine cleanup');
console.log('🧹 清理管理器已停止');
if (this.routineIntervalId) {
clearInterval(this.routineIntervalId);
@@ -81,11 +74,10 @@ class CleanupManager {
this.cleanupCount++;
this.lastCleanupTime = Date.now();
// 总是显示日志(帮助诊断)
console.log('[CleanupManager] Routine cleanup completed');
console.log(` Caches cleared: ${cachesCleared}`);
console.log(` Heap reclaimed: ${(memoryReclaimed / 1024 / 1024).toFixed(2)} MB`);
console.log(` RSS: ${rssBefore.toFixed(0)}MB → ${rssAfter.toFixed(0)}MB (${rssReclaimed > 0 ? '-' : '+'}${Math.abs(rssReclaimed).toFixed(0)}MB)`);
// 只在内存回收显著时输出日志
if (Math.abs(rssReclaimed) > 10) {
console.log(`🧹 内存清理: ${rssBefore.toFixed(0)}MB → ${rssAfter.toFixed(0)}MB`);
}
return {
memoryBefore,
@@ -95,7 +87,7 @@ class CleanupManager {
connectionsCleared: 0
};
} catch (error) {
console.error('[CleanupManager] Routine cleanup error:', error);
console.error('❌ 清理失败:', error.message);
return null;
}
}
@@ -107,7 +99,7 @@ class CleanupManager {
* - Force garbage collection multiple times (3+)
*/
executeEmergencyCleanup() {
console.log('[CleanupManager] 🚨 Executing EMERGENCY cleanup...');
console.log('🚨 执行紧急内存清理...');
const memoryBefore = this.getMemoryStats();
@@ -118,15 +110,15 @@ class CleanupManager {
try {
this.dbPool.closeAll();
connectionsCleared = 1;
console.log('[CleanupManager] ✓ Database connections closed');
console.log(' ✓ 数据库连接已关闭');
} catch (error) {
console.error('[CleanupManager] ✗ Failed to close connections:', error.message);
console.error(' ✗ 关闭连接失败:', error.message);
}
}
// 2. Clear all registered caches
const cachesCleared = this.clearAllCaches();
console.log(`[CleanupManager] ✓ ${cachesCleared} caches cleared`);
console.log(` ✓ 已清理 ${cachesCleared} 个缓存`);
// 3. Force garbage collection multiple times (minimum 3)
this.forceGarbageCollection(3);
@@ -134,10 +126,7 @@ class CleanupManager {
const memoryAfter = this.getMemoryStats();
const memoryReclaimed = memoryBefore.heapUsed - memoryAfter.heapUsed;
console.log('[CleanupManager] Emergency cleanup completed');
console.log(` Memory before: ${(memoryBefore.heapUsed / 1024 / 1024).toFixed(2)} MB`);
console.log(` Memory after: ${(memoryAfter.heapUsed / 1024 / 1024).toFixed(2)} MB`);
console.log(` Memory reclaimed: ${(memoryReclaimed / 1024 / 1024).toFixed(2)} MB`);
console.log(`✅ 紧急清理完成: 回收 ${(memoryReclaimed / 1024 / 1024).toFixed(0)}MB`);
return {
memoryBefore,
@@ -147,7 +136,7 @@ class CleanupManager {
connectionsCleared
};
} catch (error) {
console.error('[CleanupManager] Emergency cleanup error:', error);
console.error('❌ 紧急清理失败:', error.message);
return null;
}
}
@@ -157,23 +146,16 @@ class CleanupManager {
* @param {number} iterations - Number of GC iterations (minimum 3)
*/
forceGarbageCollection(iterations = 3) {
if (!global.gc) {
console.warn('[CleanupManager] Garbage collection not available (run with --expose-gc)');
return;
}
if (!global.gc) return;
const actualIterations = Math.max(3, iterations);
console.log(`[CleanupManager] Running GC ${actualIterations} times...`);
for (let i = 0; i < actualIterations; i++) {
try {
global.gc();
} catch (error) {
console.error(`[CleanupManager] GC iteration ${i + 1} failed:`, error.message);
console.error(` ✗ GC失败 (${i + 1}/${actualIterations}):`, error.message);
}
}
console.log('[CleanupManager] ✓ Garbage collection completed');
}
/**
@@ -190,7 +172,7 @@ class CleanupManager {
cleared++;
}
} catch (error) {
console.error(`[CleanupManager] Failed to clear cache "${name}":`, error.message);
console.error(`❌ 清理缓存失败 "${name}":`, error.message);
}
}
@@ -204,12 +186,11 @@ class CleanupManager {
*/
registerCache(name, cache) {
if (!cache || typeof cache.clear !== 'function') {
console.warn(`[CleanupManager] Cache "${name}" does not have a clear() method`);
console.warn(`⚠️ 缓存 "${name}" 无clear()方法`);
return;
}
this.caches.set(name, cache);
console.log(`[CleanupManager] Registered cache: ${name}`);
}
/**