summaryrefslogtreecommitdiff
path: root/spooler.go
diff options
context:
space:
mode:
authorKatolaZ <katolaz@freaknet.org>2017-07-15 01:26:39 +0100
committerKatolaZ <katolaz@freaknet.org>2017-07-15 01:26:39 +0100
commit726b399e4747032a3d052339cd62c57ae5b6767c (patch)
treeaac8350e83bb13e9628dd287ca3aae97ecafcb32 /spooler.go
parented637037b75cb5dfe1b49e776956fa6ab3632b68 (diff)
pipeline from spool to worker is done -- added examples
Diffstat (limited to 'spooler.go')
-rw-r--r--spooler.go42
1 files changed, 30 insertions, 12 deletions
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
-
}