27 lines
1.0 KiB
TypeScript
27 lines
1.0 KiB
TypeScript
|
|
import { InjectQueue } from '@nestjs/bullmq';
|
||
|
|
import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
|
||
|
|
import { Queue } from 'bullmq';
|
||
|
|
import EventBus from '@server/utils/event-bus';
|
||
|
|
import { ObjectType, VisitType } from '@nice/common';
|
||
|
|
import { QueueJobType, updateVisitCountJobData } from '@server/queue/types';
|
||
|
|
|
||
|
|
@Injectable()
|
||
|
|
export class PostQueueService implements OnModuleInit {
|
||
|
|
private readonly logger = new Logger(PostQueueService.name);
|
||
|
|
constructor(@InjectQueue('general') private generalQueue: Queue) {}
|
||
|
|
onModuleInit() {
|
||
|
|
EventBus.on('updateVisitCount', ({ id, objectType, visitType }) => {
|
||
|
|
console.log('updateVisitCount');
|
||
|
|
if (objectType === ObjectType.POST) {
|
||
|
|
this.addUpdateVisitCountJob({ id, type: visitType });
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
async addUpdateVisitCountJob(data: updateVisitCountJobData) {
|
||
|
|
this.logger.log(`update post view count ${data.id}`);
|
||
|
|
await this.generalQueue.add(QueueJobType.UPDATE_POST_VISIT_COUNT, data, {
|
||
|
|
debounce: { id: data.id },
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|