queue

package module
v0.0.0-...-9741868 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2024 License: MIT Imports: 5 Imported by: 0

README

RedQueue

RedQueue is a simple Redis and KeyDB-backed queue implementation in Go. It provides an easy way to manage jobs using them as a persistent storage system.

How to Install

Firstly, you need to make sure that your environment have Go installed. Afterwards, you can download and install redqueue with the command:

go get -u github.com/chand1012/redqueue

Usage

Let's take a look how to use the RedQueue in your Go application.

package main

import (
	queue "github.com/chand1012/redqueue"
	"github.com/redis/go-redis/v9"
)

func main() {
	opts := &redis.Options{
		Addr:     "localhost:6379",
		DB:       0, // use default DB
		Password: "", // no password set
	}

	// Create a new queue named "queue_test"
	q := queue.New(opts, "queue_test")

	type testData struct {
		Id   int    `json:"id"`
		Name string `json:"name"`
	}

	data := testData{Id: 1, Name: "test"}

	// Add data to the queue
	err := q.Push(data)
	if err != nil {
		panic(err)
	}

	// add more test data
	data = testData{Id: 2, Name: "test2"}
	err = q.Push(data)
	if err != nil {
		panic(err)
	}

	// Pulls data from the queue as bytes
	fetched, err := q.Process()
	if err != nil {
		panic(err)
	}

	// Unmarshal the pulled data into the a given struct
	var processed testData
	err = q.ProcessInto(fetched, &processed)
	if err != nil {
		panic(err)
	}

	// Mark the task as completed
	err = q.Finish()
	if err != nil {
		panic(err)
	}

	// Close the queue
	q.Close()
}

Please note that the address, database, and password for Redis server are passed to the queue with redis.Options .

Tests

There are some comprehensive unit tests provided in this queue library. Make sure you have a local Redis or KeyDB server running, then you can run them with command go test .

Contributing

We welcome contributors, please feel free to enhance this library.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Queue

type Queue struct {
	// contains filtered or unexported fields
}

func FromClient

func FromClient(rdb *redis.Client, name string) *Queue

creates a new queue from a redis client and a name

func New

func New(opts *redis.Options, name string) *Queue

New creates a new queue with the given name and redis options

func (*Queue) Close

func (q *Queue) Close() error

close the queue and the redis client

func (*Queue) Finish

func (q *Queue) Finish() error

removes a task from the processing queue

func (*Queue) Process

func (q *Queue) Process() ([]byte, error)

pop a task from the queue, marking it as processing

func (*Queue) ProcessInto

func (q *Queue) ProcessInto(v any) error

same as process, but takes in a pointer to a struct or map, and unmarshals the data into it

func (*Queue) Push

func (q *Queue) Push(data any) error

takes a string, bytes, a map, or a struct. Will marshal struct or map to JSON. Errors if marshaling fails.

Jump to

Keyboard shortcuts

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