sync.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package service
  2. import (
  3. "context"
  4. "sync"
  5. "go-common/app/job/main/account-summary/model"
  6. member "go-common/app/service/main/member/model"
  7. "go-common/app/service/main/member/model/block"
  8. relation "go-common/app/service/main/relation/model"
  9. "go-common/library/log"
  10. xtime "go-common/library/time"
  11. )
  12. func (s *Service) block(ctx context.Context, mid int64) (*model.BlockSummary, error) {
  13. bl, err := s.dao.MemberService.BlockInfo(ctx, &block.RPCArgInfo{MID: mid})
  14. if err != nil {
  15. return nil, err
  16. }
  17. blSummary := &model.BlockSummary{
  18. EmbedMid: model.EmbedMid{Mid: bl.MID},
  19. BlockStatus: int64(bl.BlockStatus),
  20. StartTime: model.Datetime(xtime.Time(bl.StartTime)),
  21. EndTime: model.Datetime(xtime.Time(bl.EndTime)),
  22. }
  23. return blSummary, nil
  24. }
  25. func (s *Service) relationStat(ctx context.Context, mid int64) (*model.RelationStat, error) {
  26. stat, err := s.dao.RelationService.Stat(ctx, &relation.ArgMid{Mid: mid})
  27. if err != nil {
  28. return nil, err
  29. }
  30. reStat := &model.RelationStat{
  31. EmbedMid: model.EmbedMid{Mid: stat.Mid},
  32. Follower: stat.Follower,
  33. Following: stat.Following,
  34. Whisper: stat.Whisper,
  35. Black: stat.Black,
  36. }
  37. return reStat, nil
  38. }
  39. func (s *Service) passportSummary(ctx context.Context, mid int64) (*model.PassportSummary, error) {
  40. ps := &model.PassportSummary{
  41. EmbedMid: model.EmbedMid{Mid: mid},
  42. }
  43. wg := &sync.WaitGroup{}
  44. wg.Add(1)
  45. go func() error {
  46. defer wg.Done()
  47. pp, err := s.dao.PassportProfile(ctx, mid)
  48. if err != nil {
  49. log.Error("Failed to fetch passport profile: %+v", err)
  50. return err
  51. }
  52. ps.TelStatus = pp.TelStatus()
  53. ps.CountryID = pp.CountryCode
  54. ps.JoinIP = pp.JoinIP
  55. ps.JoinTime = model.Datetime(pp.JoinTime)
  56. ps.EmailSuffix = pp.EmailSuffix()
  57. return nil
  58. }()
  59. wg.Add(1)
  60. go func() error {
  61. defer wg.Done()
  62. origin, err := s.dao.AsoAccountRegOrigin(ctx, mid)
  63. if err != nil {
  64. log.Error("Failed to fetch passport aso account reg origin: %+v", err)
  65. return err
  66. }
  67. ps.RegType = origin.RegType
  68. ps.OriginType = origin.OriginType
  69. return nil
  70. }()
  71. wg.Wait()
  72. return ps, nil
  73. }
  74. func (s *Service) member(ctx context.Context, mid int64) (*model.MemberBase, *model.MemberExp, *model.MemberOfficial, error) {
  75. mb, err := s.dao.MemberService.Member(ctx, &member.ArgMemberMid{Mid: mid})
  76. if err != nil {
  77. return nil, nil, nil, err
  78. }
  79. var base *model.MemberBase
  80. if mb.BaseInfo != nil {
  81. base = &model.MemberBase{
  82. EmbedMid: model.EmbedMid{Mid: mb.Mid},
  83. Name: mb.Name,
  84. Face: mb.Face,
  85. Rank: int64(mb.Rank),
  86. Sex: mb.Sex,
  87. Sign: mb.Sign,
  88. Birthday: model.Date(mb.Birthday),
  89. }
  90. }
  91. var exp *model.MemberExp
  92. if mb.LevelInfo != nil {
  93. exp = &model.MemberExp{
  94. EmbedMid: model.EmbedMid{Mid: mb.Mid},
  95. Exp: int64(mb.NowExp),
  96. }
  97. }
  98. var of *model.MemberOfficial
  99. if mb.OfficialInfo != nil {
  100. of = &model.MemberOfficial{
  101. EmbedMid: model.EmbedMid{Mid: mb.Mid},
  102. Role: int64(mb.Role),
  103. Title: mb.Title,
  104. Description: mb.Desc,
  105. }
  106. }
  107. return base, exp, of, nil
  108. }
  109. func (s *Service) syncMember(ctx context.Context, mid int64) error {
  110. base, exp, of, err := s.member(ctx, mid)
  111. if err != nil {
  112. return err
  113. }
  114. syncable := make([]Syncable, 0, 3)
  115. if base != nil {
  116. syncable = append(syncable, base)
  117. }
  118. if exp != nil {
  119. syncable = append(syncable, exp)
  120. }
  121. if of != nil {
  122. syncable = append(syncable, of)
  123. }
  124. for _, data := range syncable {
  125. if err := s.SyncToHBase(ctx, data); err != nil {
  126. return err
  127. }
  128. }
  129. return nil
  130. }
  131. func (s *Service) syncRelationStat(ctx context.Context, mid int64) error {
  132. reStat, err := s.relationStat(ctx, mid)
  133. if err != nil {
  134. return err
  135. }
  136. if err := s.SyncToHBase(ctx, reStat); err != nil {
  137. return err
  138. }
  139. return nil
  140. }
  141. func (s *Service) syncBlock(ctx context.Context, mid int64) error {
  142. blSummary, err := s.block(ctx, mid)
  143. if err != nil {
  144. return err
  145. }
  146. if err := s.SyncToHBase(ctx, blSummary); err != nil {
  147. return err
  148. }
  149. return nil
  150. }
  151. func (s *Service) syncPassportSummary(ctx context.Context, mid int64) error {
  152. ps, err := s.passportSummary(ctx, mid)
  153. if err != nil {
  154. return err
  155. }
  156. if err := s.SyncToHBase(ctx, ps); err != nil {
  157. return err
  158. }
  159. return nil
  160. }
  161. func (s *Service) syncRangeproc(ctx context.Context, start, end int64, worker uint64) {
  162. log.Info("Sync with range: start: %d, end %d, worker: %d", start, end, worker)
  163. syncChan := make(chan int64, worker*128)
  164. defer close(syncChan)
  165. // initial
  166. wg := sync.WaitGroup{}
  167. wg.Add(1)
  168. for i := uint64(0); i < worker; i++ {
  169. go func() {
  170. defer wg.Done()
  171. for mid := range syncChan {
  172. if err := s.SyncOne(context.Background(), mid); err != nil {
  173. log.Error("Failed to sync user with mid: %d: %+v", mid, err)
  174. }
  175. }
  176. }()
  177. }
  178. for j := start; j <= end; j++ {
  179. syncChan <- j
  180. }
  181. wg.Wait()
  182. }