Spaces:
Sleeping
Sleeping
Download src/classes/core/ServiceLoader.ts from lamsaoganh/teamta: direct link, hf CLI and curl.
- Browser
- Download file 5.33 kB
-
https://huggingface.co/spaces/lamsaoganh/teamta/resolve/main/src/classes/core/ServiceLoader.ts
- Command line
-
hf download hf://spaces/lamsaoganh/teamta/src/classes/core/ServiceLoader.ts
-
curl -L -o ServiceLoader.ts https://huggingface.co/spaces/lamsaoganh/teamta/resolve/main/src/classes/core/ServiceLoader.ts
5.33 kB
| /** | |
| * 🏭 SERVICE LOADER | |
| * | |
| * Responsible for auto-discovering và loading tất cả services vào ServiceRegistry. | |
| * Hỗ trợ comprehensive validation để đảm bảo services được implement đầy đủ. | |
| * | |
| * � PROJECT-BASED STRUCTURE: | |
| * Từ version mới, services được tổ chức theo projectId: | |
| * services/{projectId}/{messageType}/ | |
| * | |
| * Ví dụ: services/testService/WebsiteContentIdeaRequest/ | |
| * | |
| * �💡 Để thêm service mới: | |
| * 1. Tạo service theo hướng dẫn trong docs/CREATE_NEW_SERVICE.md | |
| * 2. Đặt trong thư mục services/{projectId}/{messageType}/ | |
| * 3. Import ServiceFactory của service đó | |
| * 4. Thêm factory vào serviceFactories array | |
| * | |
| * @see docs/CREATE_NEW_SERVICE.md | |
| */ | |
| import { IServiceRegistry } from '../../interfaces/core/IServiceRegistry'; | |
| import { ServiceConfigManager } from '../../config/ServiceConfig'; | |
| import { ServiceValidator } from './ServiceValidator'; | |
| import { ServiceFactoryType } from '../../interfaces/core/IServiceFactory'; | |
| import { AggregationCounterAPI } from './AggregationCounterAPI'; | |
| import { IWorkerRegistry } from '../../interfaces/core/IWorkerRegistry'; | |
| // Default | |
| import { ServiceFactory as DefaultServiceFactory } from '../services/Default/DefaultService/ServiceFactory'; | |
| // 📋 DANH SÁCH TẤT CẢ SERVICE FACTORIES | |
| // Cấu trúc: services/{projectId}/{messageType}/ServiceFactory | |
| // Thêm service factory mới vào đây khi tạo service mới | |
| const candidateFactories: Array<{ messageType: string; factory: ServiceFactoryType }> = [ | |
| { messageType: "DefaultService", factory: DefaultServiceFactory as ServiceFactoryType }, | |
| ]; | |
| export class ServiceLoader { | |
| /** | |
| * 🚀 Load tất cả services với validation | |
| * | |
| * Process: | |
| * 1. Print service configuration summary | |
| * 2. Load services từ factories | |
| * 3. Register vào ServiceRegistry | |
| * 4. Comprehensive validation | |
| * 5. Exit nếu có lỗi (strict mode) | |
| */ | |
| static loadAllServices( | |
| serviceRegistry: IServiceRegistry, | |
| aggregationCounterAPI: AggregationCounterAPI, | |
| workerRegistry: IWorkerRegistry, | |
| options?: { enabledMessageTypes?: string[] } | |
| ): void { | |
| console.log("🔄 Loading all services..."); | |
| // Validate centralized service configs first (fail-fast) | |
| const cfgValidation = ServiceConfigManager.validateAllConfigs(); | |
| if (!cfgValidation.valid) { | |
| console.error("🛑 SERVICE CONFIG VALIDATION ERRORS:"); | |
| cfgValidation.errors.forEach(err => console.error(` - ${err}`)); | |
| console.error("Fix SERVICE_CONFIGS in src/config/ServiceConfig.ts and restart."); | |
| process.exit(1); | |
| } | |
| // Print service configs để dev review | |
| ServiceConfigManager.printServicesSummary(); | |
| const enabledMessageTypes = options?.enabledMessageTypes && options.enabledMessageTypes.length > 0 | |
| ? new Set(options.enabledMessageTypes) | |
| : new Set(candidateFactories.map(c => c.messageType)); | |
| if (options?.enabledMessageTypes && options.enabledMessageTypes.length > 0) { | |
| console.log("🔧 ServiceLoader: enabled messageTypes from worker profile:", options.enabledMessageTypes.join(', ')); | |
| } | |
| const serviceFactories: ServiceFactoryType[] = candidateFactories | |
| .filter(c => enabledMessageTypes.has(c.messageType)) | |
| .map(c => c.factory) | |
| .filter(f => f && typeof (f as any).createServiceConfig === 'function'); | |
| const loadedConfigs: any[] = []; | |
| // Load từng service factory | |
| serviceFactories.forEach(factory => { | |
| try { | |
| // Tất cả service factories đều nhận dependencies object | |
| const config = factory.createServiceConfig({ | |
| aggregationCounterAPI, | |
| workerRegistry | |
| }); | |
| serviceRegistry.registerService(config); | |
| loadedConfigs.push(config); | |
| } catch (error) { | |
| console.error(`❌ Failed to load service from factory:`, error); | |
| } | |
| }); | |
| console.log(`✅ Loaded ${serviceRegistry.getAllServices().length} services`); | |
| // 🔍 COMPREHENSIVE VALIDATION | |
| // Kiểm tra tất cả services đã được implement đầy đủ chưa | |
| console.log("\n🔍 Starting service validation..."); | |
| const validator = new ServiceValidator(); | |
| const validationReport = validator.validateAllServices(loadedConfigs); | |
| // Strict mode: Exit nếu có service invalid | |
| if (validationReport.invalidServices > 0) { | |
| const shouldContinue = process.env.IGNORE_SERVICE_VALIDATION === 'true'; | |
| if (!shouldContinue) { | |
| console.error("🛑 STARTUP ABORTED: Invalid services detected!"); | |
| console.error("Fix the errors above or set IGNORE_SERVICE_VALIDATION=true to bypass (not recommended)"); | |
| process.exit(1); | |
| } else { | |
| console.warn("⚠️ WARNING: Continuing despite invalid services (IGNORE_SERVICE_VALIDATION=true)"); | |
| } | |
| } | |
| } | |
| } |