feat(platform): Implement platform infrastructure with cloud-native support
- Add storage service (LocalAdapter + OSSAdapter stub) - Add database connection pool with graceful shutdown - Add logging system with winston (JSON format) - Add environment config management - Add async job queue (MemoryQueue + DatabaseQueue stub) - Add cache service (MemoryCache + RedisCache stub) - Add health check endpoints for SAE - Add monitoring metrics for DB, memory, API Key Features: - Zero-code switching between local and cloud environments - Adapter pattern for multi-environment support - Backward compatible with legacy modules - Ready for Aliyun Serverless deployment Related: Platform Infrastructure Planning (docs/09-鏋舵瀯瀹炴柦/04-骞冲彴鍩虹璁炬柦瑙勫垝.md)
This commit is contained in:
82
backend/src/common/jobs/JobFactory.ts
Normal file
82
backend/src/common/jobs/JobFactory.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
import { JobQueue } from './types.js'
|
||||
import { MemoryQueue } from './MemoryQueue.js'
|
||||
|
||||
/**
|
||||
* 任务队列工厂类
|
||||
*
|
||||
* 根据环境变量自动选择队列实现:
|
||||
* - QUEUE_TYPE=memory: 使用MemoryQueue(内存队列)
|
||||
* - QUEUE_TYPE=database: 使用DatabaseQueue(数据库队列,待实现)
|
||||
*
|
||||
* 零代码切换:
|
||||
* - 本地开发:不配置QUEUE_TYPE,默认使用memory
|
||||
* - 云端部署:配置QUEUE_TYPE=database(多实例共享)
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* import { jobQueue } from '@/common/jobs'
|
||||
*
|
||||
* // 业务代码不关心是memory还是database
|
||||
* const job = await jobQueue.push('asl:screening', { projectId: 123 })
|
||||
* ```
|
||||
*/
|
||||
export class JobFactory {
|
||||
private static instance: JobQueue | null = null
|
||||
|
||||
/**
|
||||
* 获取任务队列实例(单例模式)
|
||||
*/
|
||||
static getInstance(): JobQueue {
|
||||
if (!this.instance) {
|
||||
this.instance = this.createQueue()
|
||||
}
|
||||
return this.instance
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建任务队列
|
||||
*/
|
||||
private static createQueue(): JobQueue {
|
||||
const queueType = process.env.QUEUE_TYPE || 'memory'
|
||||
|
||||
switch (queueType) {
|
||||
case 'memory':
|
||||
return this.createMemoryQueue()
|
||||
|
||||
case 'database':
|
||||
// TODO: 实现DatabaseQueue
|
||||
console.warn('[JobFactory] DatabaseQueue not implemented yet, fallback to MemoryQueue')
|
||||
return this.createMemoryQueue()
|
||||
|
||||
default:
|
||||
console.warn(`[JobFactory] Unknown QUEUE_TYPE: ${queueType}, fallback to memory`)
|
||||
return this.createMemoryQueue()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建内存队列
|
||||
*/
|
||||
private static createMemoryQueue(): MemoryQueue {
|
||||
console.log('[JobFactory] Using MemoryQueue')
|
||||
|
||||
const queue = new MemoryQueue()
|
||||
|
||||
// 定期清理已完成的任务(避免内存泄漏)
|
||||
if (process.env.NODE_ENV !== 'test') {
|
||||
setInterval(() => {
|
||||
queue.cleanup()
|
||||
}, 60 * 60 * 1000) // 每小时清理一次
|
||||
}
|
||||
|
||||
return queue
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置实例(用于测试)
|
||||
*/
|
||||
static reset(): void {
|
||||
this.instance = null
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user