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

manager_run.go 1.7 kB

3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. "code.gitea.io/gitea/services/reward"
  7. "context"
  8. "time"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/graceful"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/setting"
  13. "code.gitea.io/gitea/modules/timeutil"
  14. )
  15. // Init starts this eventsource
  16. func (m *Manager) Init() {
  17. go graceful.GetManager().RunWithShutdownContext(m.Run)
  18. }
  19. // Run runs the manager within a provided context
  20. func (m *Manager) Run(ctx context.Context) {
  21. then := timeutil.TimeStampNow().Add(-2)
  22. timer := time.NewTicker(setting.UI.Notification.EventSourceUpdateTime)
  23. rewardThen := then
  24. rewardTimer := time.NewTicker(setting.UI.Notification.RewardNotifyUpdateTime)
  25. loop:
  26. for {
  27. select {
  28. case <-rewardTimer.C:
  29. log.Debug("rewardTimer run")
  30. now := timeutil.TimeStampNow().Add(-2)
  31. list := reward.GetRewardOperation(rewardThen, now)
  32. if list != nil {
  33. log.Debug("GetRewardOperation list=%v", list)
  34. for _, l := range list {
  35. m.SendMessage(l.UserId, &Event{
  36. Name: "reward-operation",
  37. Data: l.Msg,
  38. })
  39. }
  40. }
  41. rewardThen = now
  42. }
  43. select {
  44. case <-ctx.Done():
  45. timer.Stop()
  46. break loop
  47. case <-timer.C:
  48. now := timeutil.TimeStampNow().Add(-2)
  49. uidCounts, err := models.GetUIDsAndNotificationCounts(then, now)
  50. if err != nil {
  51. log.Error("Unable to get UIDcounts: %v", err)
  52. }
  53. for _, uidCount := range uidCounts {
  54. m.SendMessage(uidCount.UserID, &Event{
  55. Name: "notification-count",
  56. Data: uidCount,
  57. })
  58. }
  59. then = now
  60. default:
  61. }
  62. }
  63. m.UnregisterAll()
  64. }