conf.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package conf
  2. import (
  3. "flag"
  4. "regexp"
  5. "sync"
  6. "time"
  7. "go-common/app/tool/saga/model"
  8. "go-common/library/cache/memcache"
  9. "go-common/library/cache/redis"
  10. "go-common/library/conf"
  11. "go-common/library/database/hbase.v2"
  12. "go-common/library/database/orm"
  13. "go-common/library/log"
  14. bm "go-common/library/net/http/blademaster"
  15. "go-common/library/net/trace"
  16. xtime "go-common/library/time"
  17. "github.com/BurntSushi/toml"
  18. "github.com/pkg/errors"
  19. )
  20. const (
  21. configKey = "saga.toml"
  22. )
  23. var (
  24. confPath string
  25. client *conf.Client
  26. // Conf store the global config
  27. Conf = &Config{}
  28. reload chan bool
  29. )
  30. func init() {
  31. flag.StringVar(&confPath, "conf", "", "config path")
  32. reload = make(chan bool, 10)
  33. }
  34. // Config def.
  35. type Config struct {
  36. Tracer *trace.Config
  37. BM *bm.ServerConfig
  38. HTTPClient *bm.ClientConfig
  39. Memcache *Memcache
  40. Redis *redis.Config
  41. HBase *HBaseConfig
  42. Log *log.Config
  43. Property *Property
  44. sync.RWMutex
  45. // orm
  46. ORM *orm.Config
  47. }
  48. // Memcache config.
  49. type Memcache struct {
  50. MR *memcache.Config
  51. MRRecordExpire xtime.Duration
  52. }
  53. // HBaseConfig for new hbase client.
  54. type HBaseConfig struct {
  55. *hbase.Config
  56. WriteTimeout xtime.Duration
  57. ReadTimeout xtime.Duration
  58. }
  59. // Property config for biz logic.
  60. type Property struct {
  61. TaskInterval xtime.Duration // 任务轮询时间
  62. TaskTimeout xtime.Duration // 任务超时时间
  63. PollPipeline xtime.Duration //pipeline轮询间隔时间
  64. Gitlab *struct {
  65. API string // gitlab api host
  66. Token string // saga 账户 access token
  67. }
  68. WebHooks []*model.WebHook
  69. Mail *struct {
  70. Host string
  71. Port int
  72. Address string
  73. Pwd string
  74. Name string
  75. }
  76. HealthCheck *struct {
  77. CheckCron string
  78. AlertAddrs []*model.MailAddress
  79. }
  80. ReportRequiredVisible *struct {
  81. CheckCron string
  82. AlertAddrs []*model.MailAddress
  83. }
  84. SyncContact *struct {
  85. CheckCron string
  86. }
  87. UT *struct {
  88. Rate float64
  89. }
  90. Wechat *model.AppConfig
  91. Contact *model.AppConfig
  92. Repos []*model.RepoConfig
  93. }
  94. // Init init conf.
  95. func Init() (err error) {
  96. if confPath == "" {
  97. return configCenter()
  98. }
  99. if _, err = toml.DecodeFile(confPath, &Conf); err != nil {
  100. log.Error("toml.DecodeFile(%s) err(%+v)", confPath, err)
  101. return
  102. }
  103. Conf = doDefault(Conf)
  104. return
  105. }
  106. func configCenter() (err error) {
  107. if client, err = conf.New(); err != nil {
  108. panic(err)
  109. }
  110. if err = load(); err != nil {
  111. return
  112. }
  113. //client.WatchAll()
  114. client.Watch(configKey)
  115. go func() {
  116. for range client.Event() {
  117. log.Info("config reload")
  118. if err = load(); err != nil {
  119. log.Error("config reload error (+%v)", err)
  120. } else {
  121. reload <- true
  122. }
  123. }
  124. }()
  125. return
  126. }
  127. func load() (err error) {
  128. var (
  129. s string
  130. ok bool
  131. tmpConf *Config
  132. )
  133. if s, ok = client.Value(configKey); !ok {
  134. err = errors.Errorf("load config center error [%s]", configKey)
  135. return
  136. }
  137. if _, err = toml.Decode(s, &tmpConf); err != nil {
  138. err = errors.Wrapf(err, "could not decode config err(%+v)", err)
  139. return
  140. }
  141. Conf = doDefault(tmpConf)
  142. return
  143. }
  144. func doDefault(c *Config) *Config {
  145. if int64(c.Property.TaskInterval) == 0 {
  146. c.Property.TaskInterval = xtime.Duration(3 * time.Second)
  147. }
  148. if int64(c.Property.TaskTimeout) == 0 {
  149. c.Property.TaskTimeout = xtime.Duration(time.Minute)
  150. }
  151. if int64(c.Property.PollPipeline) == 0 {
  152. c.Property.PollPipeline = xtime.Duration(10 * time.Second)
  153. }
  154. for _, r := range c.Property.Repos {
  155. if r.GName == "" {
  156. r.GName = r.Name
  157. }
  158. if r.Language == "" {
  159. r.Language = "any"
  160. }
  161. if r.MinReviewer < 0 {
  162. r.MinReviewer = 0
  163. }
  164. if r.LockTimeout == 0 {
  165. r.LockTimeout = 600
  166. }
  167. if len(r.AuthBranches) == 0 {
  168. r.AuthBranches = []string{"master"}
  169. }
  170. if len(r.TargetBranches) == 0 {
  171. r.TargetBranches = r.AuthBranches
  172. }
  173. for _, b := range r.TargetBranches {
  174. r.TargetBranchRegexes = append(r.TargetBranchRegexes, regexp.MustCompile(b))
  175. }
  176. }
  177. return c
  178. }
  179. // ReloadEvents return the reload chan
  180. func ReloadEvents() <-chan bool {
  181. return reload
  182. }