feat: 关闭软件时清理 Worker

This commit is contained in:
digua
2026-01-12 20:24:22 +08:00
parent 46c54f7ca6
commit fe1a20759d
3 changed files with 21 additions and 5 deletions

View File

@@ -2,7 +2,7 @@ import { app, shell, BrowserWindow, protocol, nativeTheme } from 'electron'
import { join } from 'path'
import { optimizer, is, platform } from '@electron-toolkit/utils'
import { checkUpdate } from './update'
import mainIpcMain from './ipcMain'
import mainIpcMain, { cleanup } from './ipcMain'
import { initAnalytics, trackDailyActive } from './analytics'
import { initProxy } from './network/proxy'
import { needsLegacyMigration, migrateFromLegacyDir, ensureAppDirs } from './paths'
@@ -207,6 +207,11 @@ class MainProcess {
// @ts-ignore
app.isQuiting = true
})
// 退出前清理资源
app.on('will-quit', () => {
cleanup()
})
})
}

View File

@@ -29,7 +29,7 @@ function clearTempDbCache(filePath: string): void {
/**
* 清理所有缓存(删除所有临时数据库)
*/
function clearAllTempDbCache(): void {
export function cleanupTempDbs(): void {
for (const tempDbPath of tempDbCache.values()) {
deleteTempDatabase(tempDbPath)
}
@@ -125,9 +125,8 @@ export function registerMergeHandlers(ctx: IpcContext): void {
if (filePath) {
clearTempDbCache(filePath)
} else {
clearAllTempDbCache()
cleanupTempDbs()
}
return true
})
}

View File

@@ -8,7 +8,7 @@ import type { IpcContext } from './ipc/types'
// 导入各功能模块
import { registerWindowHandlers } from './ipc/window'
import { registerChatHandlers } from './ipc/chat'
import { registerMergeHandlers, initMergeModule } from './ipc/merge'
import { registerMergeHandlers, initMergeModule, cleanupTempDbs } from './ipc/merge'
import { registerAIHandlers } from './ipc/ai'
import { registerMessagesHandlers } from './ipc/messages'
import { registerCacheHandlers } from './ipc/cache'
@@ -50,4 +50,16 @@ const mainIpcMain = (win: BrowserWindow) => {
console.log('[IpcMain] All IPC handlers registered successfully')
}
export const cleanup = () => {
console.log('[IpcMain] Cleaning up resources...')
try {
// 关闭 Worker
worker.closeWorker()
// 清理临时数据库
cleanupTempDbs()
} catch (error) {
console.error('[IpcMain] Error during cleanup:', error)
}
}
export default mainIpcMain