From 7e522cf71c9eac90149ed513487d65f23b6a28b6 Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Fri, 21 Feb 2020 10:55:45 +0300 Subject: Slight update Start to write tests --- service/reload/service.go | 2 +- service/reload/watcher.go | 86 ++++-------------------------------------- service/reload/watcher_test.go | 8 ++++ 3 files changed, 16 insertions(+), 80 deletions(-) create mode 100644 service/reload/watcher_test.go (limited to 'service') diff --git a/service/reload/service.go b/service/reload/service.go index 16648007..12b157ab 100644 --- a/service/reload/service.go +++ b/service/reload/service.go @@ -80,7 +80,7 @@ func (s *Service) Serve() error { for { select { case e := <-s.watcher.Event: - println(fmt.Sprintf("Service is:%s, path:%s, name:%s", e.service, e.path, e.info.Name())) + println(fmt.Sprintf("[UPDATE] Service: %s, path to file: %s, filename: %s", e.service, e.path, e.info.Name())) srv := s.reloadConfig.Services[e.service] diff --git a/service/reload/watcher.go b/service/reload/watcher.go index da8007a3..a9005a00 100644 --- a/service/reload/watcher.go +++ b/service/reload/watcher.go @@ -65,6 +65,7 @@ type Watcher struct { // Options is used to set Watcher Options type Options func(*Watcher) +// NewWatcher returns new instance of File Watcher func NewWatcher(workDir string, configs []WatcherConfig, options ...Options) (*Watcher, error) { w := &Watcher{ Event: make(chan Event), @@ -98,6 +99,7 @@ func NewWatcher(workDir string, configs []WatcherConfig, options ...Options) (*W return w, nil } +// initFs makes initial map with files func (w *Watcher) initFs() error { for srvName, config := range w.watcherConfigs { fileList, err := w.retrieveFileList(srvName, config) @@ -112,6 +114,7 @@ func (w *Watcher) initFs() error { return nil } +// ConvertIgnored is used to convert slice to map with ignored files func ConvertIgnored(workdir string, ignored []string) map[string]string { abs, _ := filepath.Abs(workdir) if len(ignored) == 0 { @@ -127,10 +130,6 @@ func ConvertIgnored(workdir string, ignored []string) map[string]string { } -func (w *Watcher) AddWatcherConfig(config WatcherConfig) { - w.watcherConfigs[config.serviceName] = config -} - // https://en.wikipedia.org/wiki/Inotify // SetMaxFileEvents sets max file notify events for Watcher // In case of file watch errors, this value can be increased system-wide @@ -143,81 +142,6 @@ func (w *Watcher) AddWatcherConfig(config WatcherConfig) { // //} -// SetDefaultRootPath is used to set own root path for adding files -func SetDefaultRootPath(path string) Options { - return func(watcher *Watcher) { - watcher.workingDir = path - } -} - -// Add -// name will be current working dir -func (w *Watcher) AddSingle(serviceName, relPath string) error { - absPath, err := filepath.Abs(w.workingDir) - if err != nil { - return err - } - - // full path is workdir/relative_path - fullPath := path.Join(absPath, relPath) - - // Ignored files - // map is to have O(1) when search for file - _, ignored := w.watcherConfigs[serviceName].ignored[fullPath] - if ignored { - return nil - } - - // small optimization for smallvector - //fileList := make(map[string]os.FileInfo, 10) - fileList, err := w.retrieveFilesSingle(serviceName, fullPath) - if err != nil { - return err - } - - for fullPth, file := range fileList { - w.watcherConfigs[serviceName].files[fullPth] = file - } - - return nil -} - -func (w *Watcher) AddRecursive(serviceName string, relPath string) error { - workDirAbs, err := filepath.Abs(w.workingDir) - if err != nil { - return err - } - - fullPath := path.Join(workDirAbs, relPath) - - filesList, err := w.retrieveFilesRecursive(serviceName, fullPath) - if err != nil { - return err - } - - for pathToFile, file := range filesList { - w.watcherConfigs[serviceName].files[pathToFile] = file - } - - return nil -} - -func (w *Watcher) AddIgnored(serviceName string, directories []string) error { - workDirAbs, err := filepath.Abs(w.workingDir) - if err != nil { - return err - } - - // concat wd with relative paths from config - // todo check path for existance - for _, v := range directories { - fullPath := path.Join(workDirAbs, v) - w.watcherConfigs[serviceName].ignored[fullPath] = fullPath - } - - return nil -} - // pass map from outside func (w *Watcher) retrieveFilesSingle(serviceName, path string) (map[string]os.FileInfo, error) { stat, err := os.Stat(path) @@ -297,6 +221,9 @@ func (w *Watcher) waitEvent(d time.Duration) error { ticker.Stop() return nil case <-ticker.C: + // this is not very effective way + // because we have to wait on Lock + // better is to listen files in parallel, but, since that would be used in debug... TODO for serviceName, config := range w.watcherConfigs { go func(sn string, c WatcherConfig) { fileList, _ := w.retrieveFileList(sn, c) @@ -308,6 +235,7 @@ func (w *Watcher) waitEvent(d time.Duration) error { } +// retrieveFileList get file list for service func (w *Watcher) retrieveFileList(serviceName string, config WatcherConfig) (map[string]os.FileInfo, error) { w.mu.Lock() defer w.mu.Unlock() diff --git a/service/reload/watcher_test.go b/service/reload/watcher_test.go new file mode 100644 index 00000000..4e5e3210 --- /dev/null +++ b/service/reload/watcher_test.go @@ -0,0 +1,8 @@ +package reload + +import "testing" + +func Test_Watcher(t *testing.T) { + +} + -- cgit v1.2.3