diff options
Diffstat (limited to 'src/rhimportd/ctrlWatchDir.go')
-rw-r--r-- | src/rhimportd/ctrlWatchDir.go | 60 |
1 files changed, 24 insertions, 36 deletions
diff --git a/src/rhimportd/ctrlWatchDir.go b/src/rhimportd/ctrlWatchDir.go index 87246a7..a8d06ed 100644 --- a/src/rhimportd/ctrlWatchDir.go +++ b/src/rhimportd/ctrlWatchDir.go @@ -25,8 +25,6 @@ package main import ( - "code.helsinki.at/rhrd-go/rddb" - "code.helsinki.at/rhrd-go/rhimport" "encoding/json" "fmt" "net/http" @@ -34,6 +32,8 @@ import ( "path/filepath" "strings" "time" + + "code.helsinki.at/rhrd-go/rhimport" ) type watchDirRequestData struct { @@ -101,7 +101,7 @@ func watchDirResponse(filename string, result *rhimport.Result) { watchDirWriteResponse(filename, &watchDirResponseData{result.ResponseCode, result.ErrorString, result.Cart, result.Cut, result.SourceFile}) } -func watchDirParseRequest(conf *rhimport.Config, db *rddb.DB, req *os.File) (ctx *rhimport.Context, err error) { +func watchDirParseRequest(conf *rhimport.Config, req *os.File) (ctx *rhimport.Context, err error) { decoder := json.NewDecoder(req) reqdata := newWatchDirRequestData(conf) @@ -110,9 +110,7 @@ func watchDirParseRequest(conf *rhimport.Config, db *rddb.DB, req *os.File) (ctx return } - logname := "watch-" + filepath.Base(req.Name()) - logname = strings.TrimSuffix(logname, filepath.Ext(logname)) - ctx = rhimport.NewContext(conf, db, getStdLog(logname+"-std"), getDbgLog(logname+"-dbg")) + ctx = rhimport.NewContext(conf, nil, getStdLog("sess-%s-std"), getDbgLog("sess-%s-dbg")) ctx.UserName = reqdata.UserName ctx.Trusted = true ctx.ShowId = reqdata.ShowId @@ -132,44 +130,34 @@ func watchDirParseRequest(conf *rhimport.Config, db *rddb.DB, req *os.File) (ctx return } -func watchDirHandler(conf *rhimport.Config, db *rddb.DB, ctx *rhimport.Context, filename string) { +func watchDirDone(res rhimport.Result, userdata interface{}) bool { + c := userdata.(chan<- rhimport.Result) + c <- res + return true +} + +func watchDirHandler(conf *rhimport.Config, sessions *rhimport.SessionStore, ctx *rhimport.Context, filename string) { rhdl.Printf("WatchDirHandler: request for '%s'", filename) - var err error - if err = ctx.SanityCheck(); err != nil { - watchDirErrorResponse(filename, http.StatusBadRequest, err.Error(), "") + _, s, code, errstring := sessions.New(ctx, "") + if code != http.StatusOK { + watchDirErrorResponse(filename, code, errstring, "") return } - var res *rhimport.Result - if res, err = rhimport.FetchFile(ctx); err != nil { + donechan := make(chan rhimport.Result, 1) + if err := s.AddDoneHandler((chan<- rhimport.Result)(donechan), watchDirDone); err != nil { watchDirErrorResponse(filename, http.StatusInternalServerError, err.Error(), "") return } - if res.ResponseCode != http.StatusOK { - watchDirErrorResponse(filename, res.ResponseCode, res.ErrorString, "") - return - } - - if res, err = rhimport.NormalizeFile(ctx); err != nil { - watchDirErrorResponse(filename, http.StatusInternalServerError, err.Error(), "") - return - } - if res.ResponseCode != http.StatusOK { - watchDirErrorResponse(filename, res.ResponseCode, res.ErrorString, "") - return - } - - if res, err = rhimport.ImportFile(ctx); err != nil { - watchDirErrorResponse(filename, http.StatusInternalServerError, err.Error(), res.SourceFile) - return - } - watchDirResponse(filename, res) + s.Run(0) + res := <-donechan + watchDirResponse(filename, &res) return } -func watchDirRun(dirname string, conf *rhimport.Config, db *rddb.DB) bool { +func watchDirRun(dirname string, conf *rhimport.Config, sessions *rhimport.SessionStore) bool { dir, err := os.Open(dirname) if err != nil { rhl.Printf("watch-dir-ctrl: %s", err) @@ -202,11 +190,11 @@ func watchDirRun(dirname string, conf *rhimport.Config, db *rddb.DB) bool { rhl.Printf("watch-dir-ctrl: error reading new file: %s", err) continue } - if ctx, err := watchDirParseRequest(conf, db, file); err == nil { + if ctx, err := watchDirParseRequest(conf, file); err == nil { file.Close() dstname := strings.TrimSuffix(srcname, ".new") + ".running" os.Rename(srcname, dstname) - go watchDirHandler(conf, db, ctx, dstname) + go watchDirHandler(conf, sessions, 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) @@ -216,12 +204,12 @@ func watchDirRun(dirname string, conf *rhimport.Config, db *rddb.DB) bool { return true } -func StartWatchDir(dirname string, conf *rhimport.Config, db *rddb.DB) { +func StartWatchDir(dirname string, conf *rhimport.Config, sessions *rhimport.SessionStore) { for { rhl.Printf("watch-dir-ctrl: watching for files in %s", dirname) t := time.NewTicker(1 * time.Second) for { - if !watchDirRun(dirname, conf, db) { + if !watchDirRun(dirname, conf, sessions) { break } <-t.C |