From 633022fd6f9d4a2d5b715e2eaf452bb0d5097861 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Fri, 24 Jun 2016 17:30:52 +0200 Subject: refactored watch-dir control to always reopen the directory for every scan run diff --git a/src/rhimportd/ctrlWatchDir.go b/src/rhimportd/ctrlWatchDir.go index 14d1c1f..6985a74 100644 --- a/src/rhimportd/ctrlWatchDir.go +++ b/src/rhimportd/ctrlWatchDir.go @@ -163,74 +163,66 @@ func watchDirHandler(conf *rhimport.Config, db *rddb.DBChan, ctx *rhimport.Conte return } -func watchDirRun(dir *os.File, conf *rhimport.Config, db *rddb.DBChan) { - rhl.Printf("watch-dir-ctrl: watching for files in %s", dir.Name()) - t := time.NewTicker(1 * time.Second) - defer t.Stop() - for { - var err error - if _, err = dir.Seek(0, 0); err != nil { - rhl.Printf("watch-dir-ctrl: reading directory contents failed: %s", err) - return - } +func watchDirRun(dirname string, conf *rhimport.Config, db *rddb.DBChan) bool { + dir, err := os.Open(dirname) + if err != nil { + rhl.Printf("watch-dir-ctrl: %s", err) + return false + } + defer dir.Close() - var names []string - if names, err = dir.Readdirnames(0); err != nil { - rhl.Printf("watch-dir-ctrl: reading directory contents failed: %s", err) - return - } + var di os.FileInfo + if di, err = dir.Stat(); err != nil { + rhl.Printf("watch-dir-ctrl: %s", err) + return false + } + if !di.IsDir() { + rhl.Printf("watch-dir-ctrl: %s is not a directory", dirname) + return false + } - for _, name := range names { - if strings.HasSuffix(name, ".new") { - srcname := filepath.Join(dir.Name(), name) - - rhdl.Printf("watch-dir-ctrl: found new file %s", srcname) - var file *os.File - if file, err = os.Open(srcname); err != nil { - rhl.Printf("watch-dir-ctrl: error reading new file: %s", err) - continue - } - if ctx, err := watchDirParseRequest(conf, db, file); err == nil { - file.Close() - dstname := strings.TrimSuffix(srcname, ".new") + ".running" - os.Rename(srcname, dstname) - go watchDirHandler(conf, db, ctx, dstname) - } else { // ignoring files with json errors -> maybe the file has not been written completely - file.Close() - rhdl.Printf("watch-dir-ctrl: new file %s parser error: %s, ignoring for now", srcname, err) - } + var names []string + if names, err = dir.Readdirnames(0); err != nil { + rhl.Printf("watch-dir-ctrl: reading directory contents failed: %s", err) + return false + } + for _, name := range names { + if strings.HasSuffix(name, ".new") { + srcname := filepath.Join(dir.Name(), name) + + rhdl.Printf("watch-dir-ctrl: found new file %s", srcname) + var file *os.File + if file, err = os.Open(srcname); err != nil { + rhl.Printf("watch-dir-ctrl: error reading new file: %s", err) + continue + } + if ctx, err := watchDirParseRequest(conf, db, file); err == nil { + file.Close() + dstname := strings.TrimSuffix(srcname, ".new") + ".running" + os.Rename(srcname, dstname) + go watchDirHandler(conf, db, ctx, dstname) + } else { // ignoring files with json errors -> maybe the file has not been written completely + file.Close() + rhdl.Printf("watch-dir-ctrl: new file %s parser error: %s, ignoring for now", srcname, err) } } - <-t.C } + return true } func StartWatchDir(dirname string, conf *rhimport.Config, db *rddb.DBChan) { - first := true for { - if !first { - time.Sleep(5 * time.Second) - } - first = false - - dir, err := os.Open(dirname) - if err != nil { - rhl.Printf("watch-dir-ctrl: %s", err) - continue - } - if i, err := dir.Stat(); err != nil { - rhl.Printf("watch-dir-ctrl: %s", err) - dir.Close() - continue - } else { - if !i.IsDir() { - rhl.Printf("watch-dir-ctrl: %s is not a directory", dirname) - dir.Close() - continue + rhl.Printf("watch-dir-ctrl: watching for files in %s", dirname) + t := time.NewTicker(1 * time.Second) + for { + if !watchDirRun(dirname, conf, db) { + break } + <-t.C } - watchDirRun(dir, conf, db) - rhdl.Printf("watchDirRun: returned - restring in 5 sec...") - dir.Close() + t.Stop() + + rhdl.Printf("watch-dir-ctrl: restarting in 5 sec...") + time.Sleep(5 * time.Second) } } -- cgit v0.10.2