diff --git a/pkg/api/controller.go b/pkg/api/controller.go index 44c8e8172..24b5ac2cd 100644 --- a/pkg/api/controller.go +++ b/pkg/api/controller.go @@ -301,10 +301,6 @@ func (c *Controller) Init() error { } } - if err := c.HTPasswdWatcher.Start(); err != nil { - return err - } - return nil } @@ -463,10 +459,6 @@ func (c *Controller) Shutdown() { ctx := context.Background() _ = c.Server.Shutdown(ctx) } - - if c.HTPasswdWatcher != nil { - _ = c.HTPasswdWatcher.Stop() - } } // Will stop scheduler and wait for all tasks to finish their work. diff --git a/pkg/api/htpasswd.go b/pkg/api/htpasswd.go index d40c49721..4406730bb 100644 --- a/pkg/api/htpasswd.go +++ b/pkg/api/htpasswd.go @@ -105,11 +105,11 @@ type HTPasswdWatcher struct { htp *HTPasswd filePath string watcher *fsnotify.Watcher - ctx context.Context //nolint: containedctx - ccancel context.CancelFunc + cancel context.CancelFunc log log.Logger } +// NewHTPasswdWatcher create and start watcher func NewHTPasswdWatcher(htp *HTPasswd, filePath string) (*HTPasswdWatcher, error) { watcher, err := fsnotify.NewWatcher() if err != nil { @@ -130,11 +130,38 @@ func NewHTPasswdWatcher(htp *HTPasswd, filePath string) (*HTPasswdWatcher, error htp: htp, filePath: filePath, watcher: watcher, - ctx: ctx, - ccancel: cancel, + cancel: cancel, log: htp.log, } + go func() { + defer ret.watcher.Close() //nolint: errcheck + + for { + select { + case ev := <-ret.watcher.Events: + if ev.Op != fsnotify.Write { + continue + } + + ret.log.Info().Str("htpasswd-file", ret.filePath).Msg("htpasswd file changed, trying to reload config") + + err := ret.htp.Reload(ret.filePath) + if err != nil { + ret.log.Warn().Err(err).Str("htpasswd-file", ret.filePath).Msg("failed to reload file") + } + + case err := <-ret.watcher.Errors: + ret.log.Panic().Err(err).Str("htpasswd-file", ret.filePath).Msg("fsnotfy error while watching config") + + case <-ctx.Done(): + ret.log.Debug().Msg("htpasswd watcher terminating...") + + return + } + } + }() + return ret, nil } @@ -164,42 +191,8 @@ func (s *HTPasswdWatcher) ChangeFile(filePath string) error { return s.htp.Reload(filePath) } -// Start watcher. Can only be run once. -func (s *HTPasswdWatcher) Start() error { - go func() { - defer s.watcher.Close() //nolint: errcheck - - for { - select { - case ev := <-s.watcher.Events: - if ev.Op != fsnotify.Write { - continue - } - - s.log.Info().Str("htpasswd-file", s.filePath).Msg("htpasswd file changed, trying to reload config") - - err := s.htp.Reload(s.filePath) - if err != nil { - s.log.Warn().Err(err).Str("htpasswd-file", s.filePath).Msg("failed to reload file") - } - - case err := <-s.watcher.Errors: - s.log.Panic().Err(err).Str("htpasswd-file", s.filePath).Msg("fsnotfy error while watching config") - - case <-s.ctx.Done(): - s.log.Debug().Msg("htpasswd watcher terminating...") - - return - } - } - }() - - return nil -} - -// Stop watcher. Can only be run once. -func (s *HTPasswdWatcher) Stop() error { - s.ccancel() +func (s *HTPasswdWatcher) Close() error { + s.cancel() return nil }