Skip to content

Commit

Permalink
(NOBIDS) BEDS-372: improve sendPushNotifications
Browse files Browse the repository at this point in the history
  • Loading branch information
guybrush committed Aug 28, 2024
1 parent 7ff322b commit b2968a0
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 deletions services/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,10 +482,10 @@ func dispatchNotifications(useDB *sqlx.DB) error {
return fmt.Errorf("error sending email notifications, err: %w", err)
}

// err = sendPushNotifications(useDB)
// if err != nil {
// return fmt.Errorf("error sending push notifications, err: %w", err)
// }
err = sendPushNotifications(useDB)
if err != nil {
return fmt.Errorf("error sending push notifications, err: %w", err)
}

err = sendWebhookNotifications(useDB)
if err != nil {
Expand Down Expand Up @@ -603,28 +603,40 @@ func sendPushNotifications(useDB *sqlx.DB) error {
logger.Infof("processing %v push notifications", len(notificationQueueItem))

batchSize := 500
for _, n := range notificationQueueItem {
for b := 0; b < len(n.Content.Messages); b += batchSize {
start := b
end := b + batchSize
if len(n.Content.Messages) < end {
end = len(n.Content.Messages)
}

err = notify.SendPushBatch(n.Content.Messages[start:end])
if err != nil {
metrics.Errors.WithLabelValues("notifications_send_push_batch").Inc()
logger.WithError(err).Error("error sending firebase batch job")
} else {
metrics.NotificationsSent.WithLabelValues("push", "200").Add(float64(len(n.Content.Messages)))
}
messages := []*messaging.Message{}
ids := []uint64{}

for i, n := range notificationQueueItem {
messages = append(messages, n.Content.Messages...)
ids = append(ids, n.Id)
if len(messages) >= batchSize || i == len(notificationQueueItem)-1 {
for b := 0; b < len(n.Content.Messages); b += batchSize {
start := b
end := b + batchSize
if len(messages) < end {
end = len(messages)
}

_, err = useDB.Exec(`UPDATE notification_queue SET sent = now() WHERE id = $1`, n.Id)
err = notify.SendPushBatch(messages[start:end])
if err != nil {
metrics.Errors.WithLabelValues("notifications_send_push_batch").Inc()
logger.WithError(err).Error("error sending firebase batch job")
} else {
// #TODO ask what this label means
metrics.NotificationsSent.WithLabelValues("push", "200").Add(float64(len(n.Content.Messages)))
}
}
_, err = useDB.Exec(`UPDATE notification_queue SET sent = now() WHERE id = any($1)`, ids)
if err != nil {
return fmt.Errorf("error updating sent status for push notification with id: %v, err: %w", n.Id, err)
return fmt.Errorf("error updating sent status for push notification for ids: %v, err: %w", ids, err)
}
// reset the slices
messages = messages[:0]
ids = ids[:0]
}
}

return nil
}

Expand Down

0 comments on commit b2968a0

Please sign in to comment.