-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
98 lines (80 loc) · 2.22 KB
/
main.go
File metadata and controls
98 lines (80 loc) · 2.22 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/openframebox/goqueue"
)
// TaskMessage represents a task to be processed
type TaskMessage struct {
Name string `json:"name"`
Data string `json:"data"`
}
func (t TaskMessage) QueueName() string {
return "tasks"
}
// TaskHandler processes task messages
type TaskHandler struct{}
func (h *TaskHandler) QueueName() string {
return "tasks"
}
func (h *TaskHandler) Handle(ctx context.Context, envelope *goqueue.Envelope) error {
log.Printf("Processing task: %s (retry: %d)", envelope.ID, envelope.RetryCount)
// Simulate work
time.Sleep(100 * time.Millisecond)
return nil
}
func main() {
// Create RabbitMQ backend with all features enabled
backend, err := goqueue.NewRabbitMQBackend(
"", // empty URL - using component config instead
goqueue.WithRabbitMQHost("localhost", 5672),
goqueue.WithRabbitMQAuth("root", "@passWORD1"), // No encoding needed!
goqueue.WithRabbitMQVHost("/"),
goqueue.WithRabbitMQDurableQueues(true),
goqueue.WithRabbitMQPersistence(true),
goqueue.WithRabbitMQPrefetch(10, 0),
goqueue.WithRabbitMQPriority(10),
goqueue.WithRabbitMQDLX("goqueue.dlx", "dead-letter-queue"),
)
if err != nil {
log.Fatalf("Failed to create RabbitMQ backend: %v", err)
}
defer backend.Close()
// Create queue with DLQ enabled
gq := goqueue.New(
backend,
goqueue.WithWorkerCount(5),
goqueue.WithRetryCount(3),
goqueue.WithRetryDelay(1*time.Second),
goqueue.WithDLQ("dead-letter-queue"),
)
// Register handler
if err := gq.Register(&TaskHandler{}); err != nil {
log.Fatalf("Failed to register handler: %v", err)
}
// Start processing
ctx := context.Background()
if err := gq.Start(ctx); err != nil {
log.Fatalf("Failed to start queue: %v", err)
}
// Publish some tasks
for i := 0; i < 10; i++ {
task := TaskMessage{
Name: fmt.Sprintf("Task %d", i+1),
Data: fmt.Sprintf("Data for task %d", i+1),
}
if err := gq.Publish(ctx, task); err != nil {
log.Printf("Failed to publish task: %v", err)
}
}
log.Println("Published 10 tasks")
// Wait a bit for processing
time.Sleep(5 * time.Second)
// Graceful shutdown
if err := gq.Stop(); err != nil {
log.Printf("Error stopping queue: %v", err)
}
log.Println("Done!")
}