diff --git a/config/logging.go b/config/logging.go index 60b67823e3a..9e33daacc0d 100644 --- a/config/logging.go +++ b/config/logging.go @@ -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) { diff --git a/config/watcher.go b/config/watcher.go deleted file mode 100644 index 6c4837c938f..00000000000 --- a/config/watcher.go +++ /dev/null @@ -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 -} diff --git a/config/watcher_test.go b/config/watcher_test.go deleted file mode 100644 index 2eaf1fc54f6..00000000000 --- a/config/watcher_test.go +++ /dev/null @@ -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") - } -}