Code Monkey home page Code Monkey logo

goqueuelite's Introduction

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.

goqueuelite's People

Contributors

risico avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.