You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.2 KiB

  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package eventsource
  5. import (
  6. "context"
  7. "time"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/graceful"
  10. "code.gitea.io/gitea/modules/log"
  11. "code.gitea.io/gitea/modules/setting"
  12. "code.gitea.io/gitea/modules/timeutil"
  13. )
  14. // Init starts this eventsource
  15. func (m *Manager) Init() {
  16. go graceful.GetManager().RunWithShutdownContext(m.Run)
  17. }
  18. // Run runs the manager within a provided context
  19. func (m *Manager) Run(ctx context.Context) {
  20. then := timeutil.TimeStampNow().Add(-2)
  21. timer := time.NewTicker(setting.UI.Notification.EventSourceUpdateTime)
  22. loop:
  23. for {
  24. select {
  25. case <-ctx.Done():
  26. timer.Stop()
  27. break loop
  28. case <-timer.C:
  29. now := timeutil.TimeStampNow().Add(-2)
  30. uidCounts, err := models.GetUIDsAndNotificationCounts(then, now)
  31. if err != nil {
  32. log.Error("Unable to get UIDcounts: %v", err)
  33. }
  34. for _, uidCount := range uidCounts {
  35. m.SendMessage(uidCount.UserID, &Event{
  36. Name: "notification-count",
  37. Data: uidCount,
  38. })
  39. }
  40. then = now
  41. }
  42. }
  43. m.UnregisterAll()
  44. }