Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:alarm cronjob #220

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

Check failure on line 1 in server/backend/cron/alarm.go

View workflow job for this annotation

GitHub Actions / lint

: # github.com/TUM-Dev/Campus-Backend/server/backend/cron [github.com/TUM-Dev/Campus-Backend/server/backend/cron.test]

//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.Devices) error {
log.Trace("sendGCMNotification")
return nil
}

func notifyAllUsers(db *gorm.DB, callbacks ...func(*[]model.Devices) 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.Devices
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
}
7 changes: 5 additions & 2 deletions server/backend/cron/cronjobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ const (
CanteenHeadcount = "canteenHeadCount"
MovieType = "movie"
FeedbackEmail = "feedbackEmail"
AlarmType = "alarm"

/* MensaType = "mensa"
AlarmType = "alarm" */
// MensaType = "mensa"
)

func New(db *gorm.DB) *CronService {
Expand All @@ -57,6 +57,7 @@ func (c *CronService) Run() error {
CanteenHeadcount,
MovieType,
FeedbackEmail,
AlarmType,
).
Scan(&res)

Expand Down Expand Up @@ -96,6 +97,8 @@ func (c *CronService) Run() error {
g.Go(func() error { return c.canteenHeadCountCron() })
case FeedbackEmail:
g.Go(func() error { return c.feedbackEmailCron() })
case AlarmType:
g.Go(func() error { return c.alarmCron() })
}
}

Expand Down
Loading