goqite
by Phil Tadros
March 11, 2024
2024-03-11 05:39:36
goqite (pronounced Go-queue-ite) is a persistent message queue Go library constructed on SQLite and impressed by AWS SQS (however a lot less complicated).
$ go get github.com/maragudk/goqite
Instance
bundle predominant
import (
"context"
"database/sql"
"fmt"
"log"
"time"
_ "github.com/mattn/go-sqlite3"
"github.com/maragudk/goqite"
)
const schema = `
create desk goqite (
id textual content major key default ('m_' || decrease(hex(randomblob(16)))),
created textual content not null default (strftime('%Y-%m-%dTpercentH:%M:%fZ')),
up to date textual content not null default (strftime('%Y-%m-%dTpercentH:%M:%fZ')),
queue textual content not null,
physique blob not null,
timeout textual content not null default (strftime('%Y-%m-%dTpercentH:%M:%fZ')),
obtained integer not null default 0
) strict;
create set off goqite_updated_timestamp after replace on goqite start
replace goqite set up to date = strftime('%Y-%m-%dTpercentH:%M:%fZ') the place id = previous.id;
finish;
create index goqite_queue_created_idx on goqite (queue, created);
`
func predominant() {
db, err := sql.Open("sqlite3", ":reminiscence:?_journal=WAL&_timeout=5000&_fk=true")
if err != nil {
log.Fatalln(err)
}
if _, err := db.Exec(schema); err != nil {
log.Fatalln(err)
}
// Create a brand new queue named "jobs".
// It's also possible to customise the message redelivery timeout and most obtain rely, however right here, we use the defaults.
q := goqite.New(goqite.NewOpts{
DB: db,
Title: "jobs",
})
// Ship a message to the queue.
// Notice that the physique is an arbitrary byte slice, so you'll be able to resolve what sort of payload you have got.
// It's also possible to set a message delay.
err = q.Ship(context.Background(), goqite.Message{
Physique: []byte("yo"),
})
if err != nil {
log.Fatalln(err)
}
// Obtain a message from the queue, throughout which era it isn't obtainable to different customers
// (till the message timeout has handed).
m, err := q.Obtain(context.Background())
if err != nil {
log.Fatalln(err)
}
fmt.Println(string(m.Physique))
// If you happen to want extra time for processing the message, you'll be able to prolong the message timeout as many occasions as you need.
if err := q.Lengthen(context.Background(), m.ID, time.Second); err != nil {
log.Fatalln(err)
}
// Ensure that to delete the message, so it does not get redelivered.
if err := q.Delete(context.Background(), m.ID); err != nil {
log.Fatalln(err)
}
}
What's Your Reaction?
Excited
0
Happy
0
In Love
0
Not Sure
0
Silly
0