service.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package service
  2. import (
  3. "context"
  4. "sync"
  5. "time"
  6. "go-common/app/job/main/thumbup/conf"
  7. "go-common/app/job/main/thumbup/dao"
  8. "go-common/app/job/main/thumbup/model"
  9. "go-common/library/queue/databus"
  10. "go-common/library/queue/databus/databusutil"
  11. "go-common/library/sync/pipeline"
  12. )
  13. const (
  14. _retryTimes = 3
  15. )
  16. // Service .Service
  17. type Service struct {
  18. c *conf.Config
  19. dao *dao.Dao
  20. waiter *sync.WaitGroup
  21. merge *pipeline.Pipeline
  22. likeGroup *databusutil.Group
  23. itemLikesGroup *databusutil.Group
  24. userLikesGroup *databusutil.Group
  25. // businessMap
  26. businessMap map[string]*model.Business
  27. businessIDMap map[int64]*model.Business
  28. // for 拜年祭
  29. statMerge *statMerge
  30. }
  31. type statMerge struct {
  32. Business string
  33. Target int64
  34. Sources map[int64]bool
  35. }
  36. // New create service instance and return.
  37. func New(c *conf.Config) (s *Service) {
  38. s = &Service{
  39. c: c,
  40. dao: dao.New(c),
  41. // waitGroup
  42. waiter: new(sync.WaitGroup),
  43. }
  44. if c.StatMerge != nil {
  45. s.statMerge = &statMerge{
  46. Business: c.StatMerge.Business,
  47. Target: c.StatMerge.Target,
  48. Sources: make(map[int64]bool),
  49. }
  50. for _, id := range c.StatMerge.Sources {
  51. s.statMerge.Sources[id] = true
  52. }
  53. }
  54. s.loadBusiness()
  55. s.initLikeGroup(c)
  56. s.initItemLikesGroup(c)
  57. s.initUserLikesGroup(c)
  58. s.initCountsMerge()
  59. go s.loadBusinessproc()
  60. return
  61. }
  62. func (s *Service) loadBusinessproc() {
  63. for {
  64. s.loadBusiness()
  65. time.Sleep(time.Minute * 5)
  66. }
  67. }
  68. func (s *Service) initItemLikesGroup(c *conf.Config) {
  69. ig := databusutil.NewGroup(c.ItemLikesDatabusutil, databus.New(c.Databus.ItemLikes).Messages())
  70. ig.New = newItemLikeMsg
  71. ig.Split = itemLikesSplit
  72. ig.Do = s.itemLikesDo
  73. ig.Start()
  74. s.itemLikesGroup = ig
  75. }
  76. func (s *Service) initUserLikesGroup(c *conf.Config) {
  77. ug := databusutil.NewGroup(c.UserLikesDatabusutil, databus.New(c.Databus.UserLikes).Messages())
  78. ug.New = newUserLikeMsg
  79. ug.Split = userLikesSplit
  80. ug.Do = s.userLikesDo
  81. ug.Start()
  82. s.userLikesGroup = ug
  83. }
  84. func (s *Service) initLikeGroup(c *conf.Config) {
  85. lg := databusutil.NewGroup(c.LikeDatabusutil, databus.New(c.Databus.Like).Messages())
  86. lg.New = newLikeMsg
  87. lg.Split = likeSplit
  88. lg.Do = s.likeDo
  89. lg.Start()
  90. s.likeGroup = lg
  91. }
  92. func (s *Service) initCountsMerge() {
  93. s.merge = pipeline.NewPipeline(s.c.Merge)
  94. s.merge.Split = s.countsSplit
  95. s.merge.Do = s.updateCountsDo
  96. s.merge.Start()
  97. }
  98. func (s *Service) loadBusiness() {
  99. var (
  100. err error
  101. business []*model.Business
  102. )
  103. businessMap := make(map[string]*model.Business)
  104. businessIDMap := make(map[int64]*model.Business)
  105. for {
  106. if business, err = s.dao.Business(context.TODO()); err != nil {
  107. time.Sleep(time.Second)
  108. continue
  109. }
  110. for _, b := range business {
  111. businessMap[b.Name] = b
  112. businessIDMap[b.ID] = b
  113. }
  114. s.businessMap = businessMap
  115. s.businessIDMap = businessIDMap
  116. return
  117. }
  118. }
  119. // Ping .
  120. func (s *Service) Ping(ctx context.Context) error {
  121. return s.dao.Ping(ctx)
  122. }
  123. // Close .
  124. func (s *Service) Close() {
  125. s.waiter.Wait()
  126. s.itemLikesGroup.Close()
  127. s.userLikesGroup.Close()
  128. s.likeGroup.Close()
  129. s.dao.Close()
  130. }