// src/main/lifecycle.ts — 종료 시퀀스 // // 종료 시 정리 대상은 ShutdownRegistry 에 서비스가 스스로 등록한다. lifecycle 은 개별 서비스를 // import 하지 않고 등록부만 비운다. setupLifecycle 은 멱등이다 — bootstrap 이 가장 먼저 불러 // 초기화 도중의 app.quit()(치명 단계 실패 등)도 isQuitting 을 세우게 한다(그래야 창의 // close-to-tray 가 종료를 가로채지 않는다). index.ts 의 뒤늦은 호출은 아무것도 하지 않는다. import { app } from 'electron' import { getLogger } from './services/LoggerService' import { getConfigService } from './services/ConfigService' import { getShutdownRegistry } from './shutdown-registry' const logger = getLogger('lifecycle') /** 종료 정리 전체 상한 — STT 사이드카 /shutdown 요청(최대 3초) + SIGKILL 여유 */ export const SHUTDOWN_TIMEOUT_MS = 4000 let isQuitting = false let lifecycleInstalled = false let shutdownComplete = false export function getIsQuitting(): boolean { return isQuitting } export function setIsQuitting(value: boolean): void { isQuitting = value } export function setupLifecycle(): void { if (lifecycleInstalled) return lifecycleInstalled = true app.on('before-quit', () => { logger.info('[lifecycle] before-quit') isQuitting = true }) app.on('will-quit', (event) => { if (shutdownComplete) return // 첫 will-quit 에서만 종료를 미루고 등록된 자원을 정리한 뒤 다시 종료한다 event.preventDefault() logger.info('[lifecycle] will-quit — disposing registered resources') void getShutdownRegistry() .disposeAll(SHUTDOWN_TIMEOUT_MS) .then((report) => { logger.info( `[lifecycle] shutdown: completed=${report.completed.length}, failed=${report.failed.join(',') || '-'}, timedOut=${report.timedOut.join(',') || '-'}`, ) }) .catch((error: unknown) => { logger.warn('[lifecycle] shutdown registry failed:', error) }) .finally(() => { try { const configService = getConfigService() if (configService) { // electron-store는 동기적으로 디스크에 쓰므로 별도 flush 불필요 logger.info('[lifecycle] config flush complete') } } catch (error) { logger.warn('[lifecycle] config flush failed:', error) } shutdownComplete = true app.quit() }) }) }