notifcation
This commit is contained in:
@@ -1,20 +1,23 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { InjectQueue } from '@nestjs/bullmq';
|
||||
import { Queue } from 'bullmq';
|
||||
import { NotificationQueueNameEnum } from '../constants/queue';
|
||||
import { NotificationQueueJob } from '../interfaces/notification-queue.interface';
|
||||
import { NotificationChannel } from '../entities/notification.entity';
|
||||
|
||||
@Injectable()
|
||||
export class NotificationQueueService {
|
||||
private readonly logger = new Logger(NotificationQueueService.name);
|
||||
|
||||
constructor(@InjectQueue('notification-queue') private readonly notificationQueue: Queue) {}
|
||||
constructor(
|
||||
@InjectQueue(NotificationQueueNameEnum.SMS) private readonly smsQueue: Queue,
|
||||
@InjectQueue(NotificationQueueNameEnum.PUSH) private readonly pushQueue: Queue,
|
||||
) {}
|
||||
|
||||
async addNotification(job: NotificationQueueJob): Promise<void> {
|
||||
async addSmsNotification(job: NotificationQueueJob): Promise<void> {
|
||||
try {
|
||||
const jobId = job.idempotencyKey || `${job.restaurantId}-${job.notificationType}-${job.channel}-${Date.now()}`;
|
||||
const jobId = job.idempotencyKey || `${job.restaurantId}-${job.title}-${Date.now()}`;
|
||||
|
||||
await this.notificationQueue.add('send-notification', job, {
|
||||
await this.smsQueue.add('sms-notification', job, {
|
||||
jobId,
|
||||
attempts: 3,
|
||||
backoff: {
|
||||
@@ -30,34 +33,60 @@ export class NotificationQueueService {
|
||||
},
|
||||
});
|
||||
|
||||
this.logger.log(`Notification job added to queue: ${jobId}`);
|
||||
this.logger.log(`SMS notification job added to queue: ${jobId}`);
|
||||
} catch (error) {
|
||||
this.logger.error(`Failed to add notification to queue:`, error);
|
||||
this.logger.error(`Failed to add SMS notification to queue:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async addBulkNotifications(jobs: NotificationQueueJob[]): Promise<void> {
|
||||
async addPushNotification(job: NotificationQueueJob): Promise<void> {
|
||||
try {
|
||||
const queueJobs = jobs.map(job => ({
|
||||
name: 'send-notification',
|
||||
data: job,
|
||||
opts: {
|
||||
jobId: job.idempotencyKey || `${job.restaurantId}-${job.notificationType}-${job.channel}-${Date.now()}`,
|
||||
attempts: 3,
|
||||
backoff: {
|
||||
type: 'exponential',
|
||||
delay: 2000,
|
||||
},
|
||||
},
|
||||
}));
|
||||
const jobId = job.idempotencyKey || `${job.restaurantId}-${job.title}-${Date.now()}`;
|
||||
|
||||
await this.notificationQueue.addBulk(queueJobs);
|
||||
this.logger.log(`Added ${jobs.length} notification jobs to queue`);
|
||||
await this.pushQueue.add('push-notification', job, {
|
||||
jobId,
|
||||
attempts: 3,
|
||||
backoff: {
|
||||
type: 'exponential',
|
||||
delay: 2000,
|
||||
},
|
||||
removeOnComplete: {
|
||||
age: 24 * 3600, // Keep completed jobs for 24 hours
|
||||
count: 1000,
|
||||
},
|
||||
removeOnFail: {
|
||||
age: 7 * 24 * 3600, // Keep failed jobs for 7 days
|
||||
},
|
||||
});
|
||||
|
||||
this.logger.log(`Push notification job added to queue: ${jobId}`);
|
||||
} catch (error) {
|
||||
this.logger.error(`Failed to add bulk notifications to queue:`, error);
|
||||
this.logger.error(`Failed to add push notification to queue:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// async addBulkNotifications(jobs: NotificationQueueJob[]): Promise<void> {
|
||||
// try {
|
||||
// const queueJobs = jobs.map(job => ({
|
||||
// name: 'send-notification',
|
||||
// data: job,
|
||||
// opts: {
|
||||
// jobId: job.idempotencyKey || `${job.restaurantId}-${job.title}-${Date.now()}`,
|
||||
// attempts: 3,
|
||||
// backoff: {
|
||||
// type: 'exponential',
|
||||
// delay: 2000,
|
||||
// },
|
||||
// },
|
||||
// }));
|
||||
|
||||
// await this.notificationQueue.addBulk(queueJobs);
|
||||
// this.logger.log(`Added ${jobs.length} notification jobs to queue`);
|
||||
// } catch (error) {
|
||||
// this.logger.error(`Failed to add bulk notifications to queue:`, error);
|
||||
// throw error;
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user