import 'reflect-metadata'; import { type INestApplicationContext, type NestApplicationOptions } from '@nestjs/common'; import { NestFactory } from '@nestjs/core'; import path from 'node:path'; import { loadSchedulerProcessConfig } from '../../../libs/config/src/load-process-env'; import { toSafeRuntimeSummary, type SchedulerRuntimeConfig, } from '../../../libs/config/src/runtime-config'; import { waitForShutdownSignal } from '../../../libs/infrastructure/src/process-lifecycle'; import { SchedulerModule } from './scheduler.module'; /** Scheduler 引导参数确保模块只读取本进程已校验的最小配置。 */ interface BootstrapSchedulerOptions { config: SchedulerRuntimeConfig; logger?: NestApplicationOptions['logger']; } /** 启动无 HTTP 端口的 Scheduler 应用上下文,生命周期由命令入口持有。 */ export async function bootstrapScheduler( options: BootstrapSchedulerOptions, ): Promise { const app = await NestFactory.createApplicationContext(SchedulerModule.register(options.config), { logger: options.logger, }); return app; } async function runScheduler(): Promise { const config = loadSchedulerProcessConfig(); console.info('Scheduler 进程启动配置', toSafeRuntimeSummary(config)); const app = await bootstrapScheduler({ config }); await waitForShutdownSignal(app); } if (process.argv[1] && path.resolve(process.argv[1]) === path.resolve(__filename)) { void runScheduler(); }