fix(GOV-009): 完成脚手架审查整改
This commit is contained in:
@@ -2,35 +2,39 @@ import 'reflect-metadata';
|
||||
|
||||
import { type INestApplicationContext, type NestApplicationOptions } from '@nestjs/common';
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import path from 'node:path';
|
||||
|
||||
import { loadWorkerProcessConfig } from '../../../libs/config/src/load-process-env';
|
||||
import {
|
||||
toSafeRuntimeSummary,
|
||||
type WorkerRuntimeConfig,
|
||||
} from '../../../libs/config/src/runtime-config';
|
||||
import { waitForShutdownSignal } from '../../../libs/infrastructure/src/process-lifecycle';
|
||||
import { WorkerModule } from './worker.module';
|
||||
|
||||
/** Worker 引导参数确保模块只读取本进程已校验的最小配置。 */
|
||||
interface BootstrapWorkerOptions {
|
||||
config: WorkerRuntimeConfig;
|
||||
logger?: NestApplicationOptions['logger'];
|
||||
}
|
||||
|
||||
/** 启动无 HTTP 端口的 Worker 应用上下文,生命周期由命令入口持有。 */
|
||||
export async function bootstrapWorker(
|
||||
options: BootstrapWorkerOptions,
|
||||
): Promise<INestApplicationContext> {
|
||||
const app = await NestFactory.createApplicationContext(WorkerModule, {
|
||||
const app = await NestFactory.createApplicationContext(WorkerModule.register(options.config), {
|
||||
logger: options.logger,
|
||||
});
|
||||
app.enableShutdownHooks();
|
||||
return app;
|
||||
}
|
||||
|
||||
async function runWorker(): Promise<void> {
|
||||
const config = loadWorkerProcessConfig();
|
||||
console.info('Worker 进程启动配置', toSafeRuntimeSummary(config));
|
||||
await bootstrapWorker({ config });
|
||||
const app = await bootstrapWorker({ config });
|
||||
await waitForShutdownSignal(app);
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
if (process.argv[1] && path.resolve(process.argv[1]) === path.resolve(__filename)) {
|
||||
void runWorker();
|
||||
}
|
||||
|
||||
@@ -1,4 +1,18 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { type DynamicModule, Module } from '@nestjs/common';
|
||||
|
||||
import type { WorkerRuntimeConfig } from '../../../libs/config/src/runtime-config';
|
||||
|
||||
/** Worker 的最小权限配置令牌,仅包含采集、持久化、消息与 AI 处理所需配置。 */
|
||||
export const WORKER_RUNTIME_CONFIG = Symbol('WORKER_RUNTIME_CONFIG');
|
||||
|
||||
@Module({})
|
||||
export class WorkerModule {}
|
||||
export class WorkerModule {
|
||||
/** 将入口校验后的 Worker 配置注册进当前进程容器。 */
|
||||
static register(config: WorkerRuntimeConfig): DynamicModule {
|
||||
return {
|
||||
module: WorkerModule,
|
||||
providers: [{ provide: WORKER_RUNTIME_CONFIG, useValue: config }],
|
||||
exports: [WORKER_RUNTIME_CONFIG],
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user