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
This commit is contained in:
Corey Hulen
2017-04-04 08:24:58 -07:00
committed by Christopher Speller
parent c4fd04efb6
commit f0e451a2d3
5 changed files with 73 additions and 7 deletions

View File

@@ -68,8 +68,6 @@ func addLicense(c *Context, w http.ResponseWriter, r *http.Request) {
c.Err = err
return
} else {
app.ReloadConfig()
app.InvalidateAllCaches()
c.LogAudit("success")
w.Write([]byte(license.ToJson()))
}

View File

@@ -137,8 +137,10 @@ func SaveConfig(cfg *model.Config) *model.AppError {
}
//oldCfg := utils.Cfg
utils.DisableConfigWatch()
utils.SaveConfig(utils.CfgFileName, cfg)
utils.LoadConfig(utils.CfgFileName)
utils.EnableConfigWatch()
if einterfaces.GetMetricsInterface() != nil {
if *utils.Cfg.MetricsSettings.Enable {

View File

@@ -100,6 +100,9 @@ func SaveLicense(licenseBytes []byte) (*model.License, *model.AppError) {
return nil, model.NewLocAppError("addLicense", model.INVALID_LICENSE_ERROR, nil, "")
}
ReloadConfig()
InvalidateAllCaches()
return license, nil
}

View File

@@ -18,6 +18,7 @@ func doLoadConfig(filename string) (err string) {
utils.TranslationsPreInit()
utils.EnableConfigFromEnviromentVars()
utils.LoadConfig(filename)
utils.InitializeConfigWatch()
utils.EnableConfigWatch()
return ""
}

View File

@@ -12,6 +12,7 @@ import (
"path/filepath"
"strconv"
"strings"
"sync"
l4g "github.com/alecthomas/log4go"
"github.com/fsnotify/fsnotify"
@@ -28,6 +29,8 @@ const (
LOG_ROTATE_SIZE = 10000
)
var cfgMutex = &sync.Mutex{}
var watcher *fsnotify.Watcher
var Cfg *model.Config = &model.Config{}
var CfgDiagnosticId = ""
var CfgHash = ""
@@ -140,6 +143,9 @@ func GetLogFileLocation(fileLocation string) string {
}
func SaveConfig(fileName string, config *model.Config) *model.AppError {
cfgMutex.Lock()
defer cfgMutex.Unlock()
b, err := json.MarshalIndent(config, "", " ")
if err != nil {
return model.NewLocAppError("SaveConfig", "utils.config.save_config.saving.app_error",
@@ -161,18 +167,72 @@ func EnableConfigFromEnviromentVars() {
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() {
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
l4g.Info(fmt.Sprintf("Config file watcher detected a change reloading %v", CfgFileName))
LoadConfig(CfgFileName)
})
cfgMutex.Lock()
defer cfgMutex.Unlock()
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.
// It will search /tmp/fileName then attempt ./config/fileName,
// then ../config/fileName and last it will look at fileName
func LoadConfig(fileName string) {
cfgMutex.Lock()
defer cfgMutex.Unlock()
fileNameWithExtension := filepath.Base(fileName)
fileExtension := filepath.Ext(fileNameWithExtension)
@@ -221,9 +281,11 @@ func LoadConfig(fileName string) {
}
if needSave {
cfgMutex.Unlock()
if err := SaveConfig(CfgFileName, &config); err != nil {
l4g.Warn(T(err.Id))
}
cfgMutex.Lock()
}
if err := ValidateLocales(&config); err != nil {