summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@helsinki.at>2016-01-05 17:37:41 (GMT)
committerChristian Pointner <equinox@helsinki.at>2016-01-05 17:37:41 (GMT)
commitc2ca0aff971999821b23faa5db42e1ea0f81c2ca (patch)
tree8202cd1dc9353c74ea48149a37742b6c497f2d25
parent1f7c2b415a67049fa217ac1b7495e78267ec4808 (diff)
watch dir control interface works now
-rw-r--r--src/helsinki.at/rhimportd/ctrlWatchDir.go74
1 files changed, 51 insertions, 23 deletions
diff --git a/src/helsinki.at/rhimportd/ctrlWatchDir.go b/src/helsinki.at/rhimportd/ctrlWatchDir.go
index f4e3a54..6f84c15 100644
--- a/src/helsinki.at/rhimportd/ctrlWatchDir.go
+++ b/src/helsinki.at/rhimportd/ctrlWatchDir.go
@@ -30,6 +30,8 @@ import (
"helsinki.at/rhimport"
"net/http"
"os"
+ "path/filepath"
+ "strings"
"time"
)
@@ -73,21 +75,31 @@ type watchDirResponseData struct {
Cut uint `json:"CUT_NUMBER"`
}
-func watchDirErrorResponse(code int, errStr string) {
- encoder := json.NewEncoder(os.Stdout)
- respdata := watchDirResponseData{code, errStr, 0, 0}
- encoder.Encode(respdata)
+func watchDirWriteResponse(filename string, resp *watchDirResponseData) {
+ file, err := os.OpenFile(filename, os.O_WRONLY|os.O_TRUNC, 0)
+ if err != nil {
+ rhl.Printf("watch-dir-ctrl: writing response failed: %s", err)
+ return
+ }
+ encoder := json.NewEncoder(file)
+ encoder.Encode(resp)
+ file.Close()
+
+ dstname := strings.TrimSuffix(filename, ".running") + ".done"
+ os.Rename(filename, dstname)
}
-func watchDirResponse(result *rhimport.Result) {
- encoder := json.NewEncoder(os.Stdout)
- respdata := watchDirResponseData{result.ResponseCode, result.ErrorString, result.Cart, result.Cut}
- encoder.Encode(respdata)
+func watchDirErrorResponse(filename string, code int, errStr string) {
+ watchDirWriteResponse(filename, &watchDirResponseData{code, errStr, 0, 0})
}
-func watchDirParseRequest(conf *rhimport.Config, rddb *rhimport.RdDbChan, filename string) (ctx *rhimport.Context, err error) {
+func watchDirResponse(filename string, result *rhimport.Result) {
+ watchDirWriteResponse(filename, &watchDirResponseData{result.ResponseCode, result.ErrorString, result.Cart, result.Cut})
+}
+
+func watchDirParseRequest(conf *rhimport.Config, rddb *rhimport.RdDbChan, req *os.File) (ctx *rhimport.Context, err error) {
- decoder := json.NewDecoder(os.Stdin)
+ decoder := json.NewDecoder(req)
reqdata := newWatchDirRequestData(conf)
if jsonerr := decoder.Decode(reqdata); jsonerr != nil {
err = fmt.Errorf("Error parsing JSON response: %s", jsonerr)
@@ -111,33 +123,27 @@ func watchDirParseRequest(conf *rhimport.Config, rddb *rhimport.RdDbChan, filena
return
}
-func watchDirHandler(conf *rhimport.Config, rddb *rhimport.RdDbChan, filename string) {
+func watchDirHandler(conf *rhimport.Config, rddb *rhimport.RdDbChan, ctx *rhimport.Context, filename string) {
rhdl.Printf("WatchDirHandler: request for '%s'", filename)
- var ctx *rhimport.Context
var err error
- if ctx, err = watchDirParseRequest(conf, rddb, filename); err != nil {
- watchDirErrorResponse(http.StatusBadRequest, err.Error())
- return
- }
-
if err = ctx.SanityCheck(); err != nil {
- watchDirErrorResponse(http.StatusBadRequest, err.Error())
+ watchDirErrorResponse(filename, http.StatusBadRequest, err.Error())
return
}
var res *rhimport.Result
if res, err = rhimport.FetchFile(ctx); err != nil {
- watchDirErrorResponse(http.StatusInternalServerError, err.Error())
+ watchDirErrorResponse(filename, http.StatusInternalServerError, err.Error())
return
}
if res.ResponseCode != http.StatusOK {
- watchDirErrorResponse(res.ResponseCode, res.ErrorString)
+ watchDirErrorResponse(filename, res.ResponseCode, res.ErrorString)
return
}
if res, err = rhimport.ImportFile(ctx); err != nil {
- watchDirErrorResponse(http.StatusInternalServerError, err.Error())
+ watchDirErrorResponse(filename, http.StatusInternalServerError, err.Error())
return
}
if res.ResponseCode == http.StatusOK {
@@ -146,7 +152,7 @@ func watchDirHandler(conf *rhimport.Config, rddb *rhimport.RdDbChan, filename st
rhl.Println("ImportFile import of", ctx.SourceFile, "was unsuccesful")
}
- watchDirResponse(res)
+ watchDirResponse(filename, res)
return
}
@@ -164,7 +170,29 @@ func watchDirRun(dir *os.File, conf *rhimport.Config, rddb *rhimport.RdDbChan) {
rhl.Printf("watch-dir-ctrl: reading directory contents failed: %s", err)
return
}
- rhdl.Printf("watch-dir-ctrl: got: %q", names)
+
+ 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, rddb, file); err == nil {
+ file.Close()
+ dstname := strings.TrimSuffix(srcname, ".new") + ".running"
+ os.Rename(srcname, dstname)
+ go watchDirHandler(conf, rddb, 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)
+ }
+ }
+ }
+
time.Sleep(1 * time.Second)
}
}