report.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/service/main/push/dao/mi"
  6. "go-common/app/service/main/push/model"
  7. "go-common/library/log"
  8. )
  9. // DelMiInvalid .
  10. func (d *Dao) DelMiInvalid(c context.Context) (err error) {
  11. log.Info("delete xiaomi invalid report start, apps(%d)", len(d.clientsMi))
  12. for appid, clients := range d.clientsMi {
  13. log.Info("clients info app(%d) len(%d)", appid, len(clients))
  14. if len(clients) == 0 {
  15. log.Warn("no clients app(%d)", appid)
  16. continue
  17. }
  18. log.Info("del mi invalid start, app(%d)", appid)
  19. var res *mi.Response
  20. if res, err = clients[0].InvalidTokens(); err != nil {
  21. log.Error("client.InvalidTokens() error(%v)", err)
  22. PromError("report:获取小米无效token")
  23. continue
  24. }
  25. if res == nil || len(res.Data.List) == 0 {
  26. log.Warn("no tokens app(%d)", appid)
  27. continue
  28. }
  29. if err = d.delInvalidMiReports(c, appid, res.Data.List); err != nil {
  30. PromError("report:主动删除xiaomi无效上报")
  31. continue
  32. }
  33. log.Info("already del mi invalid stop, app(%d) count(%d)", appid, len(res.Data.List))
  34. }
  35. PromInfo("report:主动删除xiaomi无效上报")
  36. return
  37. }
  38. // DelMiUninstalled deletes mi uninstalled tokens.
  39. func (d *Dao) DelMiUninstalled(c context.Context) (err error) {
  40. log.Info("delete xiaomi uninstalled tokens start, apps(%d)", len(d.clientsMi))
  41. for appid, clients := range d.clientsMi {
  42. log.Info("clients info app(%d) len(%d)", appid, len(clients))
  43. if len(clients) == 0 {
  44. log.Warn("no clients app(%d)", appid)
  45. continue
  46. }
  47. log.Info("del mi uninstalled tokens start, app(%d)", appid)
  48. var res *mi.UninstalledResponse
  49. if res, err = clients[0].UninstalledTokens(); err != nil {
  50. log.Error("client.UninstalledTokens() error(%v)", err)
  51. PromError("report:获取小米卸载token")
  52. continue
  53. }
  54. if res.Code == mi.ResultCodeNoMsgInEmq {
  55. log.Info("no tokens app(%d)", appid)
  56. continue
  57. }
  58. if res.Code != mi.ResultCodeOk {
  59. log.Error("get uninstalled tokens error resp(%+v)", res)
  60. continue
  61. }
  62. if len(res.Data) == 0 {
  63. log.Warn("no tokens app(%d)", appid)
  64. continue
  65. }
  66. if err = d.delInvalidMiReports(c, appid, res.Data); err != nil {
  67. PromError("report:主动删除xiaomi卸载token")
  68. continue
  69. }
  70. log.Info("already del mi uninstalled stop, app(%d) count(%d)", appid, len(res.Data))
  71. }
  72. PromInfo("report:主动删除xiaomi卸载token")
  73. return
  74. }
  75. func (d *Dao) delInvalidMiReports(c context.Context, appid int64, tokens []string) (err error) {
  76. var rs []*model.Report
  77. if rs, err = d.Reports(c, tokens); err != nil {
  78. log.Error("d.Reports(%v) error(%v)", tokens, err)
  79. return
  80. } else if len(rs) == 0 {
  81. log.Warn("reports can not be found by tokens(%d)", len(tokens))
  82. log.Warn("reports can not be found by tokens(%v)", tokens)
  83. return
  84. }
  85. for _, r := range rs {
  86. log.Info("deleted invalid mi report, app(%d) mid(%d) token(%s)", appid, r.Mid, r.DeviceToken)
  87. var (
  88. i int
  89. e error
  90. retry = _retry
  91. )
  92. for i < retry {
  93. if _, e = d.DelReport(c, r.DeviceToken); e == nil {
  94. break
  95. }
  96. time.Sleep(time.Second)
  97. i++
  98. log.Warn("retry delete report, mid(%d) token(%s)", r.Mid, r.DeviceToken)
  99. }
  100. if e != nil || r.Mid <= 0 {
  101. continue
  102. }
  103. i = 0
  104. for i < retry {
  105. if e = d.DelReportCache(c, r.Mid, r.APPID, r.DeviceToken); e == nil {
  106. break
  107. }
  108. log.Warn("retry delete report cache, mid(%d) token(%s)", r.Mid, r.DeviceToken)
  109. time.Sleep(time.Second)
  110. i++
  111. }
  112. }
  113. log.Info("del invalid mi report, app(%d) tokens(%d)", appid, len(rs))
  114. return
  115. }