- 建立 pnpm workspace + Turborepo 单仓、ESLint 9/Prettier/commitlint/husky 门禁 - NestJS + Fastify API:统一错误体、trace_id、Zod 管道、/health 真实依赖探活、OpenAPI 3.1 契约 - Prisma 基线:outbox_events/idempotency_keys/system_configs 与首版迁移、幂等种子 - shared 契约包:错误码、权限点、ULID、游标分页、命名/时间工具 - ui 包与 Web 壳层:Semi + Remix 图标统一出口、Cloud Blue 视觉、登录/工作台/错误页 - worker 骨架、Playwright 冒烟、Gitea CI、OpenAPI 快照与 oasdiff 回退脚本 - 补充 A0 实施报告与本地开发说明
This commit is contained in:
1 parent
9c3ab9768a
commit
04f49690f4
155 files changed
+18402
-28
No files matched your search
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"name": "@lingxi/worker",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"dev": "tsx watch src/main.ts",
|
||||
"build": "tsc -p tsconfig.build.json",
|
||||
"start": "node dist/main.js",
|
||||
"typecheck": "tsc -p tsconfig.json --noEmit"
|
||||
},
|
||||
"dependencies": {
|
||||
"@lingxi/shared": "workspace:*",
|
||||
"dotenv": "18.0.9",
|
||||
"ioredis": "5.11.1",
|
||||
"pino": "10.4.0",
|
||||
"zod": "3.25.76"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "24.19.2",
|
||||
"tsx": "4.23.15",
|
||||
"typescript": "5.9.3"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
const envSchema = z.object({
|
||||
NODE_ENV: z.enum(['development', 'test', 'production']).default('development'),
|
||||
LOG_LEVEL: z.enum(['fatal', 'error', 'warn', 'info', 'debug', 'trace']).default('info'),
|
||||
REDIS_URL: z
|
||||
.string()
|
||||
.min(1)
|
||||
.refine(
|
||||
(value) => value.startsWith('redis://') || value.startsWith('rediss://'),
|
||||
'REDIS_URL 必须是 redis:// 连接串',
|
||||
),
|
||||
QUEUE: z.string().min(1).default('default'),
|
||||
WORKER_CONCURRENCY: z.coerce.number().int().min(1).max(64).default(2),
|
||||
});
|
||||
|
||||
export interface WorkerConfig {
|
||||
nodeEnv: 'development' | 'test' | 'production';
|
||||
logLevel: 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace';
|
||||
redisUrl: string;
|
||||
queue: string;
|
||||
concurrency: number;
|
||||
}
|
||||
|
||||
export function loadWorkerConfig(source: NodeJS.ProcessEnv = process.env): WorkerConfig {
|
||||
const result = envSchema.safeParse(source);
|
||||
if (!result.success) {
|
||||
const details = result.error.issues
|
||||
.map((issue) => `${issue.path.join('.') || '(root)'}: ${issue.message}`)
|
||||
.join('; ');
|
||||
throw new Error(`Worker 环境变量校验失败:${details}`);
|
||||
}
|
||||
return {
|
||||
nodeEnv: result.data.NODE_ENV,
|
||||
logLevel: result.data.LOG_LEVEL,
|
||||
redisUrl: result.data.REDIS_URL,
|
||||
queue: result.data.QUEUE,
|
||||
concurrency: result.data.WORKER_CONCURRENCY,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
import { existsSync } from 'node:fs';
|
||||
import { resolve } from 'node:path';
|
||||
|
||||
import { config as loadDotenvFile } from 'dotenv';
|
||||
import Redis from 'ioredis';
|
||||
import pino from 'pino';
|
||||
|
||||
import { loadWorkerConfig } from './config';
|
||||
|
||||
function loadDotenv(): void {
|
||||
for (const file of ['.env.local', '.env']) {
|
||||
const path = resolve(process.cwd(), file);
|
||||
if (existsSync(path)) {
|
||||
loadDotenvFile({ path });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function main(): Promise<void> {
|
||||
loadDotenv();
|
||||
const config = loadWorkerConfig();
|
||||
const logger = pino({ level: config.logLevel, base: { service: 'lingxi-worker' } });
|
||||
|
||||
const redis = new Redis(config.redisUrl, {
|
||||
lazyConnect: true,
|
||||
maxRetriesPerRequest: null,
|
||||
enableOfflineQueue: false,
|
||||
});
|
||||
redis.on('error', (error: Error) => {
|
||||
logger.error({ err: error }, 'redis connection error');
|
||||
});
|
||||
|
||||
await redis.connect();
|
||||
await redis.ping();
|
||||
logger.info({ queue: config.queue, concurrency: config.concurrency }, 'worker 已启动,等待任务');
|
||||
|
||||
// A0 只验证进程与依赖可用;BullMQ 消费者在 M1 批次接入(QUEUE 环境变量决定队列)
|
||||
const heartbeat = setInterval(() => {
|
||||
logger.debug({ queue: config.queue }, 'worker heartbeat');
|
||||
}, 30_000);
|
||||
heartbeat.unref();
|
||||
|
||||
const shutdown = async (signal: string): Promise<void> => {
|
||||
clearInterval(heartbeat);
|
||||
logger.info({ signal }, 'worker 正在退出');
|
||||
await redis.quit();
|
||||
};
|
||||
|
||||
process.on('SIGINT', () => {
|
||||
void shutdown('SIGINT');
|
||||
});
|
||||
process.on('SIGTERM', () => {
|
||||
void shutdown('SIGTERM');
|
||||
});
|
||||
}
|
||||
|
||||
void main().catch((error: unknown) => {
|
||||
process.stderr.write(
|
||||
`Worker 启动失败:${error instanceof Error ? (error.stack ?? error.message) : String(error)}\n`,
|
||||
);
|
||||
process.exitCode = 1;
|
||||
});
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"noEmit": false,
|
||||
"outDir": "dist",
|
||||
"rootDir": "src",
|
||||
"declaration": false,
|
||||
"declarationMap": false,
|
||||
"sourceMap": true
|
||||
},
|
||||
"include": ["src"]
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"module": "CommonJS",
|
||||
"moduleResolution": "Node",
|
||||
"lib": ["ES2022"],
|
||||
"types": ["node"],
|
||||
"noEmit": true
|
||||
},
|
||||
"include": ["src"]
|
||||
}
|
||||
Reference in new issue
Block a user