relation.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "strings"
  6. "go-common/app/job/main/account-summary/model"
  7. "go-common/library/log"
  8. )
  9. func (s *Service) relationBinLogproc(ctx context.Context) {
  10. for msg := range s.RelationBinLog.Messages() {
  11. blog := &model.CanalBinLog{}
  12. if err := json.Unmarshal(msg.Value, blog); err != nil {
  13. log.Error("Failed to unmarshal canal bin log: %+v, value: %s: %+v", msg, string(msg.Value), err)
  14. msg.Commit()
  15. continue
  16. }
  17. log.Info("Handling message key: %s, value: %s", msg.Key, string(msg.Value))
  18. s.relationBinLogHandle(ctx, blog)
  19. msg.Commit()
  20. }
  21. }
  22. func (s *Service) relationBinLogHandle(ctx context.Context, blog *model.CanalBinLog) {
  23. if len(blog.New) == 0 {
  24. log.Error("Failed to sync to hbase with empty new field: %+v", blog)
  25. return
  26. }
  27. switch {
  28. case strings.HasPrefix(blog.Table, "user_relation_stat_"):
  29. midl := &model.MidBinLog{}
  30. if err := json.Unmarshal(blog.New, midl); err != nil {
  31. log.Error("Failed to unmarsha new data: %s: %+v", string(blog.New), err)
  32. return
  33. }
  34. if err := s.syncRelationStat(ctx, midl.Mid); err != nil {
  35. log.Error("Failed to sync relation stat with mid: %d: %+v", midl.Mid, err)
  36. return
  37. }
  38. default:
  39. log.Warn("Unable to hanlde binlog: %+v, old: %s, new: %s", blog, string(blog.Old), string(blog.New))
  40. }
  41. }