Skip to main content

Relay.Task

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.

type PassToTask struct {
Foo uint `json:"foo"`
Bar string `json:"bar"`
}

func main() {
[...]
consumer.OnTask = func(_ *signalwire.Consumer, ev signalwire.ParamsEventTaskingTask) {
signalwire.Log.Info("Task Delivered. %v\n", ev)

go func() {
done <- struct{}{}
}()
}

taskMsg := PassToTask{
Foo: 123,
Bar: "baz",
}

byteArray, err := json.MarshalIndent(taskMsg, "", " ")
if err != nil {
signalwire.Log.Error("%v", err)
return
}
if result := consumer.Client.Tasking.Deliver(DefaultContext, byteArray); !result {
signalwire.Log.Error("Could not deliver task\n")
go func() {
done <- struct{}{}
}()
}
}
// stop when task has been delivered or on error
<-done
}