cmd/wasmcloudd: synchronous execution

This commit is contained in:
Cadey Ratio 2019-12-09 23:01:42 +00:00
parent 59d7356142
commit 04975b6a5e
3 changed files with 101 additions and 30 deletions

View File

@ -8,7 +8,6 @@ import (
"time" "time"
"github.com/go-interpreter/wagon/wasm" "github.com/go-interpreter/wagon/wasm"
"github.com/google/uuid"
shell "github.com/ipfs/go-ipfs-api" shell "github.com/ipfs/go-ipfs-api"
"github.com/rogpeppe/go-internal/txtar" "github.com/rogpeppe/go-internal/txtar"
"tulpa.dev/within/wasmcloud/cmd/internal" "tulpa.dev/within/wasmcloud/cmd/internal"
@ -143,7 +142,7 @@ func getLogs(w http.ResponseWriter, r *http.Request, u *User) {
json.NewEncoder(w).Encode(result) json.NewEncoder(w).Encode(result)
} }
func invokeHandler(w http.ResponseWriter, r *http.Request, u *User) { func invokeHandlerSync(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx := r.Context()
q := r.URL.Query() q := r.URL.Query()
name := q.Get("name") name := q.Get("name")
@ -166,48 +165,69 @@ func invokeHandler(w http.ResponseWriter, r *http.Request, u *User) {
http.NotFound(w, r) http.NotFound(w, r)
} }
execID := uuid.New().String() resp, err := runHandler(ctx, hdlr, 5*time.Minute, data)
if err != nil {
er := internal.ExecRequest{ ln.Error(ctx, err)
WASMCID: hdlr.Path, return
Name: hdlr.Name,
Data: data,
Env: map[string]string{
"RUN_ID": execID,
},
UUID: execID,
} }
encData, err := json.Marshal(er) logData := txtar.Format(&resp.Logs)
entry := ExecutionLog{
HandlerID: hdlr.ID,
RunID: resp.UUID,
Data: logData,
}
err = db.Save(&entry).Error
if err != nil {
ln.Error(ctx, err)
return
}
ln.Log(ctx, ln.Action("saving-logs"))
for _, file := range resp.Logs.Files {
if file.Name == "stdout.txt" {
w.Write(file.Data)
return
}
}
}
func invokeHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
q := r.URL.Query()
name := q.Get("name")
if name == "" {
http.NotFound(w, r)
return
}
var hdlr Handler
err := db.Where("name = ?", name).First(&hdlr).Error
if err != nil {
ln.Error(ctx, err)
http.NotFound(w, r)
return
}
data, err := ioutil.ReadAll(http.MaxBytesReader(w, r.Body, 1*1024*1024))
if err != nil { if err != nil {
ln.Error(ctx, err) ln.Error(ctx, err)
http.NotFound(w, r) http.NotFound(w, r)
} }
go func() { go func() {
f := ln.F{ resp, err := runHandler(ctx, hdlr, 5*time.Minute, data)
"name": name,
"run_id": execID,
}
ln.Log(ctx, ln.Action("invoke-handler"), f)
msg, err := nc.Request(internal.TopicName, encData, 5*time.Minute)
if err != nil {
ln.Error(ctx, err)
return
}
var resp internal.ExecResponse
err = json.Unmarshal(msg.Data, &resp)
if err != nil { if err != nil {
ln.Error(ctx, err) ln.Error(ctx, err)
return return
} }
data := txtar.Format(&resp.Logs) data := txtar.Format(&resp.Logs)
entry := ExecutionLog{ entry := ExecutionLog{
HandlerID: hdlr.ID, HandlerID: hdlr.ID,
RunID: execID, RunID: resp.UUID,
Data: data, Data: data,
} }
@ -217,6 +237,6 @@ func invokeHandler(w http.ResponseWriter, r *http.Request, u *User) {
return return
} }
ln.Log(ctx, ln.Action("saving-logs"), f) ln.Log(ctx, ln.Action("saving-logs"))
}() }()
} }

View File

@ -0,0 +1,50 @@
package main
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/google/uuid"
"tulpa.dev/within/wasmcloud/cmd/internal"
"within.website/ln"
"within.website/ln/opname"
)
func runHandler(ctx context.Context, hdlr Handler, timeout time.Duration, message []byte) (*internal.ExecResponse, error) {
ctx = opname.With(ctx, "invoke")
execID := uuid.New().String()
er := internal.ExecRequest{
WASMCID: hdlr.Path,
Name: hdlr.Name,
Data: message,
Env: map[string]string{
"RUN_ID": execID,
},
UUID: execID,
}
encData, err := json.Marshal(er)
if err != nil {
return nil, fmt.Errorf("can't encode job: %w", err)
}
f := ln.F{
"name": hdlr.Name,
"run_id": execID,
}
ln.Log(ctx, ln.Info("starting run"), f)
msg, err := nc.Request(internal.TopicName, encData, timeout)
if err != nil {
return nil, fmt.Errorf("can't submit job: %w", err)
}
var resp internal.ExecResponse
err = json.Unmarshal(msg.Data, &resp)
if err != nil {
return nil, fmt.Errorf("can't unmarshal response: %w", err)
}
return &resp, nil
}

View File

@ -62,7 +62,8 @@ func main() {
rtr.HandleFunc("/api/whoami", makeHandler(true, apiWhoami)) rtr.HandleFunc("/api/whoami", makeHandler(true, apiWhoami))
// invocation // invocation
rtr.HandleFunc("/invoke", makeHandler(false, invokeHandler)).Methods(http.MethodPost) rtr.HandleFunc("/invoke", invokeHandler).Methods(http.MethodPost)
rtr.HandleFunc("/invoke/sync", invokeHandlerSync).Methods(http.MethodPost)
rtr.PathPrefix("/static/").Handler(http.FileServer(http.Dir("."))) rtr.PathPrefix("/static/").Handler(http.FileServer(http.Dir(".")))