service.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "strings"
  6. "sync"
  7. "sync/atomic"
  8. "time"
  9. "go-common/app/job/main/relation/conf"
  10. "go-common/app/job/main/relation/dao"
  11. "go-common/app/job/main/relation/model"
  12. sml "go-common/app/service/main/relation/model"
  13. "go-common/library/log"
  14. "go-common/library/queue/databus"
  15. xtime "go-common/library/time"
  16. )
  17. const (
  18. _relationFidTable = "user_relation_fid_"
  19. _relationMidTable = "user_relation_mid_"
  20. _relationStatTable = "user_relation_stat_"
  21. _relationTagUserTable = "user_relation_tag_user_"
  22. _retry = 5
  23. _retrySleep = time.Second * 1
  24. )
  25. // Service struct of service.
  26. type Service struct {
  27. c *conf.Config
  28. dao *dao.Dao
  29. ds *databus.Databus
  30. waiter *sync.WaitGroup
  31. // monitor
  32. mo int64
  33. // moni *monitor.Service
  34. }
  35. // New create service instance and return.
  36. func New(c *conf.Config) (s *Service) {
  37. s = &Service{
  38. c: c,
  39. dao: dao.New(c),
  40. ds: databus.New(c.DataBus),
  41. waiter: new(sync.WaitGroup),
  42. // moni: monitor.New(),
  43. }
  44. for i := 0; i < 50; i++ {
  45. s.waiter.Add(1)
  46. go s.subproc()
  47. }
  48. go s.checkConsume()
  49. return
  50. }
  51. func (s *Service) subproc() {
  52. defer s.waiter.Done()
  53. for {
  54. var (
  55. ok bool
  56. err error
  57. res *databus.Message
  58. )
  59. if res, ok = <-s.ds.Messages(); !ok {
  60. log.Error("s.ds.Messages() failed")
  61. return
  62. }
  63. log.Info("received message: res: %+v", res)
  64. mu := &model.Message{}
  65. if err = json.Unmarshal(res.Value, mu); err != nil {
  66. log.Error("go-common/app/job/main/relation,json.Unmarshal (%v) error(%v)", res.Value, err)
  67. continue
  68. }
  69. atomic.AddInt64(&s.mo, 1)
  70. for i := 0; ; i++ {
  71. switch {
  72. case strings.HasPrefix(mu.Table, _relationStatTable):
  73. err = s.stat(mu.Action, mu.New, mu.Old)
  74. case strings.HasPrefix(mu.Table, _relationMidTable):
  75. err = s.relationMid(mu.Action, mu.New, mu.Old)
  76. case strings.HasPrefix(mu.Table, _relationFidTable):
  77. err = s.relationFid(mu.Action, mu.New, mu.Old)
  78. case strings.HasPrefix(mu.Table, _relationTagUserTable):
  79. err = s.tagUser(mu.New)
  80. }
  81. if err != nil {
  82. i++
  83. log.Error("s.flush data(%s) error(%+v)", mu.New, err)
  84. time.Sleep(_retrySleep)
  85. if i > _retry {
  86. // if s.c.Env == "prod" {
  87. // s.moni.Sms(context.TODO(), s.c.Sms.Phone, s.c.Sms.Token, fmt.Sprintf("relation-job update cache fail: %v", err))
  88. // }
  89. break
  90. }
  91. continue
  92. }
  93. break
  94. }
  95. log.Info("consumer action:%v, table:%v,new :%s", mu.Action, mu.Table, mu.New)
  96. res.Commit()
  97. }
  98. }
  99. func (s *Service) tagUser(newMsg []byte) (err error) {
  100. var tags struct {
  101. Fid int64 `json:"fid"`
  102. Mid int64 `json:"mid"`
  103. }
  104. if err = json.Unmarshal(newMsg, &tags); err != nil {
  105. log.Error("json.Unmarshal err(%v)", err)
  106. return
  107. }
  108. f, err := s.dao.UserRelation(context.TODO(), tags.Mid, tags.Fid)
  109. if err != nil || f == nil {
  110. return
  111. }
  112. s.dao.DelTagsCache(context.TODO(), tags.Mid)
  113. s.upFollowingCache(context.TODO(), tags.Mid, f)
  114. return
  115. }
  116. func (s *Service) upFollowingCache(c context.Context, mid int64, f *sml.Following) (err error) {
  117. if f.Attribute == 0 {
  118. s.dao.DelFollowing(c, mid, f)
  119. } else {
  120. if err = s.dao.AddFollowingCache(c, mid, f); err != nil {
  121. return
  122. }
  123. }
  124. if err = s.dao.DelFollowingCache(c, mid); err != nil {
  125. return
  126. }
  127. return s.dao.DelTagCountCache(c, mid)
  128. }
  129. // relationFid
  130. func (s *Service) relationFid(action string, nwMsg []byte, oldMsg []byte) error {
  131. var or *model.Relation
  132. mr := &model.Relation{}
  133. if err := json.Unmarshal(nwMsg, mr); err != nil {
  134. log.Error("json.Unmarshal(%v) error(%v)", nwMsg, err)
  135. return err
  136. }
  137. if len(oldMsg) > 0 {
  138. or = new(model.Relation)
  139. if err := json.Unmarshal(oldMsg, or); err != nil {
  140. log.Error("json.Unmarshal(%v) error(%v)", oldMsg, err)
  141. }
  142. }
  143. // step 1: add notify
  144. // if err := s.dao.AddNotify(context.TODO(), mr.Fid); err != nil {
  145. // log.Error("Failed to s.dao.AddNotify(%v): %+v", mr.Fid, err)
  146. // }
  147. // step 2: handle recent followers
  148. // s.RecentFollowers(action, mr, or)
  149. // step 3: delete cache
  150. return s.dao.DelFollowerCache(mr.Fid)
  151. }
  152. // relationMid
  153. func (s *Service) relationMid(action string, nwMsg []byte, oldMsg []byte) (err error) {
  154. mr := &model.Relation{}
  155. if err = json.Unmarshal(nwMsg, mr); err != nil {
  156. log.Error("json.Unmarshal(%v) error(%v)", nwMsg, err)
  157. return
  158. }
  159. f := &sml.Following{
  160. Mid: mr.Fid,
  161. Attribute: mr.Attribute,
  162. MTime: xtime.Time(time.Now().Unix()),
  163. }
  164. if err = s.upFollowingCache(context.TODO(), mr.Mid, f); err != nil {
  165. return
  166. }
  167. log.Info("Succeed to update following cache: mid: %d: mr: %+v", mr.Mid, mr)
  168. // TODO: del this, just for special attention del old cache.
  169. s.dao.DelTagsCache(context.TODO(), mr.Mid)
  170. log.Info("Succeed to delete tags cache: mid: %d", mr.Mid)
  171. // s.dao.AddNotify(context.TODO(), mr.Mid)
  172. return
  173. }
  174. // stat
  175. func (s *Service) stat(action string, nwMsg []byte, oldMsg []byte) (err error) {
  176. ms := &model.Stat{}
  177. if err = json.Unmarshal(nwMsg, ms); err != nil {
  178. log.Error("json.Unmarshal(%v) error(%v)", nwMsg, err)
  179. return
  180. }
  181. mo := &model.Stat{}
  182. if len(oldMsg) > 0 {
  183. if err = json.Unmarshal(oldMsg, mo); err != nil {
  184. log.Error("json.Unmarshal(%v) error(%v)", oldMsg, err)
  185. err = nil
  186. }
  187. }
  188. if ms.Follower > mo.Follower {
  189. s.dao.FollowerAchieve(context.TODO(), ms.Mid, ms.Follower)
  190. }
  191. return s.dao.DelStatCache(ms.Mid)
  192. }
  193. // check consumer stat
  194. func (s *Service) checkConsume() {
  195. if s.c.Env != "prod" {
  196. return
  197. }
  198. var reMo int64
  199. for {
  200. time.Sleep(1 * time.Minute)
  201. atomic.AddInt64(&s.mo, -reMo)
  202. // if atomic.AddInt64(&s.mo, -reMo) == 0 {
  203. // s.moni.Sms(context.TODO(), s.c.Sms.Phone, s.c.Sms.Token, "relation-job stat did not consume within a minute")
  204. // }
  205. reMo = atomic.LoadInt64(&s.mo)
  206. }
  207. }
  208. // Ping check server ok
  209. func (s *Service) Ping(c context.Context) (err error) {
  210. return s.dao.Ping(c)
  211. }
  212. // Close kafka consumer close.
  213. func (s *Service) Close() (err error) {
  214. s.ds.Close()
  215. return
  216. }
  217. // Wait wait for service exit.
  218. func (s *Service) Wait() {
  219. s.waiter.Wait()
  220. }