Skip to content

stellaraf/go-task-queue

Repository files navigation



taskqueue


GitHub tag (latest SemVer) GitHub Workflow Status GoDoc Reference

Usage

Basic Task Queue

BasicTaskQueue is a simple task queue that stores tasks as strings.

queue, err := taskqueue.NewBasic("queue-name")
queue.Add("example1", "example2")
value := queue.Pop()
// example1
value = queue.Pop()
// example2

JSON Task Queue

JSONTaskQueue provides a very similar API to BasicTaskQueue and has the ability to store virtually any objet type as a task.

type Data struct {
  System string
  Count int
}

type Task struct {
  ID string `json:"id"`
  Details []string `json:"details"`
  Timestamp time.Time `json:"timestamp"`
  Data *Data `json:"data"`
}

task := Task{
  ID: "1234",
  Details: []string{"some", "details"},
  Timestamp: time.Now(),
  Data: &Data{System: "aws", Count: 5},
}

queue, err := taskqueue.NewJSON("queue-name")
queue.Add(task)
var fromQueue *Task
queue.Pop(&fromQueue)

Options

Both BasicTaskQueue and JSONTaskQueue support the same options:

taskqueue.NewBasic(
  "queue-name",
  // Use a Redis connection string instead of WithHost/WithUsername/WithPassword.
  taskqueue.WithURI("redis://redis-username:redispassword@your-redis-host.example.com:55032"),
  // Set the Redis hostname. By default, this is localhost.
  taskqueue.WithHost("your-redis-host.example.com"),
  // Set the Redis username.
  taskqueue.WithUsername("redis-username"),
  // Set the Redis password.
  taskqueue.WithPassword("redis-password"),
  // Use your own context object. Useful if operating from within a web request.
  taskqueue.WithContext(context.Background()),
  // Set a Redis read/write timeout.
  taskqueue.WithTimeout(time.Seconds*10),
  // Add your own TLS configuration.
  taskqueue.WithTLSConfig(&tls.Config{InsecureSkipVerify: true}),
  // Don't re-add tasks to the queue that fail when unmarshaled.
  taskqueue.WithNoRetry(),
)