Remove global config watcher (#8080)
* remove global config watcher * keep config watcher disabled for tests * compile fix * fix resource leak
Этот коммит содержится в:
@@ -173,13 +173,13 @@ func (a *App) SaveConfig(cfg *model.Config, sendConfigChangeClusterMessage bool)
|
||||
return model.NewAppError("saveConfig", "ent.cluster.save_config.error", nil, "", http.StatusForbidden)
|
||||
}
|
||||
|
||||
utils.DisableConfigWatch()
|
||||
a.DisableConfigWatch()
|
||||
a.UpdateConfig(func(update *model.Config) {
|
||||
*update = *cfg
|
||||
})
|
||||
a.PersistConfig()
|
||||
a.ReloadConfig()
|
||||
utils.EnableConfigWatch()
|
||||
a.EnableConfigWatch()
|
||||
|
||||
if a.Metrics != nil {
|
||||
if *a.Config().MetricsSettings.Enable {
|
||||
|
||||
62
app/app.go
62
app/app.go
@@ -4,19 +4,16 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"net"
|
||||
"net/http"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
l4g "github.com/alecthomas/log4go"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/mattermost/mattermost-server/einterfaces"
|
||||
ejobs "github.com/mattermost/mattermost-server/einterfaces/jobs"
|
||||
@@ -65,6 +62,8 @@ type App struct {
|
||||
roles map[string]*model.Role
|
||||
configListenerId string
|
||||
licenseListenerId string
|
||||
disableConfigWatch bool
|
||||
configWatcher *utils.ConfigWatcher
|
||||
|
||||
pluginCommands []*PluginCommand
|
||||
pluginCommandsLock sync.RWMutex
|
||||
@@ -78,7 +77,7 @@ var appCount = 0
|
||||
|
||||
// New creates a new App. You must call Shutdown when you're done with it.
|
||||
// XXX: For now, only one at a time is allowed as some resources are still shared.
|
||||
func New(options ...Option) *App {
|
||||
func New(options ...Option) (*App, error) {
|
||||
appCount++
|
||||
if appCount > 1 {
|
||||
panic("Only one App should exist at a time. Did you forget to call Shutdown()?")
|
||||
@@ -99,11 +98,16 @@ func New(options ...Option) *App {
|
||||
}
|
||||
|
||||
if utils.T == nil {
|
||||
utils.TranslationsPreInit()
|
||||
if err := utils.TranslationsPreInit(); err != nil {
|
||||
return nil, errors.Wrapf(err, "unable to load Mattermost translation files")
|
||||
}
|
||||
}
|
||||
model.AppErrorInit(utils.T)
|
||||
utils.LoadGlobalConfig(app.configFile)
|
||||
utils.InitTranslations(utils.Cfg.LocalizationSettings)
|
||||
app.EnableConfigWatch()
|
||||
if err := utils.InitTranslations(utils.Cfg.LocalizationSettings); err != nil {
|
||||
return nil, errors.Wrapf(err, "unable to load Mattermost translation files")
|
||||
}
|
||||
|
||||
app.configListenerId = utils.AddConfigListener(func(_, _ *model.Config) {
|
||||
app.configOrLicenseListener()
|
||||
@@ -142,7 +146,7 @@ func New(options ...Option) *App {
|
||||
handlers: make(map[string]webSocketHandler),
|
||||
}
|
||||
|
||||
return app
|
||||
return app, nil
|
||||
}
|
||||
|
||||
func (a *App) configOrLicenseListener() {
|
||||
@@ -171,6 +175,8 @@ func (a *App) Shutdown() {
|
||||
utils.RemoveConfigListener(a.configListenerId)
|
||||
utils.RemoveLicenseListener(a.licenseListenerId)
|
||||
l4g.Info(utils.T("api.server.stop_server.stopped.info"))
|
||||
|
||||
a.DisableConfigWatch()
|
||||
}
|
||||
|
||||
var accountMigrationInterface func(*App) einterfaces.AccountMigrationInterface
|
||||
@@ -341,46 +347,6 @@ func (a *App) initJobs() {
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) Config() *model.Config {
|
||||
return utils.Cfg
|
||||
}
|
||||
|
||||
func (a *App) UpdateConfig(f func(*model.Config)) {
|
||||
old := utils.Cfg.Clone()
|
||||
f(utils.Cfg)
|
||||
utils.InvokeGlobalConfigListeners(old, utils.Cfg)
|
||||
}
|
||||
|
||||
func (a *App) PersistConfig() {
|
||||
utils.SaveConfig(a.ConfigFileName(), a.Config())
|
||||
}
|
||||
|
||||
func (a *App) ReloadConfig() {
|
||||
debug.FreeOSMemory()
|
||||
utils.LoadGlobalConfig(a.ConfigFileName())
|
||||
|
||||
// start/restart email batching job if necessary
|
||||
a.InitEmailBatching()
|
||||
}
|
||||
|
||||
func (a *App) ConfigFileName() string {
|
||||
return utils.CfgFileName
|
||||
}
|
||||
|
||||
func (a *App) ClientConfig() map[string]string {
|
||||
return a.clientConfig
|
||||
}
|
||||
|
||||
func (a *App) ClientConfigHash() string {
|
||||
return a.clientConfigHash
|
||||
}
|
||||
|
||||
func (a *App) regenerateClientConfig() {
|
||||
a.clientConfig = utils.GenerateClientConfig(a.Config(), a.DiagnosticId())
|
||||
clientConfigJSON, _ := json.Marshal(a.clientConfig)
|
||||
a.clientConfigHash = fmt.Sprintf("%x", md5.Sum(clientConfigJSON))
|
||||
}
|
||||
|
||||
func (a *App) DiagnosticId() string {
|
||||
return a.diagnosticId
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
l4g "github.com/alecthomas/log4go"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
"github.com/mattermost/mattermost-server/store/storetest"
|
||||
@@ -47,7 +48,8 @@ func TestMain(m *testing.M) {
|
||||
|
||||
func TestAppRace(t *testing.T) {
|
||||
for i := 0; i < 10; i++ {
|
||||
a := New()
|
||||
a, err := New()
|
||||
require.NoError(t, err)
|
||||
a.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.ListenAddress = ":0" })
|
||||
a.StartServer()
|
||||
a.Shutdown()
|
||||
|
||||
@@ -57,13 +57,18 @@ func StopTestStore() {
|
||||
}
|
||||
|
||||
func setupTestHelper(enterprise bool) *TestHelper {
|
||||
var options []Option
|
||||
options := []Option{DisableConfigWatch}
|
||||
if testStore != nil {
|
||||
options = append(options, StoreOverride(testStore))
|
||||
}
|
||||
|
||||
a, err := New(options...)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
th := &TestHelper{
|
||||
App: New(options...),
|
||||
App: a,
|
||||
pluginHooks: make(map[string]plugin.Hooks),
|
||||
}
|
||||
|
||||
|
||||
73
app/config.go
Обычный файл
73
app/config.go
Обычный файл
@@ -0,0 +1,73 @@
|
||||
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
package app
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"runtime/debug"
|
||||
|
||||
l4g "github.com/alecthomas/log4go"
|
||||
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
"github.com/mattermost/mattermost-server/utils"
|
||||
)
|
||||
|
||||
func (a *App) Config() *model.Config {
|
||||
return utils.Cfg
|
||||
}
|
||||
|
||||
func (a *App) UpdateConfig(f func(*model.Config)) {
|
||||
old := utils.Cfg.Clone()
|
||||
f(utils.Cfg)
|
||||
utils.InvokeGlobalConfigListeners(old, utils.Cfg)
|
||||
}
|
||||
|
||||
func (a *App) PersistConfig() {
|
||||
utils.SaveConfig(a.ConfigFileName(), a.Config())
|
||||
}
|
||||
|
||||
func (a *App) ReloadConfig() {
|
||||
debug.FreeOSMemory()
|
||||
utils.LoadGlobalConfig(a.ConfigFileName())
|
||||
|
||||
// start/restart email batching job if necessary
|
||||
a.InitEmailBatching()
|
||||
}
|
||||
|
||||
func (a *App) ConfigFileName() string {
|
||||
return utils.CfgFileName
|
||||
}
|
||||
|
||||
func (a *App) ClientConfig() map[string]string {
|
||||
return a.clientConfig
|
||||
}
|
||||
|
||||
func (a *App) ClientConfigHash() string {
|
||||
return a.clientConfigHash
|
||||
}
|
||||
|
||||
func (a *App) EnableConfigWatch() {
|
||||
if a.configWatcher == nil && !a.disableConfigWatch {
|
||||
configWatcher, err := utils.NewConfigWatcher(utils.CfgFileName)
|
||||
if err != nil {
|
||||
l4g.Error(err)
|
||||
}
|
||||
a.configWatcher = configWatcher
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) DisableConfigWatch() {
|
||||
if a.configWatcher != nil {
|
||||
a.configWatcher.Close()
|
||||
a.configWatcher = nil
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) regenerateClientConfig() {
|
||||
a.clientConfig = utils.GenerateClientConfig(a.Config(), a.DiagnosticId())
|
||||
clientConfigJSON, _ := json.Marshal(a.clientConfig)
|
||||
a.clientConfigHash = fmt.Sprintf("%x", md5.Sum(clientConfigJSON))
|
||||
}
|
||||
@@ -35,3 +35,7 @@ func ConfigFile(file string) Option {
|
||||
a.configFile = file
|
||||
}
|
||||
}
|
||||
|
||||
func DisableConfigWatch(a *App) {
|
||||
a.disableConfigWatch = true
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user