This commit is contained in:
117
scripts/triggerLessonDataFill.js
Normal file
117
scripts/triggerLessonDataFill.js
Normal file
@@ -0,0 +1,117 @@
|
||||
/**
|
||||
* Script để trigger Lesson Data Fill Worker
|
||||
* Chạy script này để bắt đầu xử lý tất cả lessons và tạo process_data tasks
|
||||
*/
|
||||
|
||||
const { Queue } = require('bullmq');
|
||||
const { connectionOptions } = require('../config/bullmq');
|
||||
|
||||
/**
|
||||
* Trigger worker để xử lý lessons
|
||||
*/
|
||||
async function triggerLessonDataFill(options = {}) {
|
||||
const {
|
||||
batchSize = 50,
|
||||
totalLessons = null, // null = process all
|
||||
filters = {}, // { lesson_content_type: 'vocabulary', chapter_id: 'xxx' }
|
||||
} = options;
|
||||
|
||||
const queue = new Queue('lesson-data-fill', {
|
||||
connection: connectionOptions,
|
||||
prefix: process.env.BULLMQ_PREFIX || 'vcb',
|
||||
});
|
||||
|
||||
try {
|
||||
let offset = 0;
|
||||
let jobsCreated = 0;
|
||||
let hasMore = true;
|
||||
|
||||
console.log('🚀 Starting lesson data fill process...');
|
||||
console.log('📋 Config:', { batchSize, totalLessons, filters });
|
||||
|
||||
while (hasMore) {
|
||||
// Kiểm tra nếu đã đạt giới hạn
|
||||
if (totalLessons && offset >= totalLessons) {
|
||||
console.log(`✅ Reached limit of ${totalLessons} lessons`);
|
||||
break;
|
||||
}
|
||||
|
||||
// Tạo job cho batch này
|
||||
const job = await queue.add(
|
||||
`process-lessons-batch-${offset}`,
|
||||
{
|
||||
batchSize,
|
||||
offset,
|
||||
filters,
|
||||
},
|
||||
{
|
||||
priority: 5,
|
||||
jobId: `lesson-fill-${offset}-${Date.now()}`,
|
||||
}
|
||||
);
|
||||
|
||||
console.log(`✅ Created job ${job.id} for offset ${offset}`);
|
||||
jobsCreated++;
|
||||
|
||||
// Chờ một chút để tránh quá tải
|
||||
await new Promise(resolve => setTimeout(resolve, 100));
|
||||
|
||||
offset += batchSize;
|
||||
|
||||
// Nếu không có totalLessons, sẽ dừng sau 1 job để kiểm tra hasMore
|
||||
if (!totalLessons) {
|
||||
// Worker sẽ báo hasMore trong result
|
||||
hasMore = false; // Tạm dừng, có thể chạy lại script để tiếp tục
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`\n✅ Process completed!`);
|
||||
console.log(`📊 Total jobs created: ${jobsCreated}`);
|
||||
console.log(`📊 Lessons range: 0 to ${offset}`);
|
||||
|
||||
await queue.close();
|
||||
process.exit(0);
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Error:', error.message);
|
||||
await queue.close();
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// Parse command line arguments
|
||||
const args = process.argv.slice(2);
|
||||
const options = {};
|
||||
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
const arg = args[i];
|
||||
|
||||
if (arg === '--batch-size' && args[i + 1]) {
|
||||
options.batchSize = parseInt(args[i + 1]);
|
||||
i++;
|
||||
}
|
||||
|
||||
if (arg === '--total' && args[i + 1]) {
|
||||
options.totalLessons = parseInt(args[i + 1]);
|
||||
i++;
|
||||
}
|
||||
|
||||
if (arg === '--type' && args[i + 1]) {
|
||||
options.filters = options.filters || {};
|
||||
options.filters.lesson_content_type = args[i + 1];
|
||||
i++;
|
||||
}
|
||||
|
||||
if (arg === '--chapter' && args[i + 1]) {
|
||||
options.filters = options.filters || {};
|
||||
options.filters.chapter_id = args[i + 1];
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
// Run
|
||||
console.log('═══════════════════════════════════════════════════════');
|
||||
console.log(' Lesson Data Fill Trigger Script');
|
||||
console.log('═══════════════════════════════════════════════════════\n');
|
||||
|
||||
triggerLessonDataFill(options);
|
||||
Reference in New Issue
Block a user