Files
mostlymatter/config/watcher.go
Claudio Costa 3681cd3688 [MM-32390] Config logic refactor (#17578)
* Replace config generator

* Cleanup

* Some renaming and docs additions to add clarity

* Cleanup logging related methods

* Cleanup emitter

* Fix TestDefaultsGenerator

* Move feature flags synchronization logic out of config package

* Remove unnecessary util functions

* Simplify load/set logic

* Refine semantics and add some test to cover them

* Remove unnecessary deep copies

* Improve logic further

* Fix license header

* Review file store tests

* Fix test

* Fix test

* Avoid additional write during initialization

* More consistent naming

* Update app/feature_flags.go

Co-authored-by: Christopher Speller <crspeller@gmail.com>

* Update config/store.go

Co-authored-by: Christopher Speller <crspeller@gmail.com>

* Update config/store.go

Co-authored-by: Christopher Speller <crspeller@gmail.com>

* Update config/store.go

Co-authored-by: Ibrahim Serdar Acikgoz <serdaracikgoz86@gmail.com>

* Move FF synchronizer to its own package

* Remove unidiomatic use of sync.Once

* Add some comments

* Rename function

* More comment

Co-authored-by: Christopher Speller <crspeller@gmail.com>
Co-authored-by: Ibrahim Serdar Acikgoz <serdaracikgoz86@gmail.com>
2021-05-19 13:30:26 +02:00

81 строка
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/v5/shared/mlog"
)
// watcher monitors a file for changes
type watcher struct {
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
}