优化 素材库新增处理优化,提速,其余bug处理
This commit is contained in:
1 parent
ad774d8a22
commit
4bce6f5d72
67 files changed
+4861
-3111
No files matched your search
+90
-25
@@ -35,13 +35,13 @@ if (!FRONTEND_DIST) {
|
||||
for (const p of possiblePaths) {
|
||||
if (fs.existsSync(p)) {
|
||||
FRONTEND_DIST = p;
|
||||
console.log('✅ Found frontend at:', p);
|
||||
console.log('✅ 前端目录:', p);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!FRONTEND_DIST) {
|
||||
console.log('⚠️ Frontend not found, API-only mode');
|
||||
console.log('⚠️ 未找到前端,API模式');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,15 +89,18 @@ server.on('request', app);
|
||||
|
||||
// Socket.IO 连接处理
|
||||
io.on('connection', (socket) => {
|
||||
console.log('✅ Socket.IO client connected:', socket.id);
|
||||
console.log('✅ 客户端连接:', socket.id);
|
||||
|
||||
socket.on('disconnect', () => {
|
||||
console.log('❌ Socket.IO client disconnected:', socket.id);
|
||||
console.log('❌ 客户端断开:', socket.id);
|
||||
});
|
||||
});
|
||||
|
||||
// 启动内存监控
|
||||
const memoryMonitor = new MemoryMonitor();
|
||||
// 启动内存监控(开发模式:每30秒输出RSS)
|
||||
const memoryMonitor = new MemoryMonitor({
|
||||
devMode: true,
|
||||
devLogInterval: 30000 // 30秒
|
||||
});
|
||||
memoryMonitor.start();
|
||||
|
||||
// 启动清理管理器
|
||||
@@ -106,36 +109,95 @@ cleanupManager.startRoutineCleanup();
|
||||
|
||||
// 启动服务器
|
||||
server.listen(PORT, () => {
|
||||
console.log('\n🚀 FlyPic Server (New Architecture) Started');
|
||||
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
|
||||
console.log(`📡 Server running on http://localhost:${PORT}`);
|
||||
console.log(`🔌 Socket.IO ready`);
|
||||
console.log(`📁 Frontend: ${FRONTEND_DIST || 'Not found (API-only mode)'}`);
|
||||
console.log(`🏗️ Architecture: Config → Model → Service → Route`);
|
||||
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n');
|
||||
|
||||
// 为当前素材库启动文件监控
|
||||
console.log('\n🚀 FlyPic 服务器已启动');
|
||||
console.log(`📡 端口: ${PORT}`);
|
||||
console.log(`🔌 Socket.IO 就绪`);
|
||||
if (FRONTEND_DIST) console.log(`📁 前端: ${FRONTEND_DIST}`);
|
||||
console.log('');
|
||||
|
||||
try {
|
||||
const currentConfig = config.loadConfig();
|
||||
|
||||
// 恢复所有素材库的扫描状态
|
||||
if (currentConfig.libraries && currentConfig.libraries.length > 0) {
|
||||
scanManager.restoreAllStates(currentConfig.libraries);
|
||||
|
||||
// 检查是否有未完成的扫描,自动继续
|
||||
const activeStates = scanManager.getAllActiveStates();
|
||||
if (Object.keys(activeStates).length > 0) {
|
||||
console.log(`📊 发现 ${Object.keys(activeStates).length} 个活跃扫描`);
|
||||
}
|
||||
|
||||
for (const [libraryId, state] of Object.entries(activeStates)) {
|
||||
const lib = currentConfig.libraries.find(l => l.id === libraryId);
|
||||
if (lib && state.status === 'scanning') {
|
||||
console.log(`🔄 恢复扫描: ${lib.name} (${state.progress?.percent || 0}%)`);
|
||||
|
||||
// 立即恢复扫描状态(让前端能检测到)
|
||||
scanManager.scanStates.set(libraryId, {
|
||||
status: 'scanning',
|
||||
progress: state.progress || { current: 0, total: 0, percent: 0 },
|
||||
startTime: state.startTime || Date.now()
|
||||
});
|
||||
|
||||
// 立即向所有连接的客户端推送扫描状态
|
||||
io.emit('scanProgress', {
|
||||
libraryId,
|
||||
...state.progress,
|
||||
resuming: true
|
||||
});
|
||||
|
||||
// 延迟启动实际扫描,等服务完全准备好
|
||||
setTimeout(() => {
|
||||
const db = dbPool.acquire(lib.path);
|
||||
// 继续扫描(从中断处继续)
|
||||
scanner.scanLibrary(
|
||||
lib.path,
|
||||
db,
|
||||
(progress) => {
|
||||
io.emit('scanProgress', { libraryId, ...progress });
|
||||
},
|
||||
libraryId
|
||||
).then(() => {
|
||||
scanManager.completeScan(libraryId);
|
||||
io.emit('scanComplete', { libraryId });
|
||||
dbPool.release(lib.path);
|
||||
console.log(`✅ 扫描完成: ${lib.name}`);
|
||||
}).catch((err) => {
|
||||
console.error(`❌ 扫描失败: ${lib.name}`, err.message);
|
||||
scanManager.completeScan(libraryId);
|
||||
dbPool.release(lib.path);
|
||||
});
|
||||
}, 2000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 为当前素材库启动文件监控
|
||||
if (currentConfig.currentLibraryId) {
|
||||
const currentLib = currentConfig.libraries.find(lib => lib.id === currentConfig.currentLibraryId);
|
||||
if (currentLib) {
|
||||
lightweightWatcher.watch(currentLib.id, currentLib.path, currentLib.name, io);
|
||||
console.log(`📂 File watcher started for: ${currentLib.name}`);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('⚠️ Failed to start file watcher:', e.message);
|
||||
console.warn('⚠️ 初始化失败:', e.message);
|
||||
}
|
||||
});
|
||||
|
||||
// 标记是否正在关闭
|
||||
let isShuttingDown = false;
|
||||
|
||||
// 优雅关闭
|
||||
const shutdown = async () => {
|
||||
console.log('\n🛑 Shutting down gracefully...');
|
||||
if (isShuttingDown) return;
|
||||
isShuttingDown = true;
|
||||
|
||||
console.log('\n🛑 正在关闭服务器...');
|
||||
|
||||
// 停止接受新连接
|
||||
server.close(() => {
|
||||
console.log('✅ HTTP server closed');
|
||||
console.log('✅ HTTP 服务器已关闭');
|
||||
});
|
||||
|
||||
// 停止监控
|
||||
@@ -145,27 +207,30 @@ const shutdown = async () => {
|
||||
// 停止所有文件监控
|
||||
lightweightWatcher.stopAll();
|
||||
|
||||
// 等待扫描任务完成当前批次(最多等2秒)
|
||||
await new Promise(resolve => setTimeout(resolve, 2000));
|
||||
|
||||
// 关闭所有数据库连接
|
||||
dbPool.closeAll();
|
||||
|
||||
// 等待资源释放
|
||||
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||
|
||||
console.log('✅ Shutdown complete');
|
||||
console.log('✅ 关闭完成');
|
||||
process.exit(0);
|
||||
};
|
||||
|
||||
// 导出关闭状态供其他模块检查
|
||||
module.exports.isShuttingDown = () => isShuttingDown;
|
||||
|
||||
process.on('SIGTERM', shutdown);
|
||||
process.on('SIGINT', shutdown);
|
||||
|
||||
// 错误处理
|
||||
process.on('uncaughtException', (error) => {
|
||||
console.error('💥 Uncaught Exception:', error);
|
||||
console.error('💥 未捕获异常:', error.message);
|
||||
shutdown();
|
||||
});
|
||||
|
||||
process.on('unhandledRejection', (reason, promise) => {
|
||||
console.error('💥 Unhandled Rejection at:', promise, 'reason:', reason);
|
||||
console.error('💥 未处理的Promise拒绝:', reason);
|
||||
shutdown();
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user