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
Parameter | Type | Required | Description |
---|---|---|---|
Context | string | required | Context where to send the Task. |
Message | array | required | Array 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
}