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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. now := timeutil.TimeStampNow().Add(-2)
  30. list := reward.GetRewardOperation(rewardThen, now)
  31. if list != nil {
  32. for _, l := range list {
  33. m.SendMessage(l.UserId, &Event{
  34. Name: "reward-operation",
  35. Data: l.Msg,
  36. })
  37. }
  38. }
  39. rewardThen = now
  40. }
  41. select {
  42. case <-ctx.Done():
  43. timer.Stop()
  44. break loop
  45. case <-timer.C:
  46. now := timeutil.TimeStampNow().Add(-2)
  47. uidCounts, err := models.GetUIDsAndNotificationCounts(then, now)
  48. if err != nil {
  49. log.Error("Unable to get UIDcounts: %v", err)
  50. }
  51. for _, uidCount := range uidCounts {
  52. m.SendMessage(uidCount.UserID, &Event{
  53. Name: "notification-count",
  54. Data: uidCount,
  55. })
  56. }
  57. then = now
  58. default:
  59. }
  60. }
  61. m.UnregisterAll()
  62. }