-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexample.go
67 lines (54 loc) · 1.43 KB
/
example.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"context"
"log"
"time"
"github.com/takenet/deckard"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
func main() {
// Dial the connection
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
// Example using Insecure credentials
conn, err := grpc.DialContext(ctx, "localhost:8081", grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock())
if err != nil {
log.Fatal(err)
}
client := deckard.NewDeckardClient(conn)
defer conn.Close()
// Create a new message
response, err := client.Add(context.Background(), &deckard.AddRequest{
Messages: []*deckard.AddMessage{
{
Id: "1",
Queue: "queue",
TtlMinutes: 10,
Metadata: map[string]string{"key": "value"},
},
},
})
if err != nil {
log.Fatal(err)
}
if response.CreatedCount != 1 {
log.Fatal("Message not added")
}
// Pull messages from the queue
pullResponse, err := client.Pull(context.Background(), &deckard.PullRequest{Queue: "queue", Amount: 1})
if err != nil {
log.Fatal(err)
}
// Do something with the message
log.Println(pullResponse.Messages[0].Score)
// Ack message
client.Ack(context.Background(), &deckard.AckRequest{
Id: pullResponse.Messages[0].Id,
Queue: "queue",
// Remove the message from the queue
RemoveMessage: true,
// Lock message for 10 seconds before it can be pulled again
//LockMs: 10000,
})
}