Fixing config file watch and config reload on license save (#5954)
* Fixing config file watch and config reload on license save * Fixing config file watch and config reload on license save * Fixing build error * Fixing locking issue
Этот коммит содержится в:
коммит произвёл
Christopher Speller
родитель
c4fd04efb6
Коммит
f0e451a2d3
@@ -68,8 +68,6 @@ func addLicense(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
c.Err = err
|
c.Err = err
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
app.ReloadConfig()
|
|
||||||
app.InvalidateAllCaches()
|
|
||||||
c.LogAudit("success")
|
c.LogAudit("success")
|
||||||
w.Write([]byte(license.ToJson()))
|
w.Write([]byte(license.ToJson()))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -137,8 +137,10 @@ func SaveConfig(cfg *model.Config) *model.AppError {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//oldCfg := utils.Cfg
|
//oldCfg := utils.Cfg
|
||||||
|
utils.DisableConfigWatch()
|
||||||
utils.SaveConfig(utils.CfgFileName, cfg)
|
utils.SaveConfig(utils.CfgFileName, cfg)
|
||||||
utils.LoadConfig(utils.CfgFileName)
|
utils.LoadConfig(utils.CfgFileName)
|
||||||
|
utils.EnableConfigWatch()
|
||||||
|
|
||||||
if einterfaces.GetMetricsInterface() != nil {
|
if einterfaces.GetMetricsInterface() != nil {
|
||||||
if *utils.Cfg.MetricsSettings.Enable {
|
if *utils.Cfg.MetricsSettings.Enable {
|
||||||
|
|||||||
@@ -100,6 +100,9 @@ func SaveLicense(licenseBytes []byte) (*model.License, *model.AppError) {
|
|||||||
return nil, model.NewLocAppError("addLicense", model.INVALID_LICENSE_ERROR, nil, "")
|
return nil, model.NewLocAppError("addLicense", model.INVALID_LICENSE_ERROR, nil, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ReloadConfig()
|
||||||
|
InvalidateAllCaches()
|
||||||
|
|
||||||
return license, nil
|
return license, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ func doLoadConfig(filename string) (err string) {
|
|||||||
utils.TranslationsPreInit()
|
utils.TranslationsPreInit()
|
||||||
utils.EnableConfigFromEnviromentVars()
|
utils.EnableConfigFromEnviromentVars()
|
||||||
utils.LoadConfig(filename)
|
utils.LoadConfig(filename)
|
||||||
|
utils.InitializeConfigWatch()
|
||||||
utils.EnableConfigWatch()
|
utils.EnableConfigWatch()
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
l4g "github.com/alecthomas/log4go"
|
l4g "github.com/alecthomas/log4go"
|
||||||
"github.com/fsnotify/fsnotify"
|
"github.com/fsnotify/fsnotify"
|
||||||
@@ -28,6 +29,8 @@ const (
|
|||||||
LOG_ROTATE_SIZE = 10000
|
LOG_ROTATE_SIZE = 10000
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var cfgMutex = &sync.Mutex{}
|
||||||
|
var watcher *fsnotify.Watcher
|
||||||
var Cfg *model.Config = &model.Config{}
|
var Cfg *model.Config = &model.Config{}
|
||||||
var CfgDiagnosticId = ""
|
var CfgDiagnosticId = ""
|
||||||
var CfgHash = ""
|
var CfgHash = ""
|
||||||
@@ -140,6 +143,9 @@ func GetLogFileLocation(fileLocation string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func SaveConfig(fileName string, config *model.Config) *model.AppError {
|
func SaveConfig(fileName string, config *model.Config) *model.AppError {
|
||||||
|
cfgMutex.Lock()
|
||||||
|
defer cfgMutex.Unlock()
|
||||||
|
|
||||||
b, err := json.MarshalIndent(config, "", " ")
|
b, err := json.MarshalIndent(config, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return model.NewLocAppError("SaveConfig", "utils.config.save_config.saving.app_error",
|
return model.NewLocAppError("SaveConfig", "utils.config.save_config.saving.app_error",
|
||||||
@@ -161,18 +167,72 @@ func EnableConfigFromEnviromentVars() {
|
|||||||
viper.AutomaticEnv()
|
viper.AutomaticEnv()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func InitializeConfigWatch() {
|
||||||
|
cfgMutex.Lock()
|
||||||
|
defer cfgMutex.Unlock()
|
||||||
|
|
||||||
|
if watcher == nil {
|
||||||
|
var err error
|
||||||
|
watcher, err = fsnotify.NewWatcher()
|
||||||
|
if err != nil {
|
||||||
|
l4g.Error(fmt.Sprintf("Failed to watch config file at %v with err=%v", CfgFileName, err.Error()))
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
configFile := filepath.Clean(CfgFileName)
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case event := <-watcher.Events:
|
||||||
|
// we only care about the config file
|
||||||
|
if filepath.Clean(event.Name) == configFile {
|
||||||
|
if event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Create == fsnotify.Create {
|
||||||
|
l4g.Info(fmt.Sprintf("Config file watcher detected a change reloading %v", CfgFileName))
|
||||||
|
|
||||||
|
if configReadErr := viper.ReadInConfig(); configReadErr == nil {
|
||||||
|
LoadConfig(CfgFileName)
|
||||||
|
} else {
|
||||||
|
l4g.Error(fmt.Sprintf("Failed to read while watching config file at %v with err=%v", CfgFileName, configReadErr.Error()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case err := <-watcher.Errors:
|
||||||
|
l4g.Error(fmt.Sprintf("Failed while watching config file at %v with err=%v", CfgFileName, err.Error()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func EnableConfigWatch() {
|
func EnableConfigWatch() {
|
||||||
viper.WatchConfig()
|
cfgMutex.Lock()
|
||||||
viper.OnConfigChange(func(e fsnotify.Event) {
|
defer cfgMutex.Unlock()
|
||||||
l4g.Info(fmt.Sprintf("Config file watcher detected a change reloading %v", CfgFileName))
|
|
||||||
LoadConfig(CfgFileName)
|
configFile := filepath.Clean(CfgFileName)
|
||||||
})
|
configDir, _ := filepath.Split(configFile)
|
||||||
|
|
||||||
|
if watcher != nil {
|
||||||
|
watcher.Add(configDir)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func DisableConfigWatch() {
|
||||||
|
cfgMutex.Lock()
|
||||||
|
defer cfgMutex.Unlock()
|
||||||
|
|
||||||
|
if watcher != nil {
|
||||||
|
configFile := filepath.Clean(CfgFileName)
|
||||||
|
configDir, _ := filepath.Split(configFile)
|
||||||
|
watcher.Remove(configDir)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadConfig will try to search around for the corresponding config file.
|
// LoadConfig will try to search around for the corresponding config file.
|
||||||
// It will search /tmp/fileName then attempt ./config/fileName,
|
// It will search /tmp/fileName then attempt ./config/fileName,
|
||||||
// then ../config/fileName and last it will look at fileName
|
// then ../config/fileName and last it will look at fileName
|
||||||
func LoadConfig(fileName string) {
|
func LoadConfig(fileName string) {
|
||||||
|
cfgMutex.Lock()
|
||||||
|
defer cfgMutex.Unlock()
|
||||||
|
|
||||||
fileNameWithExtension := filepath.Base(fileName)
|
fileNameWithExtension := filepath.Base(fileName)
|
||||||
fileExtension := filepath.Ext(fileNameWithExtension)
|
fileExtension := filepath.Ext(fileNameWithExtension)
|
||||||
@@ -221,9 +281,11 @@ func LoadConfig(fileName string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if needSave {
|
if needSave {
|
||||||
|
cfgMutex.Unlock()
|
||||||
if err := SaveConfig(CfgFileName, &config); err != nil {
|
if err := SaveConfig(CfgFileName, &config); err != nil {
|
||||||
l4g.Warn(T(err.Id))
|
l4g.Warn(T(err.Id))
|
||||||
}
|
}
|
||||||
|
cfgMutex.Lock()
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := ValidateLocales(&config); err != nil {
|
if err := ValidateLocales(&config); err != nil {
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user