compare_only.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package service
  2. import (
  3. "bufio"
  4. "context"
  5. "io"
  6. "os"
  7. "strconv"
  8. "time"
  9. "go-common/app/job/main/passport-game-data/conf"
  10. "go-common/app/job/main/passport-game-data/dao"
  11. "go-common/library/log"
  12. )
  13. // NewCompareOnly new a service for compare only.
  14. func NewCompareOnly(c *conf.Config) (s *Service) {
  15. s = &Service{
  16. c: c,
  17. d: dao.New(c),
  18. l2cC: newCompareConfigFrom(c.Compare.Local2Cloud),
  19. }
  20. return
  21. }
  22. // CompareFromMidListFile load mid list from file and compare.
  23. func (s *Service) CompareFromMidListFile(c context.Context, fn string) (err error) {
  24. var f *os.File
  25. if f, err = os.Open(fn); err != nil {
  26. log.Error("failed to open file %s, error(%v)", fn, err)
  27. return
  28. }
  29. defer f.Close()
  30. cc := s.l2cC
  31. rd := bufio.NewReader(f)
  32. skippedCount := 0
  33. var (
  34. mid int64
  35. mids = make([]int64, 0)
  36. line []byte
  37. isPrefix bool
  38. )
  39. for {
  40. line, isPrefix, err = rd.ReadLine()
  41. if isPrefix || err != nil || err == io.EOF {
  42. break
  43. }
  44. mid, err = strconv.ParseInt(string(line), 10, 64)
  45. if err != nil {
  46. log.Error("failed to parse mid, strconv.ParseInt(%s, 10 ,64) error(%v), skip", line, err)
  47. skippedCount++
  48. continue
  49. }
  50. mids = append(mids, mid)
  51. }
  52. log.Info("mid list len: %d, total skipped count: %d", len(mids), skippedCount)
  53. if len(mids) == 0 {
  54. return
  55. }
  56. for {
  57. time.Sleep(cc.LoopDuration)
  58. if err = s.local2CloudCompare(context.TODO(), s.batchQueryLocalNonMiss(context.TODO(), mids, cc.BatchSize, cc.BatchMissRetryCount)); err == nil {
  59. break
  60. }
  61. log.Error("failed to compare mids %v, retrying", mids)
  62. }
  63. return
  64. }