version_config.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package task
  2. import (
  3. "dsbqj-admin/model/mongo/abver"
  4. "dsbqj-admin/model/mongo/md5"
  5. "strings"
  6. "sync"
  7. "github.com/kamva/mgm/v3"
  8. "go.mongodb.org/mongo-driver/bson"
  9. )
  10. type VersionConfigCache struct {
  11. sync.RWMutex
  12. abVers map[string][]*abver.AbVer
  13. md5s map[string][]*md5.MD5
  14. }
  15. type ConfigValue struct {
  16. Version string
  17. Value string
  18. }
  19. var VersionConfigTask = &VersionConfigCache{}
  20. func (cache *VersionConfigCache) Reload() {
  21. abVers := make([]*abver.AbVer, 0)
  22. md5s := make([]*md5.MD5, 0)
  23. if mgm.Coll(&abver.AbVer{}).SimpleFind(&abVers, bson.M{}) != nil || mgm.Coll(&md5.MD5{}).SimpleFind(&md5s, bson.M{}) != nil {
  24. return
  25. }
  26. newAbVers := make(map[string][]*abver.AbVer)
  27. for _, item := range abVers {
  28. key := configKey(item.Platform)
  29. newAbVers[key] = append(newAbVers[key], item)
  30. }
  31. newMD5s := make(map[string][]*md5.MD5)
  32. for _, item := range md5s {
  33. key := configKey(item.Platform)
  34. newMD5s[key] = append(newMD5s[key], item)
  35. }
  36. cache.Lock()
  37. cache.abVers, cache.md5s = newAbVers, newMD5s
  38. cache.Unlock()
  39. }
  40. func (cache *VersionConfigCache) RefreshAbVer(item *abver.AbVer) {
  41. cache.Reload()
  42. }
  43. func (cache *VersionConfigCache) RefreshMD5(item *md5.MD5) {
  44. cache.Reload()
  45. }
  46. func (cache *VersionConfigCache) GetAbVer(platform, env string) ConfigValue {
  47. cache.RLock()
  48. defer cache.RUnlock()
  49. if value, ok := highestAbVer(cache.abVers[configKey(platform)], env); ok {
  50. return value
  51. }
  52. if strings.TrimSpace(platform) != "" {
  53. value, _ := highestAbVer(cache.abVers[configKey("")], env)
  54. return value
  55. }
  56. return ConfigValue{}
  57. }
  58. func highestAbVer(items []*abver.AbVer, env string) (ConfigValue, bool) {
  59. var result *abver.AbVer
  60. for _, item := range items {
  61. if item.External && configEnvMatches(item.EnvVersions, env) && (result == nil || CompareConfigVersion(item.Version, result.Version) > 0) {
  62. result = item
  63. }
  64. }
  65. if result == nil {
  66. return ConfigValue{}, false
  67. }
  68. return ConfigValue{Version: result.Version, Value: result.Value}, true
  69. }
  70. func (cache *VersionConfigCache) GetMD5(platform, env string) ConfigValue {
  71. cache.RLock()
  72. defer cache.RUnlock()
  73. if value, ok := highestMD5(cache.md5s[configKey(platform)], env); ok {
  74. return value
  75. }
  76. if strings.TrimSpace(platform) != "" {
  77. value, _ := highestMD5(cache.md5s[configKey("")], env)
  78. return value
  79. }
  80. return ConfigValue{}
  81. }
  82. func highestMD5(items []*md5.MD5, env string) (ConfigValue, bool) {
  83. var result *md5.MD5
  84. for _, item := range items {
  85. if item.External && configEnvMatches(item.EnvVersions, env) && (result == nil || CompareConfigVersion(item.Version, result.Version) > 0) {
  86. result = item
  87. }
  88. }
  89. if result == nil {
  90. return ConfigValue{}, false
  91. }
  92. return ConfigValue{Version: result.Version, Value: result.Value}, true
  93. }
  94. func configKey(platform string) string { return strings.TrimSpace(platform) }
  95. func configEnvMatches(envs []string, env string) bool {
  96. if len(envs) == 0 {
  97. return true
  98. }
  99. for _, item := range envs {
  100. if NormalizeEnvVersion(item) == env {
  101. return true
  102. }
  103. }
  104. return false
  105. }
  106. func CompareConfigVersion(left, right string) int {
  107. l, r := strings.Split(left, "."), strings.Split(right, ".")
  108. for i := 0; i < len(l) || i < len(r); i++ {
  109. lv, rv := 0, 0
  110. if i < len(l) {
  111. for _, c := range l[i] {
  112. if c >= '0' && c <= '9' {
  113. lv = lv*10 + int(c-'0')
  114. }
  115. }
  116. }
  117. if i < len(r) {
  118. for _, c := range r[i] {
  119. if c >= '0' && c <= '9' {
  120. rv = rv*10 + int(c-'0')
  121. }
  122. }
  123. }
  124. if lv != rv {
  125. if lv < rv {
  126. return -1
  127. }
  128. return 1
  129. }
  130. }
  131. return 0
  132. }