2025-10-13 01:27:37 +08:00
|
|
|
|
import { Injectable, Logger } from '@nestjs/common';
|
|
|
|
|
|
import { InjectQueue } from '@nestjs/bullmq';
|
|
|
|
|
|
import { Queue } from 'bullmq';
|
2025-10-20 18:43:52 +08:00
|
|
|
|
import { BusinessException } from '@wwjCommon/exceptions/business.exception';
|
2025-10-13 01:27:37 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* SetMemberNoJob - 基于NestJS BullMQ
|
|
|
|
|
|
* 参考: https://docs.nestjs.com/techniques/queues
|
|
|
|
|
|
* 对应 Java: @Async + RabbitMQ
|
|
|
|
|
|
* 对应 PHP: thinkqueue
|
|
|
|
|
|
*/
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
|
export class SetMemberNoJob {
|
|
|
|
|
|
private readonly logger = new Logger(SetMemberNoJob.name);
|
|
|
|
|
|
|
|
|
|
|
|
constructor(@InjectQueue('member') private readonly queue: Queue) {}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 添加任务到队列 - 使用BullMQ标准API
|
|
|
|
|
|
* 参考: https://docs.nestjs.com/techniques/queues#producers
|
|
|
|
|
|
*/
|
|
|
|
|
|
async addJob(data: any, options?: any) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const job = await this.queue.add('SetMemberNo', data, options);
|
|
|
|
|
|
this.logger.log(`SetMemberNo job added to queue: ${job.id}`, data);
|
|
|
|
|
|
return job;
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
this.logger.error('Failed to add SetMemberNo job to queue:', error);
|
|
|
|
|
|
throw new BusinessException('SetMemberNo任务添加失败');
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 处理队列任务
|
|
|
|
|
|
* 使用Core层基础设施:统一队列服务、异常处理、日志服务
|
|
|
|
|
|
*/
|
|
|
|
|
|
async processJob(data: any) {
|
|
|
|
|
|
this.logger.log('SetMemberNo job processing:', data);
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
// 任务逻辑
|
|
|
|
|
|
await this.executeJob(data);
|
|
|
|
|
|
this.logger.log('SetMemberNo job completed successfully');
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
this.logger.error('SetMemberNo job failed:', error);
|
|
|
|
|
|
// 使用Core层异常处理
|
|
|
|
|
|
throw new BusinessException('SetMemberNo任务处理失败', error);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 执行任务
|
|
|
|
|
|
* 使用Core层基础设施:日志服务、异常处理
|
|
|
|
|
|
*/
|
|
|
|
|
|
private async executeJob(data: any) {
|
|
|
|
|
|
// 实现具体的任务逻辑
|
|
|
|
|
|
// 例如:
|
|
|
|
|
|
// - 数据清理
|
|
|
|
|
|
// - 报表生成
|
|
|
|
|
|
// - 邮件发送
|
|
|
|
|
|
// - 数据同步
|
|
|
|
|
|
// - 备份操作
|
|
|
|
|
|
|
|
|
|
|
|
this.logger.log('Executing SetMemberNo job logic with data:', data);
|
|
|
|
|
|
|
|
|
|
|
|
// 模拟异步操作
|
|
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
|
|
|
|
|
|
|
|
|
|
this.logger.log('SetMemberNo job logic completed');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 手动触发任务
|
|
|
|
|
|
* 使用Core层基础设施:日志服务、异常处理
|
|
|
|
|
|
*/
|
|
|
|
|
|
async triggerJob(data?: any) {
|
|
|
|
|
|
this.logger.log('Manually triggering SetMemberNo job...');
|
|
|
|
|
|
try {
|
|
|
|
|
|
await this.executeJob(data || {});
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
this.logger.error('Failed to trigger SetMemberNo job:', error);
|
|
|
|
|
|
// 使用Core层异常处理
|
|
|
|
|
|
throw new BusinessException('SetMemberNo任务触发失败', error);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|