Skip to content

Commit

Permalink
added an alarm cronjob
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Feb 7, 2024
1 parent 4ef9d18 commit 33904f9
Showing 1 changed file with 59 additions and 2 deletions.
61 changes: 59 additions & 2 deletions server/backend/cron/alarm.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,64 @@
package cron

//lint:ignore U1000 stub
import (
"github.com/TUM-Dev/Campus-Backend/server/model"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
)

func (c *CronService) alarmCron() error {
// TODO: implement
return notifyAllUsers(c.db, sendGCMNotification)
}

func sendGCMNotification(*[]model.Device) error {
log.Trace("sendGCMNotification")
return nil
}

func notifyAllUsers(db *gorm.DB, callbacks ...func(*[]model.Device) error) error {
var pendingNotifications []model.Notification
if err := db.
Joins("notification_confirmation").
Where("notification_confirmation.sent = ?", false).
Group("notification").
Find(&pendingNotifications).Error; err != nil {
log.WithError(err).Error("failed to get pending notifications from the db")
return err
}
for _, pending := range pendingNotifications {
// Get a few targets
var targets []model.Device
if err := db.
Distinct("device").
Where("notification = ?", pending.Notification).
Where("gcmStatus IS NULL").
Where("gcmToken IS NOT NULL").
Joins("notification_confirmation").
Where("notification_confirmation.sent = ?", false).
Limit(998).
Find(&targets).
Error; err != nil {
log.WithError(err).Error("failed to get devices which should receive the notification")
continue
}
if len(targets) > 0 {
for _, callback := range callbacks {
if err := callback(&targets); err != nil {
log.WithError(err).Error("callback failed")
continue
}
}
// mark as sent for these targets
if err := db.
Model(&model.NotificationConfirmation{}).
Where("notification = ?", pending.Notification).
Where("device IN ?", targets).
Update("sent", true).
Error; err != nil {
log.WithError(err).Error("failed to update notification_confirmation")
continue
}
}
}
return nil
}

0 comments on commit 33904f9

Please sign in to comment.