mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
* config file store Introduce an interface and concrete implementation for accessing the config. This mostly maps 1:1 with the exiting usage in `App`, except for internalizing the watcher. A future change will likely eliminate `App.PersistConfig()` and make this implicit on `Set` or `Patch` * experimental file test changes * emoji: move file driver checks from api4 to app It is no longer possible to app.UpdateConfig and provide an invalid configuration, making it hard to test this case. This check doesn't really belong in the api anyway, since it's a configuration validity check and not a permissions check. Either way, the check now occurs at the App level. * api4: generate valid public link salts for test * TestStartServerRateLimiterCriticalError: use mock store to test invalid config * remove config_test.go * remove needsSave, and have Load() save to the backing store as necessary * restore README.md * move ldap UserFilter check to model isValid checks * remove databaseStore until ready * remove unimplemented Patch * simplify unlockOnce implementation * revert forgetting to set s.Ldap * config/file.go: rename ReadOnlyConfigurationError to ErrReadOnlyConfiguration * config: export FileStore * add TestFileStoreSave * improved config/utils test coverage * restore config/README.md copy * tweaks * file store: acquire a write lock on Save/Close to safely close watcher * fix unmarshal_test.go
83 lines
2.0 KiB
Go
83 lines
2.0 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See License.txt for license information.
|
|
|
|
package config
|
|
|
|
import (
|
|
"path/filepath"
|
|
|
|
"github.com/fsnotify/fsnotify"
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/mattermost/mattermost-server/mlog"
|
|
)
|
|
|
|
// watcher monitors a file for changes
|
|
type watcher struct {
|
|
emitter
|
|
|
|
fsWatcher *fsnotify.Watcher
|
|
close chan struct{}
|
|
closed chan struct{}
|
|
}
|
|
|
|
// newWatcher creates a new instance of watcher to monitor for file changes.
|
|
func newWatcher(path string, callback func()) (w *watcher, err error) {
|
|
fsWatcher, err := fsnotify.NewWatcher()
|
|
if err != nil {
|
|
return nil, errors.Wrapf(err, "failed to create fsnotify watcher for %s", path)
|
|
}
|
|
|
|
path = filepath.Clean(path)
|
|
|
|
// Watch the entire containing directory.
|
|
configDir, _ := filepath.Split(path)
|
|
if err := fsWatcher.Add(configDir); err != nil {
|
|
if closeErr := fsWatcher.Close(); closeErr != nil {
|
|
mlog.Error("failed to stop fsnotify watcher for %s", mlog.String("path", path), mlog.Err(closeErr))
|
|
}
|
|
return nil, errors.Wrapf(err, "failed to watch directory %s", configDir)
|
|
}
|
|
|
|
w = &watcher{
|
|
fsWatcher: fsWatcher,
|
|
close: make(chan struct{}),
|
|
closed: make(chan struct{}),
|
|
}
|
|
|
|
go func() {
|
|
defer close(w.closed)
|
|
defer func() {
|
|
if err := fsWatcher.Close(); err != nil {
|
|
mlog.Error("failed to stop fsnotify watcher for %s", mlog.String("path", path))
|
|
}
|
|
}()
|
|
|
|
for {
|
|
select {
|
|
case event := <-fsWatcher.Events:
|
|
// We only care about the given file.
|
|
if filepath.Clean(event.Name) == path {
|
|
if event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Create == fsnotify.Create {
|
|
mlog.Info("Config file watcher detected a change", mlog.String("path", path))
|
|
go callback()
|
|
}
|
|
}
|
|
case err := <-fsWatcher.Errors:
|
|
mlog.Error("Failed while watching config file", mlog.String("path", path), mlog.Err(err))
|
|
case <-w.close:
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
|
|
return w, nil
|
|
}
|
|
|
|
func (w *watcher) Close() error {
|
|
close(w.close)
|
|
<-w.closed
|
|
|
|
return nil
|
|
}
|