diff --git a/pkg/daemon/ropman.go b/pkg/daemon/ropman.go index 3782b6b..a84eb59 100644 --- a/pkg/daemon/ropman.go +++ b/pkg/daemon/ropman.go @@ -7,6 +7,7 @@ import ( "math/rand" "sort" "strconv" + "time" "github.com/Bitspark/slang/pkg/api" "github.com/Bitspark/slang/pkg/core" @@ -34,13 +35,15 @@ func (rop *runningOperator) Push(data interface{}) { rop.incoming <- data } -func (rop *runningOperator) Pull() interface{} { +func (rop *runningOperator) Pull() (interface{}, bool) { for { select { case odat := <-rop.outgoing: - return odat + return odat, true + case <-time.After(500 * time.Millisecond): + return nil, false case <-rop.outStop: - return nil + return nil, false } } } diff --git a/pkg/daemon/runner_service.go b/pkg/daemon/runner_service.go index ad2c7c7..300cc93 100644 --- a/pkg/daemon/runner_service.go +++ b/pkg/daemon/runner_service.go @@ -183,9 +183,8 @@ var RunnerService = &Service{map[string]*Endpoint{ return } - r.ParseForm() - - if r.Method == "GET" || r.Method == "POST" { + if r.Method == "GET" { + r.ParseForm() props, err := parseProperties(r.Form, blueprint.PropertyDefs) if err != nil { @@ -204,9 +203,7 @@ var RunnerService = &Service{map[string]*Endpoint{ } rop.Push(nil) - out := rop.Pull() - - if out != nil { + if out, ok := rop.Pull(); ok { fmt.Println("\t<--", out) response(w, http.StatusOK, &out) } else { @@ -214,6 +211,42 @@ var RunnerService = &Service{map[string]*Endpoint{ //w.WriteHeader(http.StatusNoContent) } return + } else if r.Method == "POST" { + + type Request struct { + Properties core.Properties `json:"properties"` + Generics core.Generics `json:"generics"` + Input any `json:"input"` + } + + var req Request; + + decoder := json.NewDecoder(r.Body) + err := decoder.Decode(&req) + + if err != nil { + responseError(w, http.StatusBadRequest, err, "E05") + return + } + + rop := romanager.GetByProperties(req.Properties) + if rop == nil { + st := GetStorage(r) + rop, err = romanager.Exec(blueprint.Id, req.Generics, req.Properties, st) + if err != nil { + responseError(w, http.StatusBadRequest, err, "E04") + return + } + } + + rop.Push(req.Input) + if out, ok := rop.Pull(); ok{ + fmt.Println("\t<--", out) + response(w, http.StatusOK, &out) + } else { + response(w, http.StatusNoContent, nil) + } + return } }},