Go - Queue.Send()

Send tasks to a queue.

import (
  "context"
  "fmt"

  "github.com/nitrictech/go-sdk/api/queues"
  "github.com/nitrictech/go-sdk/nitric"
)

func main() {
  queue, err := nitric.NewQueue("queue-name").With(nitric.QueueSending)
  if err != nil {
    return
  }

  _, err := queue.Send(context.TODO(), []*queues.Task{
    {
      ID: "1234",
      PayloadType: "message",
      Payload: map[string]interface{}{
        "message": "hello world",
      },
    },
  })
  if err != nil {
    return
  }

  if err := nitric.Run(); err != nil {
    fmt.Println(err)
  }
}

Parameters

  • Name
    ctx
    Required
    Required
    Type
    context
    Description

    The context of the call, used for tracing.

  • Name
    tasks
    Required
    Required
    Type
    []*Task
    Description

    An array of tasks to send to the queue.

Examples

Send multiple tasks to a queue

import (
  "context"
  "fmt"

  "github.com/nitrictech/go-sdk/api/queues"
  "github.com/nitrictech/go-sdk/nitric"
)

func main() {
  queue, err := nitric.NewQueue("blah").With(nitric.QueueSending)
  if err != nil {
    return
  }

  _, err := queue.Send(context.TODO(), []*queues.Task{
    {
      ID: "1234",
      PayloadType: "message",
      Payload: map[string]interface{}{
        "message": "hello world",
      },
    },
  })
  if err != nil {
    return
  }

  if err := nitric.Run(); err != nil {
    fmt.Println(err)
  }
}

Dealing with failures

In rare cases when sending tasks to a queue some tasks might fail to be sent. The response from Send() will include an array of any tasks that failed to send. You can process this array to retry or log the error.

failedTasks, err := queue.Send(ctx, tasks)
if err != nil {
  fmt.Println(err)
  return
}

for _, ft := range failedTasks {
  fmt.Println(ft.Reason)
}