From 33904f9930b30801ce91bfb541236cf20b2351b0 Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Thu, 7 Sep 2023 21:53:13 +0200 Subject: [PATCH 1/3] added an alarm cronjob --- server/backend/cron/alarm.go | 61 ++++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/server/backend/cron/alarm.go b/server/backend/cron/alarm.go index aa6cf3f1..f92870af 100644 --- a/server/backend/cron/alarm.go +++ b/server/backend/cron/alarm.go @@ -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 } From 9270fa0331c3c09637371d2d8f7579e75d5ef05d Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Wed, 13 Sep 2023 19:33:10 +0200 Subject: [PATCH 2/3] typo --- server/backend/cron/alarm.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/server/backend/cron/alarm.go b/server/backend/cron/alarm.go index f92870af..7162878b 100644 --- a/server/backend/cron/alarm.go +++ b/server/backend/cron/alarm.go @@ -10,12 +10,12 @@ func (c *CronService) alarmCron() error { return notifyAllUsers(c.db, sendGCMNotification) } -func sendGCMNotification(*[]model.Device) error { +func sendGCMNotification(*[]model.Devices) error { log.Trace("sendGCMNotification") return nil } -func notifyAllUsers(db *gorm.DB, callbacks ...func(*[]model.Device) error) error { +func notifyAllUsers(db *gorm.DB, callbacks ...func(*[]model.Devices) error) error { var pendingNotifications []model.Notification if err := db. Joins("notification_confirmation"). @@ -27,7 +27,7 @@ func notifyAllUsers(db *gorm.DB, callbacks ...func(*[]model.Device) error) error } for _, pending := range pendingNotifications { // Get a few targets - var targets []model.Device + var targets []model.Devices if err := db. Distinct("device"). Where("notification = ?", pending.Notification). From f50ed82e1a136be265be163b6690596d45fad9d7 Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Sun, 22 Oct 2023 02:08:46 +0200 Subject: [PATCH 3/3] added the cronjob as a cronjob --- server/backend/cron/cronjobs.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/server/backend/cron/cronjobs.go b/server/backend/cron/cronjobs.go index 82d9b2a8..55b3579b 100644 --- a/server/backend/cron/cronjobs.go +++ b/server/backend/cron/cronjobs.go @@ -34,9 +34,9 @@ const ( NewExamResultsHook = "newExamResultsHook" MovieType = "movie" FeedbackEmail = "feedbackEmail" + AlarmType = "alarm" - /* MensaType = "mensa" - AlarmType = "alarm" */ + // MensaType = "mensa" ) func New(db *gorm.DB) *CronService { @@ -66,6 +66,7 @@ func (c *CronService) Run() error { NewExamResultsHook, MovieType, FeedbackEmail, + AlarmType, ). Scan(&res) @@ -111,6 +112,8 @@ func (c *CronService) Run() error { g.Go(func() error { return c.iosActivityReset() }) case FeedbackEmail: g.Go(func() error { return c.feedbackEmailCron() }) + case AlarmType: + g.Go(func() error { return c.alarmCron() }) } }