Skip to content

Commit

Permalink
added support to call functions of other engines
Browse files Browse the repository at this point in the history
  • Loading branch information
YourTechBud committed Nov 17, 2018
1 parent 050b3ac commit 1e6f9ca
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ myEngine.RegisterFunc("my-func", myFunc)
// Start engine
myEngine.Start()

// Call function of some other engine
res, err := myEngine.Call("some-engine", "some-func", engine.M{"msg": "space-engine-go is awesome!"}, 1000)
log.println("Res", res, "Err", err)

```
4 changes: 4 additions & 0 deletions engine/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ myEngine.RegisterFunc("my-func", myFunc)
// Start engine
myEngine.Start()

// Call function of some other engine
res, err := myEngine.Call("some-engine", "some-func", engine.M{"msg": "space-engine-go is awesome!"}, 1000)
log.println("Res", res, "Err", err)

```
27 changes: 27 additions & 0 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package engine
import (
"encoding/json"
"log"
"time"

nats "github.com/nats-io/go-nats"
)
Expand Down Expand Up @@ -120,3 +121,29 @@ func (engine *Engine) Start() {
}
}
}

// Call -- calls a function of any engine
func (engine *Engine) Call(engineName, functionName string, params map[string]interface{}, timeOut int) (*M, error) {
subj := "faas:" + engineName + ":" + functionName

// Convert params into json
dataBytes, err := json.Marshal(&params)
if err != nil {
return nil, err
}

// Make a nats request
msg, err := engine.natsClient.Request(subj, dataBytes, time.Duration(timeOut)*time.Millisecond)
if err != nil {
return nil, err
}

// Parse msg
res := M{}
err = json.Unmarshal(msg.Data, &res)
if err != nil {
return nil, err
}

return &res, nil
}

0 comments on commit 1e6f9ca

Please sign in to comment.