Skip to content

Commit

Permalink
pgqueue: add example on how to use queue.Delete
Browse files Browse the repository at this point in the history
  • Loading branch information
ucirello committed Aug 4, 2024
1 parent 8139a4b commit 92e6c28
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions queue_examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,18 +157,50 @@ func Example_reservedReleased() {
if err := queue.Push(ctx, content); err != nil {
log.Fatalln("cannot push message to queue:", err)
}
r, err := queue.Reserve(ctx, 1*time.Minute)
msg, err := queue.Reserve(ctx, 1*time.Minute)
if err != nil {
log.Fatalln("cannot pop message from the queue:", err)
}
fmt.Printf("content: %s\n", r.Content)
if err := r.Release(ctx); err != nil {
fmt.Printf("content: %s\n", msg.Content)
if err := msg.Release(ctx); err != nil {
log.Fatalln("cannot release the message back to the queue:", err)
}
// Output:
// content: content
}

func Example_reservedReleasedDeleted() {
ctx := context.Background()
pool, err := pgxpool.New(ctx, dsn)
if err != nil {
log.Fatalln("cannot open database connection pool:", err)
}
client, err := pgqueue.Open(ctx, pool)
if err != nil {
log.Fatalln("cannot create queue handler:", err)
}
defer client.Close()
if err := client.CreateTable(ctx); err != nil {
log.Fatalln("cannot create queue table:", err)
}
queue := client.Queue("example-queue-release")
defer queue.Close()
content := []byte("content")
if err := queue.Push(ctx, content); err != nil {
log.Fatalln("cannot push message to queue:", err)
}
msg, err := queue.Reserve(ctx, 1*time.Minute)
if err != nil {
log.Fatalln("cannot pop message from the queue:", err)
}
fmt.Printf("content: %s\n", msg.Content)
if err := queue.Delete(ctx, msg.ID()); err != nil {
log.Fatalln("cannot remove the message from the queue:", err)
}
// Output:
// content: content
}

func Example_reservedTouch() {
ctx := context.Background()
pool, err := pgxpool.New(ctx, dsn)
Expand Down

0 comments on commit 92e6c28

Please sign in to comment.