-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
handle failed child notification jobs
- Loading branch information
Showing
4 changed files
with
92 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { | ||
InjectQueue, | ||
OnQueueEvent, | ||
QueueEventsHost, | ||
QueueEventsListener, | ||
} from '@nestjs/bullmq'; | ||
import { BeforeApplicationShutdown, Injectable, Logger } from '@nestjs/common'; | ||
import { Job, Queue } from 'bullmq'; | ||
import { pullAll } from 'lodash'; | ||
import { NotificationsService } from 'src/api/notifications/notifications.service'; | ||
import { | ||
CommonService, | ||
NotificationDispatchStatusField, | ||
} from 'src/common/common.service'; | ||
import { AppConfigService } from 'src/config/app-config.service'; | ||
|
||
@QueueEventsListener('n') | ||
@Injectable() | ||
export class NotificationEventsListener | ||
extends QueueEventsHost | ||
implements BeforeApplicationShutdown | ||
{ | ||
readonly logger = new Logger(NotificationEventsListener.name); | ||
private readonly appConfig; | ||
private get guaranteedBroadcastPushDispatchProcessing() { | ||
return this.appConfig.notification | ||
?.guaranteedBroadcastPushDispatchProcessing; | ||
} | ||
private get broadcastSubscriberChunkSize() { | ||
return this.appConfig.notification?.broadcastSubscriberChunkSize; | ||
} | ||
|
||
constructor( | ||
appConfigService: AppConfigService, | ||
private readonly notificationsService: NotificationsService, | ||
private readonly commonService: CommonService, | ||
@InjectQueue('n') private notificationQueue: Queue, | ||
) { | ||
super(); | ||
this.appConfig = appConfigService.get(); | ||
} | ||
|
||
// failed listener marks all candidates in the chunk failed if | ||
// guaranteedBroadcastPushDispatchProcessing is false | ||
@OnQueueEvent('failed') | ||
async onFailed({ jobId }) { | ||
const job = await Job.fromId(this.notificationQueue, jobId); | ||
if (this.guaranteedBroadcastPushDispatchProcessing || job?.name !== 'c') { | ||
job.remove(); | ||
return; | ||
} | ||
const notification = await this.notificationsService.findOne( | ||
{ | ||
where: { id: job.data.id }, | ||
}, | ||
null, | ||
); | ||
const startIdx = job.data.s; | ||
const subChunk = (notification.dispatch.candidates as string[]).slice( | ||
startIdx, | ||
startIdx + this.broadcastSubscriberChunkSize, | ||
); | ||
pullAll( | ||
subChunk, | ||
(notification.dispatch?.failed ?? []).map((e: any) => e.subscriptionId), | ||
); | ||
await this.commonService.updateBroadcastPushNotificationStatus( | ||
notification, | ||
NotificationDispatchStatusField.failed, | ||
subChunk.map((e) => ({ | ||
subscriptionId: e, | ||
})), | ||
null, | ||
); | ||
job.remove(); | ||
} | ||
|
||
async beforeApplicationShutdown() { | ||
await this.queueEvents.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters