import { ServiceRegistry } from './ServiceRegistry' import { ServiceProvider } from './ServiceProvider' import { SystemServiceIntegration } from '../SystemServiceIntegration' import { ServiceContainerImpl } from './ServiceContainerImpl' /** * 系统启动器 * 负责初始化整个系统,包括服务容器、服务注册和启动核心服务 */ export class SystemBootstrapper { private static instance: SystemBootstrapper private serviceContainer: ServiceContainerImpl private systemService: SystemServiceIntegration | null = null private initialized = false private constructor() { this.serviceContainer = new ServiceContainerImpl() } /** * 获取系统启动器实例 */ public static getInstance(): SystemBootstrapper { if (!SystemBootstrapper.instance) { SystemBootstrapper.instance = new SystemBootstrapper() } return SystemBootstrapper.instance } /** * 启动整个系统 * @param config 系统配置参数 * @returns 启动成功返回true,失败返回false */ public async bootstrap(config?: { debug?: boolean autoCleanup?: boolean cleanupInterval?: number }): Promise { if (this.initialized) { console.warn('系统已经启动') return true } try { console.log('开始启动系统...', config) // 1. 初始化服务提供者 this.initializeServiceProvider() // 2. 初始化系统服务集成层 this.initializeSystemService(config || {}) // 3. 启动所有服务 await this.startAllServices() this.initialized = true console.log('系统启动成功') return true } catch (error) { console.error('系统启动失败:', error) return false } } /** * 初始化服务提供者 * 注册并初始化所有服务 */ private initializeServiceProvider(): void { console.log('初始化服务提供者...') // 初始化服务提供者,这会注册并初始化所有服务 ServiceProvider.initialize() console.log('服务提供者初始化完成') } /** * 初始化系统服务集成层 * 创建并配置SystemServiceIntegration实例 */ private initializeSystemService(config: { debug?: boolean autoCleanup?: boolean cleanupInterval?: number }): void { console.log('初始化系统服务集成层...') // 获取服务容器 const container = ServiceRegistry.getInstance().getContainer() // 创建系统服务集成实例,使用传入的配置 this.systemService = new SystemServiceIntegration(container, { debug: config.debug ?? true, autoCleanup: config.autoCleanup ?? true, cleanupInterval: config.cleanupInterval ?? 5 * 60 * 1000 // 默认5分钟 }) console.log('系统服务集成层初始化完成') } /** * 启动所有服务 */ private async startAllServices(): Promise { console.log('启动所有服务...') if (!this.systemService) { throw new Error('系统服务集成层未初始化') } // 初始化系统服务 await this.systemService.initialize() console.log('所有服务启动完成') } /** * 获取系统服务实例 */ public getSystemService(): SystemServiceIntegration | null { return this.systemService } /** * 检查系统是否已初始化 */ public isInitialized(): boolean { return this.initialized } /** * 关闭系统 */ public async shutdown(): Promise { if (!this.initialized) { console.warn('系统未启动') return } console.log('开始关闭系统...') try { // 关闭系统服务 if (this.systemService) { await this.systemService.shutdown() } // 重置状态 this.systemService = null this.initialized = false console.log('系统已关闭') } catch (error) { console.error('关闭系统时发生错误:', error) } } /** * 重启系统 */ public async restart(): Promise { console.log('重启系统...') try { await this.shutdown() await new Promise((resolve) => setTimeout(resolve, 1000)) return await this.bootstrap() } catch (error) { console.error('重启系统失败:', error) return false } } } /** * 导出系统启动器单例实例 */ export const systemBootstrapper = SystemBootstrapper.getInstance()