mirror of
https://github.com/mattermost/mattermost.git
synced 2026-05-28 04:35:04 -04:00
Remove remnants of config watcher (#18067)
The last part of config watching was in advanced logging. We remove it from there too to keep things consistent. https://community-daily.mattermost.com/plugins/focalboard/workspace/zyoahc9uapdn3xdptac6jb69ic?id=285b80a3-257d-41f6-8cf4-ed80ca9d92e5&v=495cdb4d-c13a-4992-8eb9-80cfee2819a4&c=a1b58527-6a3d-4faa-8144-166a3698731f ```release-note Removed config watch feature from advanced logging config. ```
This commit is contained in:
parent
868b8d91db
commit
99bb6084b3
3 changed files with 2 additions and 171 deletions
|
|
@ -110,8 +110,7 @@ type fileSrc struct {
|
|||
mutex sync.RWMutex
|
||||
cfg mlog.LogTargetCfg
|
||||
|
||||
path string
|
||||
watcher *watcher
|
||||
path string
|
||||
}
|
||||
|
||||
func newFileSrc(path string, configStore *Store) (*fileSrc, error) {
|
||||
|
|
@ -156,24 +155,6 @@ func (src *fileSrc) Set(path string, configStore *Store) error {
|
|||
src.mutex.Lock()
|
||||
defer src.mutex.Unlock()
|
||||
|
||||
if src.watcher != nil {
|
||||
if err = src.watcher.Close(); err != nil {
|
||||
mlog.Error("Failed to close watcher", mlog.Err(err))
|
||||
}
|
||||
src.watcher = nil
|
||||
}
|
||||
|
||||
watcher, err := newWatcher(path, func() {
|
||||
if serr := src.Set(path, configStore); serr != nil {
|
||||
mlog.Error("Failed to reload file on change", mlog.String("path", path), mlog.Err(serr))
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
src.watcher = watcher
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
@ -188,14 +169,7 @@ func (src *fileSrc) set(cfg mlog.LogTargetCfg) {
|
|||
|
||||
// Close cleans up resources.
|
||||
func (src *fileSrc) Close() error {
|
||||
var err error
|
||||
src.mutex.Lock()
|
||||
defer src.mutex.Unlock()
|
||||
if src.watcher != nil {
|
||||
err = src.watcher.Close()
|
||||
src.watcher = nil
|
||||
}
|
||||
return err
|
||||
return nil
|
||||
}
|
||||
|
||||
func logTargetCfgFromJSON(data []byte) (mlog.LogTargetCfg, error) {
|
||||
|
|
|
|||
|
|
@ -1,80 +0,0 @@
|
|||
// 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/v6/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
|
||||
}
|
||||
|
|
@ -1,63 +0,0 @@
|
|||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package config
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestWatcherInvalidDirectory(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping watcher test in short mode")
|
||||
}
|
||||
|
||||
callback := func() {}
|
||||
_, err := newWatcher("/does/not/exist", callback)
|
||||
require.Error(t, err, "should have failed to watch a non-existent directory")
|
||||
}
|
||||
|
||||
func TestWatcher(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping watcher test in short mode")
|
||||
}
|
||||
|
||||
tempDir, err := ioutil.TempDir("", "TestWatcher")
|
||||
require.NoError(t, err)
|
||||
defer os.RemoveAll(tempDir)
|
||||
|
||||
f, err := ioutil.TempFile(tempDir, "TestWatcher")
|
||||
require.NoError(t, err)
|
||||
defer f.Close()
|
||||
defer os.Remove(f.Name())
|
||||
|
||||
called := make(chan bool)
|
||||
callback := func() {
|
||||
called <- true
|
||||
}
|
||||
watcher, err := newWatcher(f.Name(), callback)
|
||||
require.NoError(t, err)
|
||||
defer watcher.Close()
|
||||
|
||||
// Write to a different file
|
||||
ioutil.WriteFile(filepath.Join(tempDir, "unrelated"), []byte("data"), 0644)
|
||||
select {
|
||||
case <-called:
|
||||
require.Fail(t, "callback should not have been called for unrelated file")
|
||||
case <-time.After(1 * time.Second):
|
||||
}
|
||||
|
||||
// Write to the watched file
|
||||
ioutil.WriteFile(f.Name(), []byte("data"), 0644)
|
||||
select {
|
||||
case <-called:
|
||||
case <-time.After(5 * time.Second):
|
||||
require.Fail(t, "callback should have been called when file written")
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue