diff options
author | Christian Pointner <equinox@helsinki.at> | 2016-06-24 15:30:52 (GMT) |
---|---|---|
committer | Christian Pointner <equinox@helsinki.at> | 2016-06-24 15:30:52 (GMT) |
commit | 633022fd6f9d4a2d5b715e2eaf452bb0d5097861 (patch) | |
tree | c9e20ee1d3a77feb1c854686b403d2f757c9abc7 /src | |
parent | 3f38e00703caf05a1867070873c8715438b41264 (diff) |
refactored watch-dir control to always reopen the directory for every scan run
Diffstat (limited to 'src')
-rw-r--r-- | src/rhimportd/ctrlWatchDir.go | 108 |
1 files changed, 50 insertions, 58 deletions
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) } } |