wasmcloud/cmd/wasmcloudd/handler_invoke.go

51 lines
1.0 KiB
Go
Raw Normal View History

2019-12-09 23:01:42 +00:00
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
}