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.

manager_run.go 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. if setting.UI.Notification.EventSourceUpdateTime <= 0 {
  17. return
  18. }
  19. go graceful.GetManager().RunWithShutdownContext(m.Run)
  20. }
  21. // Run runs the manager within a provided context
  22. func (m *Manager) Run(ctx context.Context) {
  23. then := timeutil.TimeStampNow().Add(-2)
  24. timer := time.NewTicker(setting.UI.Notification.EventSourceUpdateTime)
  25. loop:
  26. for {
  27. select {
  28. case <-ctx.Done():
  29. timer.Stop()
  30. break loop
  31. case <-timer.C:
  32. now := timeutil.TimeStampNow().Add(-2)
  33. uidCounts, err := models.GetUIDsAndNotificationCounts(then, now)
  34. if err != nil {
  35. log.Error("Unable to get UIDcounts: %v", err)
  36. }
  37. for _, uidCount := range uidCounts {
  38. m.SendMessage(uidCount.UserID, &Event{
  39. Name: "notification-count",
  40. Data: uidCount,
  41. })
  42. }
  43. then = now
  44. }
  45. }
  46. m.UnregisterAll()
  47. }