From 726b399e4747032a3d052339cd62c57ae5b6767c Mon Sep 17 00:00:00 2001 From: KatolaZ Date: Sat, 15 Jul 2017 01:26:39 +0100 Subject: pipeline from spool to worker is done -- added examples --- spooler.go | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) (limited to 'spooler.go') diff --git a/spooler.go b/spooler.go index e11b980..9b4fc1c 100644 --- a/spooler.go +++ b/spooler.go @@ -1,40 +1,59 @@ package main import ( + "fmt" "github.com/fsnotify/fsnotify" + "github.com/go-yaml/yaml" + "io/ioutil" "log" - "os" - "fmt" +// "time" ) // parse a request file and return a SCORSHmessage -func parse_request(fname string) (SCORSHmsg, error) { +func parse_request(fname string, msg *SCORSHmsg) error { - var ret SCORSHmsg - _, err := os.Open(fname) + + debug.log("[parse_request] message at start: %s\n", msg) + + data, err := ioutil.ReadFile(fname) if err != nil { log.Printf("Unable to open file: %s\n", fname) - return ret, SCORSHerr(SCORSH_ERR_NO_FILE) + return SCORSHerr(SCORSH_ERR_NO_FILE) } - // FIXME: Fill in the ret structure + debug.log("[parse_request] file contains: \n%s\n", data) + + debug.log("[parse_request] reading message from file: %s\n", fname) - return ret, nil + err = yaml.Unmarshal([]byte(data), msg) + if err != nil { + return fmt.Errorf("Error parsing message: %s", err) + } + debug.log("[parse_request] got message: %s\n", msg) + return nil } func spooler(watcher *fsnotify.Watcher, worker chan SCORSHmsg) { + + log.Println("Spooler started correctly") + + var msg *SCORSHmsg + msg = new(SCORSHmsg) for { select { case event := <-watcher.Events: - if event.Op == fsnotify.Create { - msg, err := parse_request(event.Name) + if event.Op == fsnotify.Write { + //time.Sleep(1000 * time.Millisecond) + debug.log("[spooler] new file %s detected\n", event.Name) + err := parse_request(event.Name, msg) if err != nil { log.Printf("Invalid packet received. [%s]\n", err) } - worker <- msg + debug.log("[spooler] read message: %s\n", msg) + worker <- *msg } case err := <-watcher.Errors: log.Println("error:", err) @@ -59,5 +78,4 @@ func StartSpooler(master *SCORSHmaster) error { go spooler(watcher, master.Spooler) return nil - } -- cgit v1.2.3