Skip to content

Tiny little queue on top of sqlite written in Go

License

Notifications You must be signed in to change notification settings

risico/goqueuelite

Repository files navigation

GoQueueLite

goqueuelito

Tiny little queue on top of SQLite written in Go.

sQueueLite is a simplistic, SQLite backed job embedded queue library for Go applications. It provides an easy way to manage and process background jobs, facilitating the scheduling and processing of tasks in an organized and efficient manner.

Warning Is still in heavy development and the API is not finalized yet and might change any moment.

Features

Installation

go get github.com/risico/goqueuelite

Usage

package main

import "github.com/risico/goqueuelite"

func main() {
	params := goqueuelite.Params{
		DatabasePath: "queue.db",
		AutoVacuum:   true,
		AutoPrune:    true,
	}
	queue, err := goqueuelite.New(params)
	if err != nil {
		panic(err)
	}
	defer queue.Close()
}

Enqueuing a Job

data := "Your job data here"
params := goqueuelite.EnqueueParams{
    Namespace: "test_namespace",
    ScheduleAfter: time.Now().Add(1 * time.Hour),
    TTL: 2 * time.Hour,
}
id, err := queue.Enqueue(data, params)
if err != nil {}

Dequeueing a Job

params := goqueuelite.DequeueParams{
    Namespace: "default",
}
message, err := queue.Dequeue(params)
if err != nil {}
fmt.Printf("Message(%+v) \n", message)

Message Payload

type Message struct {
	ID          int64
	Data        any
	Namespace   string
	Status      JobStatus
	Delay       uint64
	LockTime    int
	DoneTime    int
	Retries     int
	ScheduledAt int
	TTL         int
}

Subscribe to namespaces

ch, err = queue.Subscribe("default")
if err != nil { }

for {
    select {
        case mEvent, ok := <-ch:
            if !ok {
                return
            }

            m, err := queue.Lock(mEvent.MessageID)
            // do something with m
    }
}

Marking a Job as Done or Failed

err = queue.Done(messageID)
if err != nil {
	// handle error
}

err = queue.Fail(messageID)
if err != nil {
	// handle error
}

Retrying a job

err = queue.Retry(messageID)
if err != nil {
	// handle error
}

Getting the size of the queue

size, err := queue.Size()

Checking if the queue is empty

isEmpty, err := queue.Empty()

Manual Pruning and Vacuuming

If you have disabled AutoPrune and AutoVacuum, you can manually prune and vacuum the database.

queue.Prune()
queue.Vacuum()

To CGO or to Not CGO

CGO is required and enabled by default as the package makes use of github.com/mattn/go-sqlite3 but if you require cross-compilation and don't want to bother with
CGO you can set the -tags nocgo (alongside CGO_ENABLED=0) and it will switch to using modernc.org/sqlite which does not require CGO.

go build . -tags nocgo

Contributing

Feel free to contribute to this project by opening issues or submitting pull requests for bug fixes or features.

License

This project is licensed under the MIT License.

About

Tiny little queue on top of sqlite written in Go

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages