push.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package service
  2. import (
  3. "time"
  4. "go-common/library/log"
  5. )
  6. const (
  7. _platIOS = "ios"
  8. _platAndroid = "android"
  9. _platAll = ""
  10. _vIPad = "ipad"
  11. _vIPhone = "iphone"
  12. _vAndroid = "android"
  13. _vAndroidB = "android_b"
  14. )
  15. func (s *Service) push(resIDs map[string]int64) {
  16. var (
  17. now = time.Now().Unix()
  18. timeout = s.c.Cfg.Push.Timeout
  19. err error
  20. msg string
  21. )
  22. for resID, timeV := range resIDs {
  23. finish := false
  24. needPush := false
  25. // distinguish whether the resource is ready to push. calc finish or timeout
  26. if finish, err = s.pushDao.DiffFinish(ctx, resID); err != nil { // check whether diff cal finish
  27. continue
  28. }
  29. if now-timeV > timeout { // check whether it's already timeout
  30. needPush = true
  31. log.Info("CallPush [%v] Because of Timeout", resID)
  32. } else if finish {
  33. needPush = true
  34. log.Info("CallPush [%v] Because of DiffFinish", resID)
  35. } else {
  36. log.Info("CallPush Jump [%v]", resID)
  37. continue
  38. }
  39. // prepare api call
  40. if msg, err = s.pushDao.PushMsg(ctx, resID); err != nil { // prepare msg
  41. continue
  42. }
  43. if needPush {
  44. if err = s.pushDao.CallRefresh(ctx); err != nil {
  45. log.Error("CallPush [%d] app-resource refresh error [%v]", resID, err)
  46. continue
  47. }
  48. time.Sleep(time.Duration(s.c.Cfg.Push.Pause))
  49. if err = s.pushDao.CallPush(ctx, s.platform(resID), msg, ""); err != nil {
  50. log.Error("CallPush [%v] Error [%v]", resID, err)
  51. continue
  52. }
  53. log.Info("CallPush [%v] Succ, Platform: %s, Delete Key", resID)
  54. if err = s.pushDao.ZRem(ctx, resID); err != nil {
  55. continue
  56. }
  57. }
  58. }
  59. }
  60. // distinguish the resource's platform info
  61. func (s *Service) platform(resID string) (platform string) {
  62. var (
  63. err error
  64. ios, android bool
  65. mobiAPPs []string
  66. )
  67. platform = _platAll // default value
  68. if mobiAPPs, err = s.pushDao.Platform(ctx, resID); err != nil {
  69. return
  70. }
  71. for _, value := range mobiAPPs {
  72. switch value {
  73. case _vAndroid:
  74. android = true
  75. case _vAndroidB:
  76. android = true
  77. case _vIPad:
  78. ios = true
  79. case _vIPhone:
  80. ios = true
  81. default:
  82. log.Error("ResourceID %d, Limit Wrong Value %s", resID, value)
  83. }
  84. }
  85. if ios && !android {
  86. return _platIOS
  87. }
  88. if !ios && android {
  89. return _platAndroid
  90. }
  91. return // other case like all or none, just return the default value
  92. }
  93. func (s *Service) pushproc() {
  94. var (
  95. resIDs map[string]int64
  96. err error
  97. )
  98. defer s.waiter.Done()
  99. for {
  100. if s.daoClosed {
  101. log.Info("DB closed!")
  102. return
  103. }
  104. time.Sleep(time.Duration(s.c.Cfg.Push.Fre))
  105. // pick to push resIDs from redis
  106. if resIDs, err = s.pushDao.ZrangeList(ctx); err != nil {
  107. log.Error("Get ToPush List Err %v", err)
  108. continue
  109. }
  110. if len(resIDs) == 0 {
  111. log.Info("No ToPush Data, Sleep")
  112. continue
  113. }
  114. // push the data
  115. log.Info("ToPush Treat Data: %d", len(resIDs))
  116. s.push(resIDs)
  117. }
  118. }