|
1 | 1 | import { globalShortcut } from "electron"; |
2 | 2 | import { TranscriptionPluginManager } from "../plugins"; |
3 | | -import { DictationWindowService } from "../services/DictationWindowService"; |
4 | | -import { SettingsService } from "../services/SettingsService"; |
5 | | -import { TrayService } from "../services/TrayService"; |
6 | | -import { WindowManager } from "./WindowManager"; |
7 | 3 | import { promiseManager } from "./PromiseManager"; |
8 | | -import { PushToTalkManager } from "./PushToTalkManager"; |
| 4 | + |
| 5 | +export interface Cleanable { |
| 6 | + cleanup(): void | Promise<void>; |
| 7 | +} |
9 | 8 |
|
10 | 9 | export class CleanupManager { |
11 | | - private transcriptionPluginManager: TranscriptionPluginManager; |
12 | | - private dictationWindowService: DictationWindowService; |
13 | | - private settingsService: SettingsService; |
14 | | - private trayService: TrayService | null; |
15 | | - private windowManager: WindowManager; |
16 | 10 | private finishingTimeout: NodeJS.Timeout | null = null; |
17 | | - private pushToTalkManager: PushToTalkManager | null = null; |
18 | 11 |
|
19 | | - constructor( |
20 | | - transcriptionPluginManager: TranscriptionPluginManager, |
21 | | - dictationWindowService: DictationWindowService, |
22 | | - settingsService: SettingsService, |
23 | | - trayService: TrayService | null, |
24 | | - windowManager: WindowManager, |
25 | | - ) { |
26 | | - this.transcriptionPluginManager = transcriptionPluginManager; |
27 | | - this.dictationWindowService = dictationWindowService; |
28 | | - this.settingsService = settingsService; |
29 | | - this.trayService = trayService; |
30 | | - this.windowManager = windowManager; |
31 | | - } |
32 | | - |
33 | | - setPushToTalkManager(manager: PushToTalkManager | null): void { |
34 | | - this.pushToTalkManager = manager; |
35 | | - } |
| 12 | + constructor(private readonly cleanables: (Cleanable | null)[] = []) {} |
36 | 13 |
|
37 | 14 | setFinishingTimeout(timeout: NodeJS.Timeout | null): void { |
38 | 15 | this.finishingTimeout = timeout; |
39 | 16 | } |
40 | 17 |
|
41 | 18 | async cleanup(): Promise<void> { |
42 | | - console.log("=== Starting app cleanup ==="); |
| 19 | + console.log("=== Starting comprehensive app cleanup ==="); |
43 | 20 |
|
44 | 21 | const cleanupId = `app:cleanup:${Date.now()}`; |
45 | 22 | promiseManager.start(cleanupId); |
46 | 23 |
|
| 24 | + // Safety timeout to ensure app exits even if cleanup hangs |
47 | 25 | const cleanupTimeout = setTimeout(() => { |
48 | | - console.log("Cleanup timeout reached, forcing app quit..."); |
49 | | - promiseManager.reject(cleanupId, new Error("Cleanup timeout")); |
| 26 | + console.log("Cleanup timeout reached, forcing exit..."); |
50 | 27 | process.exit(0); |
51 | | - }, 10000); // Increased timeout to 10 seconds |
| 28 | + }, 10000); |
52 | 29 |
|
53 | 30 | try { |
54 | | - // Coordinate cleanup steps |
55 | | - await promiseManager.sequence([ |
56 | | - async () => { |
57 | | - console.log("Step 1: Stopping transcription..."); |
58 | | - await this.stopTranscription(); |
59 | | - return { step: "transcription", success: true }; |
60 | | - }, |
61 | | - async () => { |
62 | | - console.log("Step 2: Disabling push-to-talk hotkey..."); |
63 | | - this.disposePushToTalkHotkey(); |
64 | | - return { step: "push-to-talk", success: true }; |
65 | | - }, |
66 | | - async () => { |
67 | | - console.log("Step 3: Unregistering shortcuts..."); |
68 | | - this.unregisterShortcuts(); |
69 | | - return { step: "shortcuts", success: true }; |
70 | | - }, |
71 | | - async () => { |
72 | | - console.log("Step 4: Clearing timeouts..."); |
73 | | - this.clearTimeouts(); |
74 | | - return { step: "timeouts", success: true }; |
75 | | - }, |
76 | | - async () => { |
77 | | - console.log("Step 5: Cleaning up services..."); |
78 | | - this.cleanupServices(); |
79 | | - return { step: "services", success: true }; |
80 | | - }, |
81 | | - async () => { |
82 | | - console.log("Step 6: Closing windows..."); |
83 | | - this.closeWindows(); |
84 | | - return { step: "windows", success: true }; |
85 | | - }, |
86 | | - async () => { |
87 | | - console.log("Step 7: Waiting for graceful shutdown..."); |
88 | | - await new Promise((resolve) => setTimeout(resolve, 500)); |
89 | | - return { step: "graceful-wait", success: true }; |
90 | | - }, |
91 | | - async () => { |
92 | | - console.log("Step 8: Force closing remaining windows..."); |
93 | | - this.forceCloseRemainingWindows(); |
94 | | - return { step: "force-close", success: true }; |
95 | | - }, |
96 | | - async () => { |
97 | | - console.log("Step 9: Final cleanup..."); |
98 | | - await this.finalCleanup(); |
99 | | - return { step: "final", success: true }; |
100 | | - }, |
101 | | - ]); |
102 | | - |
103 | | - console.log("=== App cleanup completed successfully ==="); |
104 | | - promiseManager.resolve(cleanupId); |
105 | | - } catch (error) { |
106 | | - console.error("Error during cleanup:", error); |
107 | | - promiseManager.reject(cleanupId, error); |
108 | | - // Continue with cleanup even if there are errors |
109 | | - } finally { |
110 | | - clearTimeout(cleanupTimeout); |
111 | | - } |
112 | | - } |
113 | | - |
114 | | - private unregisterShortcuts(): void { |
115 | | - globalShortcut.unregisterAll(); |
116 | | - console.log("Global shortcuts unregistered"); |
117 | | - } |
118 | | - |
119 | | - private disposePushToTalkHotkey(): void { |
120 | | - if (this.pushToTalkManager) { |
121 | | - this.pushToTalkManager.dispose(); |
122 | | - this.pushToTalkManager = null; |
123 | | - console.log("Push-to-talk hotkey unregistered"); |
124 | | - } |
125 | | - } |
126 | | - |
127 | | - private async stopTranscription(): Promise<void> { |
128 | | - await this.transcriptionPluginManager.stopTranscription(); |
129 | | - console.log("Transcription stopped"); |
130 | | - } |
131 | | - |
132 | | - private closeWindows(): void { |
133 | | - this.dictationWindowService.cleanup(); |
134 | | - this.settingsService.cleanup(); |
135 | | - this.windowManager.closeModelManagerWindow(); |
136 | | - console.log("Windows closed"); |
137 | | - } |
138 | | - |
139 | | - private cleanupServices(): void { |
140 | | - // Best-effort async cleanup; don't await here to keep shutdown fast |
141 | | - void this.transcriptionPluginManager.cleanup(); |
142 | | - this.trayService?.destroy(); |
143 | | - console.log("Services cleaned up"); |
144 | | - } |
145 | | - |
146 | | - private clearTimeouts(): void { |
147 | | - if (this.finishingTimeout) { |
148 | | - clearTimeout(this.finishingTimeout); |
149 | | - this.finishingTimeout = null; |
150 | | - } |
151 | | - console.log("Timeouts cleared"); |
152 | | - } |
153 | | - |
154 | | - private forceCloseRemainingWindows(): void { |
155 | | - this.windowManager.forceCloseAllWindows(); |
156 | | - } |
157 | | - |
158 | | - private async finalCleanup(): Promise<void> { |
159 | | - try { |
160 | | - // Remove event listeners from services that extend EventEmitter |
161 | | - if (this.transcriptionPluginManager?.removeAllListeners) { |
162 | | - this.transcriptionPluginManager.removeAllListeners(); |
| 31 | + // Step 1: Clear application timeouts |
| 32 | + if (this.finishingTimeout) { |
| 33 | + clearTimeout(this.finishingTimeout); |
| 34 | + this.finishingTimeout = null; |
163 | 35 | } |
164 | 36 |
|
165 | | - if (this.dictationWindowService?.removeAllListeners) { |
166 | | - this.dictationWindowService.removeAllListeners(); |
| 37 | + // Step 2: Cleanup all other registered components and services |
| 38 | + console.log("Step 1: Cleaning up registered components and services..."); |
| 39 | + for (const component of this.cleanables) { |
| 40 | + try { |
| 41 | + if (component) { |
| 42 | + await component.cleanup(); |
| 43 | + } |
| 44 | + } catch (err) { |
| 45 | + console.error("Error cleaning up component:", err); |
| 46 | + } |
167 | 47 | } |
168 | 48 |
|
169 | | - // SettingsService doesn't extend EventEmitter, so it doesn't have removeAllListeners |
170 | | - // Its cleanup is handled by its own cleanup() method |
| 49 | + // Step 3: Final environment cleanup |
| 50 | + console.log("Step 2: Final environment cleanup..."); |
| 51 | + globalShortcut.unregisterAll(); |
171 | 52 |
|
172 | | - // Force garbage collection if available |
173 | | - if (typeof global !== "undefined" && global.gc) { |
174 | | - global.gc(); |
| 53 | + // Best-effort GC |
| 54 | + if (typeof global !== "undefined" && (global as any).gc) { |
| 55 | + (global as any).gc(); |
175 | 56 | } |
176 | 57 |
|
177 | | - console.log("All event listeners cleared and final cleanup completed"); |
| 58 | + console.log("=== App cleanup completed successfully ==="); |
| 59 | + promiseManager.resolve(cleanupId); |
178 | 60 | } catch (error) { |
179 | | - console.error("Error in final cleanup:", error); |
180 | | - // Don't throw - we want to continue with app shutdown |
| 61 | + console.error("Cleanup failed:", error); |
| 62 | + promiseManager.reject(cleanupId, error); |
| 63 | + } finally { |
| 64 | + clearTimeout(cleanupTimeout); |
181 | 65 | } |
182 | 66 | } |
183 | 67 | } |
0 commit comments