taskqueue

package module
v0.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 31, 2023 License: BSD-3-Clause-Clear Imports: 9 Imported by: 0

README



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:[email protected]: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(),
)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BasicTaskQueue

type BasicTaskQueue struct {
	Name  string
	Redis redis.UniversalClient
	// contains filtered or unexported fields
}

BasicTaskQueue implements a FIFO task queue of string values.

func NewBasic

func NewBasic(name string, option ...Option) (*BasicTaskQueue, error)

NewBasic creates a new BasicTaskQueue instance.

func (*BasicTaskQueue) Add

func (q *BasicTaskQueue) Add(tasks ...any) error

Add adds any number of tasks to the queue in order. Items provided will be marshaled to JSON.

func (*BasicTaskQueue) Clear

func (q *BasicTaskQueue) Clear() error

Clear removes all tasks from the queue.

func (*BasicTaskQueue) Get added in v0.0.5

func (q *BasicTaskQueue) Get(index int64) (string, error)

Get retrieves an item from the queue based on its index.

func (*BasicTaskQueue) Has added in v0.0.6

func (q *BasicTaskQueue) Has(value string) bool

Has determines if a queue has an given task.

func (*BasicTaskQueue) Pop

func (q *BasicTaskQueue) Pop() *string

Pop removes and returns the first task from the queue. If the queue is empty, the return value will be nil.

func (*BasicTaskQueue) Remove

func (q *BasicTaskQueue) Remove(task any) error

Remove removes a task from the queue.

func (*BasicTaskQueue) RemoveIndex added in v0.0.5

func (q *BasicTaskQueue) RemoveIndex(index int64) error

func (*BasicTaskQueue) Size

func (q *BasicTaskQueue) Size() uint64

Size returns the number of items in the queue.

type JSONTaskQueue

type JSONTaskQueue struct {
	// Name represents the Redis key.
	Name string
	// Redis is the underlying Redis instance.
	Redis redis.UniversalClient
	// contains filtered or unexported fields
}

JSONTaskQueue implements a FIFO task queue with any JSON-able value as the value.

func NewJSON

func NewJSON(name string, option ...Option) (*JSONTaskQueue, error)

NewJSON creates a new JSONTaskQueue instance.

func (*JSONTaskQueue) Add

func (q *JSONTaskQueue) Add(tasks ...any) error

Add adds any number of tasks to the queue in order. Items provided will be marshaled to JSON.

func (*JSONTaskQueue) Clear

func (q *JSONTaskQueue) Clear() error

Clear removes all tasks from the queue.

func (*JSONTaskQueue) Get added in v0.0.5

func (q *JSONTaskQueue) Get(index int64, target any) error

func (*JSONTaskQueue) Has added in v0.0.6

func (q *JSONTaskQueue) Has(value any) bool

Has determines if a queue has an given task.

func (*JSONTaskQueue) Pop

func (q *JSONTaskQueue) Pop(value any) error

Pop removes the first task from the queue and unmarshals the value.

func (*JSONTaskQueue) PopBytes added in v0.0.4

func (q *JSONTaskQueue) PopBytes() ([]byte, error)

func (*JSONTaskQueue) Remove

func (q *JSONTaskQueue) Remove(task any) error

Remove removes a task from the queue.

func (*JSONTaskQueue) RemoveIndex added in v0.0.5

func (q *JSONTaskQueue) RemoveIndex(index int64) error

func (*JSONTaskQueue) Size

func (q *JSONTaskQueue) Size() uint64

Size returns the number of items in the queue.

type Option

type Option func(*Options)

func WithContext

func WithContext(ctx context.Context) Option

WithContext sets the Redis context object.

func WithHost

func WithHost(host string) Option

WithHost sets the Redis connection host.

func WithNoRetry added in v0.0.4

func WithNoRetry() Option

WithNoRetry enables or disables task retry, which re-adds the task back to the queue if an error occurs while retrieving it.

func WithPassword

func WithPassword(password string) Option

WithPassword sets the password to use when authenticating to Redis.

func WithTLSConfig

func WithTLSConfig(tlsConfig *tls.Config) Option

WithTLSConfig sets the TLS configuration of the Redis instance.

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout sets the Redis read/write timeout.

func WithURI

func WithURI(uri string) Option

WithURI sets the Redis URI connection string.

func WithUsername

func WithUsername(username string) Option

WithUsername sets the username to use when authenticating to Redis.

type Options

type Options struct {
	Host      string
	Username  string
	Password  string
	TLSConfig *tls.Config
	URI       string
	Context   context.Context
	Timeout   time.Duration
	NoRetry   bool
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL