chore: update the processor of the notification

This commit is contained in:
Mahyargdz
2025-05-11 14:57:04 +03:30
parent 78d143fa85
commit e7c3d8105f
2 changed files with 120 additions and 89 deletions
+1 -1
View File
@@ -13,7 +13,7 @@ export function databaseConfigs(): TypeOrmModuleAsyncOptions {
username: configService.getOrThrow<string>("DB_USER"), username: configService.getOrThrow<string>("DB_USER"),
password: configService.getOrThrow<string>("DB_PASS"), password: configService.getOrThrow<string>("DB_PASS"),
autoLoadEntities: true, autoLoadEntities: true,
synchronize: configService.getOrThrow<string>("NODE_ENV") == "production" ? false : true, synchronize: configService.getOrThrow<string>("NODE_ENV") == "production" ? false : false,
logging: configService.getOrThrow<string>("NODE_ENV") == "production" ? false : true, logging: configService.getOrThrow<string>("NODE_ENV") == "production" ? false : true,
migrationsTableName: "typeorm_migrations", migrationsTableName: "typeorm_migrations",
migrationsRun: false, migrationsRun: false,
@@ -41,7 +41,12 @@ type NotificationJobData = {
| INewTicketNotificationData; | INewTicketNotificationData;
}; };
@Processor(NOTIFICATION.QUEUE_NAME, { concurrency: 5 }) @Processor(NOTIFICATION.QUEUE_NAME, {
concurrency: 5,
lockDuration: 30000, // 30 seconds lock duration
stalledInterval: 30000, // Check for stalled jobs every 30 seconds
maxStalledCount: 2, // Allow 2 stalls before failing
})
export class NotificationProcessor extends WorkerProcessor { export class NotificationProcessor extends WorkerProcessor {
protected readonly logger = new Logger(NotificationProcessor.name); protected readonly logger = new Logger(NotificationProcessor.name);
@@ -62,12 +67,21 @@ export class NotificationProcessor extends WorkerProcessor {
await queryRunner.connect(); await queryRunner.connect();
await queryRunner.startTransaction(); await queryRunner.startTransaction();
const heartbeat = setInterval(() => {
job.updateProgress(50);
}, 10000); // Update progress every 10 seconds
const { type, recipientId, data } = job.data; const { type, recipientId, data } = job.data;
try {
switch (type) { switch (type) {
// Admin Notifications // Admin Notifications
case NotifType.NEW_BLOG_COMMENT: case NotifType.NEW_BLOG_COMMENT:
await this.notificationsService.createNewBlogCommentNotification(recipientId, data as IBlogCommentNotificationData, queryRunner); await this.notificationsService.createNewBlogCommentNotification(
recipientId,
data as IBlogCommentNotificationData,
queryRunner,
);
break; break;
case NotifType.NEW_SERVICE_REVIEW: case NotifType.NEW_SERVICE_REVIEW:
await this.notificationsService.createNewServiceReviewNotification( await this.notificationsService.createNewServiceReviewNotification(
@@ -117,7 +131,11 @@ export class NotificationProcessor extends WorkerProcessor {
await this.notificationsService.createTicketNotification(recipientId, data as ITicketNotificationData, queryRunner); await this.notificationsService.createTicketNotification(recipientId, data as ITicketNotificationData, queryRunner);
break; break;
case NotifType.ASSIGN_TICKET: case NotifType.ASSIGN_TICKET:
await this.notificationsService.createAssignTicketNotificationForAdmin(recipientId, data as ITicketNotificationData, queryRunner); await this.notificationsService.createAssignTicketNotificationForAdmin(
recipientId,
data as ITicketNotificationData,
queryRunner,
);
break; break;
// Invoice Notifications // Invoice Notifications
@@ -125,7 +143,11 @@ export class NotificationProcessor extends WorkerProcessor {
await this.notificationsService.createInvoiceCreationNotification(recipientId, data as IInvoiceNotificationData, queryRunner); await this.notificationsService.createInvoiceCreationNotification(recipientId, data as IInvoiceNotificationData, queryRunner);
break; break;
case NotifType.BILL_INVOICE_REMINDER: case NotifType.BILL_INVOICE_REMINDER:
await this.notificationsService.createBillInvoiceReminderNotification(recipientId, data as IInvoiceNotificationData, queryRunner); await this.notificationsService.createBillInvoiceReminderNotification(
recipientId,
data as IInvoiceNotificationData,
queryRunner,
);
break; break;
case NotifType.BILL_INVOICE: case NotifType.BILL_INVOICE:
await this.notificationsService.createBillInvoiceNotification(recipientId, data as IInvoiceNotificationData, queryRunner); await this.notificationsService.createBillInvoiceNotification(recipientId, data as IInvoiceNotificationData, queryRunner);
@@ -150,7 +172,11 @@ export class NotificationProcessor extends WorkerProcessor {
await this.notificationsService.createPaymentReminderNotification(recipientId, data as IPaymentNotificationData, queryRunner); await this.notificationsService.createPaymentReminderNotification(recipientId, data as IPaymentNotificationData, queryRunner);
break; break;
case NotifType.PAYMENT_CANCELLATION: case NotifType.PAYMENT_CANCELLATION:
await this.notificationsService.createPaymentCancellationNotification(recipientId, data as IPaymentNotificationData, queryRunner); await this.notificationsService.createPaymentCancellationNotification(
recipientId,
data as IPaymentNotificationData,
queryRunner,
);
break; break;
default: default:
@@ -158,7 +184,12 @@ export class NotificationProcessor extends WorkerProcessor {
} }
await queryRunner.commitTransaction(); await queryRunner.commitTransaction();
clearInterval(heartbeat);
return true; return true;
} catch (error) {
clearInterval(heartbeat);
throw error;
}
} catch (error) { } catch (error) {
this.logger.error( this.logger.error(
`Failed to process notification: ${error instanceof Error ? error.message : "Unknown error"}`, `Failed to process notification: ${error instanceof Error ? error.message : "Unknown error"}`,