2024-08-02 19:48:38 +08:00
|
|
|
|
import { Job } from 'bullmq';
|
2024-12-30 08:26:40 +08:00
|
|
|
|
import { Logger } from '@nestjs/common';
|
2025-01-03 09:24:46 +08:00
|
|
|
|
import {
|
|
|
|
|
updateCourseLectureStats,
|
|
|
|
|
updateSectionLectureStats
|
|
|
|
|
} from '@server/models/lecture/utils';
|
|
|
|
|
import { ObjectType } from '@nicestack/common';
|
|
|
|
|
import {
|
|
|
|
|
updateCourseEnrollmentStats,
|
|
|
|
|
updateCourseReviewStats
|
|
|
|
|
} from '@server/models/course/utils';
|
|
|
|
|
import { QueueJobType } from '../types';
|
|
|
|
|
const logger = new Logger('QueueWorker');
|
|
|
|
|
export default async function processJob(job: Job<any, any, QueueJobType>) {
|
|
|
|
|
try {
|
|
|
|
|
if (job.name === QueueJobType.UPDATE_STATS) {
|
|
|
|
|
const { sectionId, courseId, type } = job.data;
|
|
|
|
|
// 处理 section 统计
|
|
|
|
|
if (sectionId) {
|
|
|
|
|
await updateSectionLectureStats(sectionId);
|
|
|
|
|
logger.debug(`Updated section stats for sectionId: ${sectionId}`);
|
|
|
|
|
}
|
|
|
|
|
// 如果没有 courseId,提前返回
|
|
|
|
|
if (!courseId) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 处理 course 相关统计
|
|
|
|
|
switch (type) {
|
|
|
|
|
case ObjectType.LECTURE:
|
|
|
|
|
await updateCourseLectureStats(courseId);
|
|
|
|
|
break;
|
|
|
|
|
case ObjectType.ENROLLMENT:
|
|
|
|
|
await updateCourseEnrollmentStats(courseId);
|
|
|
|
|
break;
|
|
|
|
|
case ObjectType.POST:
|
|
|
|
|
await updateCourseReviewStats(courseId);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
logger.warn(`Unknown update stats type: ${type}`);
|
|
|
|
|
}
|
2024-08-02 19:48:38 +08:00
|
|
|
|
|
2025-01-03 09:24:46 +08:00
|
|
|
|
logger.debug(`Updated course stats for courseId: ${courseId}, type: ${type}`);
|
|
|
|
|
}
|
2024-08-02 19:48:38 +08:00
|
|
|
|
|
2024-12-30 08:26:40 +08:00
|
|
|
|
|
2025-01-03 09:24:46 +08:00
|
|
|
|
} catch (error: any) {
|
|
|
|
|
logger.error(`Error processing stats update job: ${error.message}`, error.stack);
|
2024-12-30 08:26:40 +08:00
|
|
|
|
}
|
2024-08-02 19:48:38 +08:00
|
|
|
|
}
|