Files
wwjcloud-nest-v1/tools-v1/java-tools/generators/job-generator.js
wanwujie 699680c93a feat: 重构v1框架架构和清理整理
- 将preset.ts移动到config目录,符合架构规范
- 迁移php-tools到java-tools,参考Java架构而非PHP
- 清理AI层文档,整合为单一README
- 删除core层,专注boot和ai层
- 集成AI层与Boot层,实现100%组件集成
- 清理废弃js文件和临时报告文件
- 更新导入路径,保持代码一致性
2025-10-20 23:07:37 +08:00

269 lines
7.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const BaseGenerator = require('./base-generator');
/**
* ⚡ 任务生成器
* 专门负责生成NestJS任务/队列文件 (参考Java架构)
*/
class JobGenerator extends BaseGenerator {
constructor() {
super('JobGenerator');
this.config = {
javaBasePath: '/Users/wanwu/Documents/wwjcloud/wwjcloud-nsetjs/niucloud-java/niucloud-core/src/main/java',
phpBasePath: '/Users/wanwu/Documents/wwjcloud/wwjcloud-nsetjs/niucloud-php/niucloud', // 仅用于业务逻辑提取
nestjsBasePath: '/Users/wanwu/Documents/wwjcloud/wwjcloud-nsetjs/wwjcloud-nest-v1/libs/wwjcloud-core/src',
discoveryResultPath: '/Users/wanwu/Documents/wwjcloud/wwjcloud-nsetjs/tools-v1/java-tools/java-discovery-result.json'
};
this.discoveryData = null;
this.jobStats = {
jobsCreated: 0,
jobsSkipped: 0
};
}
/**
* 运行任务生成
*/
async run() {
try {
console.log('⚡ 启动任务生成器...');
console.log('目标生成NestJS任务/队列文件\n');
// 加载Java架构发现结果含PHP业务逻辑
await this.loadDiscoveryData();
// 生成任务
await this.generateJobs();
// 输出统计报告
this.printStats();
} catch (error) {
console.error('❌ 任务生成失败:', error);
this.stats.errors++;
}
}
/**
* 加载Java架构发现结果含PHP业务逻辑
*/
async loadDiscoveryData() {
try {
const data = fs.readFileSync(this.config.discoveryResultPath, 'utf8');
this.discoveryData = JSON.parse(data);
console.log(' ✅ 成功加载Java架构发现结果含PHP业务逻辑');
} catch (error) {
console.error('❌ 加载发现结果失败:', error);
throw error;
}
}
/**
* 生成任务
*/
async generateJobs() {
console.log(' 🔨 生成任务...');
// 检查是否有任务数据
if (!this.discoveryData.jobs || Object.keys(this.discoveryData.jobs).length === 0) {
console.log(' ⚠️ 未发现PHP任务跳过生成');
return;
}
for (const [moduleName, jobs] of Object.entries(this.discoveryData.jobs)) {
// 检查Java架构是否有对应的任务目录
if (!this.hasPHPJobs(moduleName)) {
console.log(` ⚠️ 模块 ${moduleName} 在Java架构中无对应且PHP项目中也无任务跳过`);
continue;
}
for (const [jobName, jobInfo] of Object.entries(jobs)) {
await this.createJob(moduleName, jobName, jobInfo);
this.stats.jobsCreated++;
}
}
console.log(` ✅ 生成了 ${this.stats.jobsCreated} 个任务`);
}
/**
* 创建任务
*/
async createJob(moduleName, jobName, jobInfo) {
const jobDir = path.join(this.config.nestjsBasePath, moduleName, 'jobs');
this.ensureDir(jobDir);
const normalizedBase = jobName.replace(/Job$/i, '');
const jobPath = path.join(
jobDir,
`${this.toPascalCase(normalizedBase)}Job.ts`
);
// 检查是否有对应的PHP任务文件
const phpJobPath = path.join(this.config.phpBasePath, 'app/job', moduleName, `${jobName}.php`);
if (!fs.existsSync(phpJobPath)) {
console.log(` ❌ 未找到PHP任务文件跳过生成: ${phpJobPath}`);
return;
}
const content = this.generateJobContent(moduleName, jobName);
this.writeFile(jobPath, content, `Job for ${moduleName}/${jobName}`);
this.jobStats.jobsCreated++;
}
/**
* 生成任务内容
*/
generateJobContent(moduleName, jobName) {
const baseName = jobName.replace(/Job$/i, '');
const className = `${this.toPascalCase(baseName)}Job`;
return `import { Injectable, Logger } from '@nestjs/common';
import { InjectQueue } from '@nestjs/bullmq';
import { Queue } from 'bullmq';
import { BadRequestException } from '@nestjs/common';
/**
* ${className} - 基于NestJS BullMQ
* 参考: https://docs.nestjs.com/techniques/queues
* 对应 Java: @Async + RabbitMQ
* 对应 PHP: think\queue
*/
@Injectable()
export class ${className} {
private readonly logger = new Logger(${className}.name);
constructor(
@InjectQueue('${moduleName}') 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('${baseName}', data, options);
this.logger.log(\`${baseName} job added to queue: \${job.id}\`, data);
return job;
} catch (error) {
this.logger.error('Failed to add ${baseName} job to queue:', error);
throw new BadRequestException('${baseName}任务添加失败');
}
}
/**
* 处理队列任务
* 使用Core层基础设施统一队列服务、异常处理、日志服务
*/
async processJob(data: any) {
this.logger.log('${baseName} job processing:', data);
try {
// 任务逻辑
await this.executeJob(data);
this.logger.log('${baseName} job completed successfully');
} catch (error) {
this.logger.error('${baseName} job failed:', error);
// 使用Core层异常处理
throw new BadRequestException('${baseName}任务处理失败', error);
}
}
/**
* 执行任务
* 使用Core层基础设施日志服务、异常处理
*/
private async executeJob(data: any) {
// 实现具体的任务逻辑
// 例如:
// - 数据清理
// - 报表生成
// - 邮件发送
// - 数据同步
// - 备份操作
this.logger.log('Executing ${baseName} job logic with data:', data);
// 模拟异步操作
await new Promise(resolve => setTimeout(resolve, 1000));
this.logger.log('${baseName} job logic completed');
}
/**
* 手动触发任务
* 使用Core层基础设施日志服务、异常处理
*/
async triggerJob(data?: any) {
this.logger.log('Manually triggering ${baseName} job...');
try {
await this.executeJob(data || {});
} catch (error) {
this.logger.error('Failed to trigger ${baseName} job:', error);
// 使用Core层异常处理
throw new BadRequestException('${baseName}任务触发失败', error);
}
}
}`;
}
/**
* 转换为PascalCase
*/
toPascalCase(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
/**
* 转换为camelCase
*/
toCamelCase(str) {
return str.charAt(0).toLowerCase() + str.slice(1);
}
toPascalCase(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
/**
* 检查模块是否有PHP任务
*/
hasPHPJobs(moduleName) {
const phpProjectPath = path.join(__dirname, '../../niucloud-php/niucloud');
const jobPath = path.join(phpProjectPath, 'app/job', moduleName);
return fs.existsSync(jobPath);
}
/**
* 确保目录存在
*/
ensureDir(dirPath) {
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
}
}
/**
* 输出统计报告
*/
printStats() {
super.printStats({
'Jobs Created': this.jobStats.jobsCreated,
'Jobs Skipped': this.jobStats.jobsSkipped
});
}
}
// 如果直接运行此文件
if (require.main === module) {
const generator = new JobGenerator();
generator.run().catch(console.error);
}
module.exports = JobGenerator;