From 1b9e777c9cec218e040166dd5ae12c5bde843f20 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Fri, 29 Jul 2016 02:04:00 +0200 Subject: watch dir control interface uses session diff --git a/src/rhimportd/ctrlTelnet.go b/src/rhimportd/ctrlTelnet.go index f57fd7b..19d3489 100644 --- a/src/rhimportd/ctrlTelnet.go +++ b/src/rhimportd/ctrlTelnet.go @@ -25,13 +25,14 @@ package main import ( - "code.helsinki.at/rhrd-go/rddb" - "code.helsinki.at/rhrd-go/rhimport" "fmt" - "github.com/spreadspace/telgo" "net/http" "strconv" "strings" + + "code.helsinki.at/rhrd-go/rddb" + "code.helsinki.at/rhrd-go/rhimport" + "github.com/spreadspace/telgo" ) func telnetQuit(c *telgo.Client, args []string, conf *rhimport.Config, db *rddb.DB) bool { 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 diff --git a/src/rhimportd/ctrlWebSimple.go b/src/rhimportd/ctrlWebSimple.go index 86e6893..b50f829 100644 --- a/src/rhimportd/ctrlWebSimple.go +++ b/src/rhimportd/ctrlWebSimple.go @@ -25,12 +25,13 @@ package main import ( - "code.helsinki.at/rhrd-go/rddb" - "code.helsinki.at/rhrd-go/rhimport" "encoding/json" "fmt" "html" "net/http" + + "code.helsinki.at/rhrd-go/rddb" + "code.helsinki.at/rhrd-go/rhimport" ) type webSimpleRequestData struct { @@ -91,7 +92,7 @@ func webSimpleResponse(w http.ResponseWriter, result *rhimport.Result) { encoder.Encode(respdata) } -func webSimpleParseRequest(conf *rhimport.Config, db *rddb.DB, trusted bool, r *http.Request) (ctx *rhimport.Context, err error) { +func webSimpleParseRequest(conf *rhimport.Config, trusted bool, r *http.Request) (ctx *rhimport.Context, err error) { decoder := json.NewDecoder(r.Body) reqdata := newWebSimpleRequestData(conf) if jsonerr := decoder.Decode(reqdata); jsonerr != nil { @@ -132,7 +133,7 @@ func webSimpleHandler(conf *rhimport.Config, db *rddb.DB, sessions *rhimport.Ses var ctx *rhimport.Context var err error - if ctx, err = webSimpleParseRequest(conf, db, trusted, r); err != nil { + if ctx, err = webSimpleParseRequest(conf, trusted, r); err != nil { webSimpleErrorResponse(w, http.StatusBadRequest, err.Error()) return } diff --git a/src/rhimportd/main.go b/src/rhimportd/main.go index 6c68962..41dd36c 100644 --- a/src/rhimportd/main.go +++ b/src/rhimportd/main.go @@ -25,8 +25,6 @@ package main import ( - "code.helsinki.at/rhrd-go/rddb" - "code.helsinki.at/rhrd-go/rhimport" "flag" "fmt" "io/ioutil" @@ -35,6 +33,9 @@ import ( "os/signal" "sync" "time" + + "code.helsinki.at/rhrd-go/rddb" + "code.helsinki.at/rhrd-go/rhimport" ) func getStdLog(name string) *log.Logger { @@ -172,7 +173,7 @@ func main() { go func() { defer wg.Done() rhl.Println("starting watch-dir-ctrl") - StartWatchDir(watchDir.Get().(string), conf, db.GetInterface()) + StartWatchDir(watchDir.Get().(string), conf, sessions.GetInterface()) rhl.Println("watch-dir-ctrl finished") }() } diff --git a/src/rhimportd/routeWeb.go b/src/rhimportd/routeWeb.go index 0411239..28bb082 100644 --- a/src/rhimportd/routeWeb.go +++ b/src/rhimportd/routeWeb.go @@ -25,11 +25,12 @@ package main import ( - "code.helsinki.at/rhrd-go/rddb" - "code.helsinki.at/rhrd-go/rhimport" "net/http" _ "net/http/pprof" "time" + + "code.helsinki.at/rhrd-go/rddb" + "code.helsinki.at/rhrd-go/rhimport" ) type webHandler struct { diff --git a/test/simple-watch.json b/test/simple-watch.json new file mode 100644 index 0000000..25ff8fd --- /dev/null +++ b/test/simple-watch.json @@ -0,0 +1,6 @@ +{ + "LOGIN_NAME": "heslinki", + "SHOW_ID": 10002, + "SOURCE_URI": "http://www.tonycuffe.com/mp3/tail%20toddle.mp3", + "SOURCE_FILE_POLICY": "keep" +} -- cgit v0.10.2