Relay.Task

View as Markdown

Relay.Task

A Relay.Task is simple way to send jobs to your Relay.Consumers from a short lived process, like a web framework. RELAY Tasks allow you to pass commands down to your Consumers without blocking your short lived request. Think of a RELAY Task as a way to queue a job for your background workers to processes asynchronously.

Methods-submenu

Deliver

Send a job to your Consumer in a specific context.

Parameters

ParameterTypeRequiredDescription
ContextstringrequiredContext where to send the Task.
MessagearrayrequiredArray with your custom data that will be sent to your Consumer’s onTask handler.

Returns

boolean - Whether the Task has been sent successfully.

Examples

Deliver a task to your Consumer with a message to then make an outbound Call.

1type PassToTask struct {
2 Foo uint `json:"foo"`
3 Bar string `json:"bar"`
4}
5
6func main() {
7[...]
8consumer.OnTask = func(_ *signalwire.Consumer, ev signalwire.ParamsEventTaskingTask) {
9 signalwire.Log.Info("Task Delivered. %v\n", ev)
10
11 go func() {
12 done <- struct{}{}
13 }()
14}
15
16taskMsg := PassToTask{
17 Foo: 123,
18 Bar: "baz",
19 }
20
21byteArray, err := json.MarshalIndent(taskMsg, "", " ")
22if err != nil {
23 signalwire.Log.Error("%v", err)
24 return
25}
26if result := consumer.Client.Tasking.Deliver(DefaultContext, byteArray); !result {
27 signalwire.Log.Error("Could not deliver task\n")
28 go func() {
29 done <- struct{}{}
30 }()
31 }
32}
33// stop when task has been delivered or on error
34<-done
35}