configmgr.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package singlematch
  2. import (
  3. "encoding/json"
  4. "os"
  5. "time"
  6. "bet24.com/log"
  7. platformconfig "bet24.com/servers/micros/platformconfig/proto"
  8. )
  9. const config_key = "matches_singlematch"
  10. const refresh_config_sec = 1800
  11. var cfgmgr *configmgr
  12. func getConfigManager() *configmgr {
  13. if cfgmgr == nil {
  14. cfgmgr = new(configmgr)
  15. cfgmgr.ctor()
  16. }
  17. return cfgmgr
  18. }
  19. type configmgr struct {
  20. Matches []SingleMatchConfig
  21. configString string
  22. }
  23. func (cm *configmgr) ctor() {
  24. cm.loadConfig()
  25. }
  26. func (cm *configmgr) loadConfig() {
  27. time.AfterFunc(time.Second*600, cm.loadConfig)
  28. configString := platformconfig.GetConfig(config_key)
  29. if configString == "" {
  30. data, err := os.ReadFile("serviceconf/matches_singlematch.json")
  31. if err != nil {
  32. log.Release("configmgr.loadData read config failed")
  33. return
  34. }
  35. configString = string(data)
  36. if configString != "" {
  37. platformconfig.SetConfig(config_key, configString)
  38. }
  39. }
  40. if configString == cm.configString {
  41. return
  42. }
  43. cm.configString = configString
  44. // 读取配置
  45. err := json.Unmarshal([]byte(configString), &cm.Matches)
  46. if err != nil {
  47. log.Release("configmgr.loadData Unmarshal failed %s", configString)
  48. }
  49. // 设置Index
  50. for i := 0; i < len(cm.Matches); i++ {
  51. cm.Matches[i].createRoundIndex()
  52. }
  53. }
  54. func (cm *configmgr) dumpSys(param string) {
  55. log.Release("-------------------------------")
  56. log.Release("configmgr.dumpSys")
  57. defer func() {
  58. log.Release("+++++++++++++++++++++++++++++++")
  59. log.Release("")
  60. }()
  61. for _, v := range cm.Matches {
  62. log.Release("MatchId:%d", v.MatchId)
  63. v.dump()
  64. }
  65. }
  66. func (cm *configmgr) getMatch(matchId int) *SingleMatchConfig {
  67. for _, v := range cm.Matches {
  68. if v.MatchId == matchId {
  69. return &v
  70. }
  71. }
  72. return nil
  73. }