archive.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "math"
  6. "time"
  7. "go-common/app/admin/main/up/util/mathutil"
  8. "go-common/app/job/main/up/model"
  9. "go-common/app/job/main/up/model/upcrmmodel"
  10. upGRPCv1 "go-common/app/service/main/up/api/v1"
  11. "go-common/library/log"
  12. "go-common/library/queue/databus"
  13. "go-common/app/job/main/up/model/archivemodel"
  14. )
  15. // action
  16. const (
  17. ActionUpdate = "update"
  18. ActionInsert = "insert"
  19. )
  20. // table name
  21. const (
  22. TableArchiveStaff = "archive_staff"
  23. )
  24. //ArchiveUpInfo .
  25. type ArchiveUpInfo struct {
  26. Table string `json:"table"`
  27. Action string `json:"action"`
  28. New *archivemodel.ArchiveCanal `json:"new"`
  29. Old *archivemodel.ArchiveCanal `json:"old"`
  30. }
  31. // CanalMsg canal message struct
  32. type CanalMsg struct {
  33. Action string `json:"action"`
  34. Table string `json:"table"`
  35. New json.RawMessage `json:"new"`
  36. Old json.RawMessage `json:"old"`
  37. }
  38. // handle notify t message
  39. func (s *Service) handleArchiveNotifyT(msg *databus.Message) (err error) {
  40. m := &ArchiveUpInfo{}
  41. if err = json.Unmarshal(msg.Value, m); err != nil {
  42. log.Error("json.Unmarshal(%v) error(%v)", msg.Value, err)
  43. return
  44. }
  45. switch m.Table {
  46. case "archive":
  47. err = s.checkArchiveNotify(m)
  48. }
  49. if err != nil {
  50. log.Error("handle msg fail, err=%s", err)
  51. }
  52. return
  53. }
  54. func (s *Service) checkArchiveNotify(msg *ArchiveUpInfo) (err error) {
  55. var arch *archivemodel.ArchiveCanal
  56. switch {
  57. default:
  58. if msg.Action == ActionInsert {
  59. if msg.New != nil && msg.New.State >= 0 {
  60. arch = msg.New
  61. }
  62. break
  63. }
  64. if msg.Action == ActionUpdate {
  65. if archiveStateChange(msg.New, msg.Old) {
  66. arch = msg.New
  67. }
  68. break
  69. }
  70. }
  71. if arch == nil {
  72. log.Warn("no need to update up cache, msg value=%v", msg)
  73. return
  74. }
  75. s.worker.Add(func() {
  76. s.upRPC.UpCount(context.Background(), &upGRPCv1.UpCountReq{
  77. Mid: arch.Mid,
  78. })
  79. var upCacheReq = &upGRPCv1.UpCacheReq{
  80. Mid: arch.Mid,
  81. Aid: arch.AID,
  82. }
  83. if arch.State >= 0 {
  84. s.upRPC.AddUpPassedCache(context.Background(), upCacheReq)
  85. log.Info("rpc add up cache, mid=%d, aid=%d", upCacheReq.Mid, upCacheReq.Aid)
  86. } else {
  87. s.upRPC.DelUpPassedCache(context.Background(), upCacheReq)
  88. log.Info("rpc delete up cache, mid=%d, aid=%d", upCacheReq.Mid, upCacheReq.Aid)
  89. }
  90. })
  91. return
  92. }
  93. func (s *Service) handleArchiveT(msg *databus.Message) (err error) {
  94. var m = &CanalMsg{}
  95. if err = json.Unmarshal(msg.Value, m); err != nil {
  96. log.Error("json.Unmarshal(%v) error(%v)", msg.Value, err)
  97. return
  98. }
  99. switch m.Table {
  100. case TableArchiveStaff:
  101. var new, old archivemodel.ArchiveStaff
  102. switch m.Action {
  103. case ActionInsert:
  104. if err = json.Unmarshal(m.New, &new); err != nil {
  105. log.Error("m.New -> json.Unmarshal(%v) error(%v)", m.New, err)
  106. return
  107. }
  108. case ActionUpdate:
  109. if err = json.Unmarshal(m.New, &new); err != nil {
  110. log.Error("m.New -> json.Unmarshal(%v) error(%v)", m.New, err)
  111. return
  112. }
  113. if err = json.Unmarshal(m.Old, &old); err != nil {
  114. log.Error("m.Old -> json.Unmarshal(%v) error(%v)", m.New, err)
  115. return
  116. }
  117. if new.State == old.State {
  118. log.Warn("new staff state(%d) eq old staff state(%d)", new.State, old.State)
  119. return
  120. }
  121. }
  122. // state是正常说明是新增,否则是删除
  123. needInsert := new.State == archivemodel.StaffStateNormal
  124. var req = &upGRPCv1.UpCacheReq{Mid: new.StaffMid, Aid: new.Aid}
  125. if needInsert {
  126. _, err = s.upRPC.AddUpPassedCacheByStaff(context.Background(), req)
  127. if err != nil {
  128. log.Error("rpc call add up staff, new=%v, err=%v", new, err)
  129. } else {
  130. log.Info("rpc call add up staff, new=%v", new)
  131. }
  132. } else {
  133. _, err = s.upRPC.DelUpPassedCacheByStaff(context.Background(), req)
  134. if err != nil {
  135. log.Error("rpc call del up staff, new=%v, err=%v", new, err)
  136. } else {
  137. log.Info("rpc call del up staff, new=%v", new)
  138. }
  139. }
  140. }
  141. return
  142. }
  143. func archiveStateChange(a, b *archivemodel.ArchiveCanal) bool {
  144. if a == b {
  145. return false
  146. } else if a == nil || b == nil {
  147. return true
  148. }
  149. if a.State == b.State {
  150. return false
  151. }
  152. var min, max int
  153. if a.State > b.State {
  154. min, max = b.State, a.State
  155. } else {
  156. min, max = a.State, b.State
  157. }
  158. if min < 0 && max >= 0 {
  159. return true
  160. }
  161. return false
  162. }
  163. //WarmUp warm up
  164. func (s *Service) WarmUp(c context.Context, req *model.WarmUpReq) (res *model.WarmUpReply, err error) {
  165. var (
  166. d = s.crmdb.GetDb()
  167. lastID = req.LastID
  168. limit, thisCount = 100, 100
  169. )
  170. var count = 0
  171. if req.Size == -1 {
  172. req.Size = math.MaxInt32
  173. }
  174. for ; count < req.Size && thisCount == limit; count += thisCount {
  175. time.Sleep(time.Millisecond * 1000)
  176. var end = count + limit
  177. if end > req.Size {
  178. limit = req.Size - count
  179. }
  180. var upList []*upcrmmodel.UpBaseInfo
  181. err = d.Select("mid, id").Where("id>?", lastID).Limit(limit).Find(&upList).Error
  182. if err != nil {
  183. log.Error("fail to query db, err=%v", err)
  184. return
  185. }
  186. thisCount = len(upList)
  187. var mids []int64
  188. for _, v := range upList {
  189. lastID = mathutil.Max(lastID, int(v.ID))
  190. mids = append(mids, v.Mid)
  191. }
  192. var _, e = s.upRPC.UpsArcs(context.Background(), &upGRPCv1.UpsArcsReq{
  193. Mids: mids,
  194. Pn: 1,
  195. Ps: 1,
  196. })
  197. if e != nil {
  198. log.Warn("up rpc UpsArcs return err=%v", e)
  199. }
  200. _, e = s.upRPC.UpsCount(context.Background(), &upGRPCv1.UpsCountReq{
  201. Mids: mids,
  202. })
  203. if e != nil {
  204. log.Warn("up rpc UpsCount return err=%v", e)
  205. }
  206. log.Info("warm ups, handled last id=%d, count=%d", lastID, count)
  207. }
  208. log.Info("warm ups, begin id=%d, expect up size=%d, end id=%d, real count=%d", req.LastID, req.Size, lastID, count)
  209. res = &model.WarmUpReply{
  210. LastID: lastID,
  211. }
  212. return
  213. }
  214. //WarmUpMid warm up by mid
  215. func (s *Service) WarmUpMid(c context.Context, req *model.WarmUpReq) (res *model.WarmUpReply, err error) {
  216. if _, err = s.upRPC.UpArcs(context.Background(), &upGRPCv1.UpArcsReq{
  217. Mid: req.Mid,
  218. Pn: 1,
  219. Ps: 1,
  220. }); err != nil {
  221. log.Error("up rpc UpsArc(%d) return err=%v", req.Mid, err)
  222. return
  223. }
  224. var (
  225. count int64
  226. cntReply *upGRPCv1.UpCountReply
  227. )
  228. if cntReply, err = s.upRPC.UpCount(context.Background(), &upGRPCv1.UpCountReq{
  229. Mid: req.Mid,
  230. }); err != nil {
  231. log.Error("up rpc UpsCount(%d) return err=%v", req.Mid, err)
  232. return
  233. }
  234. if cntReply != nil {
  235. count = cntReply.Count
  236. }
  237. log.Info("warm up(%d) real count=%d", req.Mid, count)
  238. return
  239. }
  240. //AddStaff .
  241. func (s *Service) AddStaff(c context.Context, req *model.AddStaffReq) (res *upGRPCv1.NoReply, err error) {
  242. return s.upRPC.AddUpPassedCacheByStaff(c, &upGRPCv1.UpCacheReq{Aid: req.Aid, Mid: req.StaffMid})
  243. }
  244. //DeleteStaff .
  245. func (s *Service) DeleteStaff(c context.Context, req *model.AddStaffReq) (res *upGRPCv1.NoReply, err error) {
  246. return s.upRPC.DelUpPassedCacheByStaff(c, &upGRPCv1.UpCacheReq{Aid: req.Aid, Mid: req.StaffMid})
  247. }