gue
Gue is Golang queue on top of PostgreSQL that uses transaction-level locks.
Originally this project used to be a fork of bgentry/que-go
but because of some backward-compatibility breaking changes and original library author not being very responsive for
PRs I turned fork into standalone project. Version 2 breaks internal backward-compatibility with the original project -
DB table and all the internal logic (queries, algorithms) is completely rewritten.
The name Gue is yet another silly word transformation: Queue -> Que, Go + Que -> Gue.
Install
go get -u github.com/vgarvardt/gue/v3
Additionally, you need to apply DB migration.
Usage Example
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"time"
"github.com/jackc/pgx/v4/pgxpool"
"golang.org/x/sync/errgroup"
"github.com/vgarvardt/gue/v3"
"github.com/vgarvardt/gue/v3/adapter/pgxv4"
)
const (
printerQueue = "name_printer"
jobTypePrinter = "PrintName"
)
type printNameArgs struct {
Name string
}
func main() {
printName := func(ctx context.Context, j *gue.Job) error {
var args printNameArgs
if err := json.Unmarshal(j.Args, &args); err != nil {
return err
}
fmt.Printf("Hello %s!\n", args.Name)
return nil
}
pgxCfg, err := pgxpool.ParseConfig(os.Getenv("DATABASE_URL"))
if err != nil {
log.Fatal(err)
}
pgxPool, err := pgxpool.ConnectConfig(context.Background(), pgxCfg)
if err != nil {
log.Fatal(err)
}
defer pgxPool.Close()
poolAdapter := pgxv4.NewConnPool(pgxPool)
gc := gue.NewClient(poolAdapter)
wm := gue.WorkMap{
jobTypePrinter: printName,
}
finishedJobsLog := func(ctx context.Context, j *gue.Job, err error) {
if err != nil {
return
}
j.Tx().Exec(
ctx,
"INSERT INTO finished_jobs_log (queue, type, run_at) VALUES ($1, $2, now())",
j.Queue,
j.Type,
)
}
workers := gue.NewWorkerPool(gc, wm, 2, gue.WithPoolQueue(printerQueue), gue.WithPoolHooksJobDone(finishedJobsLog))
ctx, shutdown := context.WithCancel(context.Background())
g, gctx := errgroup.WithContext(ctx)
g.Go(func() error {
err := workers.Run(gctx)
if err != nil {
log.Fatal(err)
}
return err
})
args, err := json.Marshal(printNameArgs{Name: "vgarvardt"})
if err != nil {
log.Fatal(err)
}
j := &gue.Job{
Type: jobTypePrinter,
Queue: printerQueue,
Args: args,
}
if err := gc.Enqueue(context.Background(), j); err != nil {
log.Fatal(err)
}
j = &gue.Job{
Type: jobTypePrinter,
Queue: printerQueue,
RunAt: time.Now().UTC().Add(30 * time.Second),
Args: args,
}
if err := gc.Enqueue(context.Background(), j); err != nil {
log.Fatal(err)
}
time.Sleep(30 * time.Second)
shutdown()
if err := g.Wait(); err != nil {
log.Fatal(err)
}
}
PostgreSQL drivers
Package supports several PostgreSQL drivers using adapter interface internally. Currently, adapters for the following
drivers have been implemented:
pgx/v4
package main
import (
"context"
"log"
"os"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/vgarvardt/gue/v3"
"github.com/vgarvardt/gue/v3/adapter/pgxv4"
)
func main() {
pgxCfg, err := pgxpool.ParseConfig(os.Getenv("DATABASE_URL"))
if err != nil {
log.Fatal(err)
}
pgxPool, err := pgxpool.ConnectConfig(context.Background(), pgxCfg)
if err != nil {
log.Fatal(err)
}
defer pgxPool.Close()
poolAdapter := pgxv4.NewConnPool(pgxPool)
gc := gue.NewClient(poolAdapter)
...
}
pgx/v3
package main
import (
"log"
"os"
"github.com/jackc/pgx"
"github.com/vgarvardt/gue/v3"
"github.com/vgarvardt/gue/v3/adapter/pgxv3"
)
func main() {
pgxCfg, err := pgx.ParseURI(os.Getenv("DATABASE_URL"))
if err != nil {
log.Fatal(err)
}
pgxPool, err := pgx.NewConnPool(pgx.ConnPoolConfig{
ConnConfig: pgxCfg,
})
if err != nil {
log.Fatal(err)
}
defer pgxPool.Close()
poolAdapter := pgxv3.NewConnPool(pgxPool)
gc := gue.NewClient(poolAdapter)
...
}
lib/pq
package main
import (
"database/sql"
"log"
"os"
_ "github.com/lib/pq"
"github.com/vgarvardt/gue/v3"
"github.com/vgarvardt/gue/v3/adapter/libpq"
)
func main() {
db, err := sql.Open("postgres", os.Getenv("DATABASE_URL"))
if err != nil {
log.Fatal(err)
}
defer db.Close()
poolAdapter := libpq.NewConnPool(db)
gc := gue.NewClient(poolAdapter)
...
}
pg/v10
package main
import (
"log"
"os"
"github.com/go-pg/pg/v10"
"github.com/vgarvardt/gue/v3"
"github.com/vgarvardt/gue/v3/adapter/gopgv10"
)
func main() {
opts, err := pg.ParseURL(os.Getenv("DATABASE_URL"))
if err != nil {
log.Fatal(err)
}
db := pg.Connect(opts)
defer db.Close()
poolAdapter := gopgv10.NewConnPool(db)
gc := gue.NewClient(poolAdapter)
...
}
Logging
Package supports several logging libraries using adapter interface internally. Currently, adapters for the following
drivers have been implemented:
- NoOp (
adapter.NoOpLogger
) - default adapter that does nothing, so it is basically /dev/null
logger - Stdlib
log
- adapter that uses log
logger for logs output. Instantiate it
with adapter.NewStdLogger(...)
. - Uber
zap
- adapter that uses go.uber.org/zap
logger for logs output.
Instantiate it with adapter.zap.New(...)
.
Testing
Linter and tests are running for every Pull Request, but it is possible to run linter and tests locally using docker
and make
.
Run linter: make link
. This command runs liner in docker container with the project source code mounted.
Run tests: make test
. This command runs project dependencies in docker containers if they are not started yet and runs
go tests with coverage.