-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpub.go
109 lines (91 loc) · 2.51 KB
/
pub.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
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
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/redis/go-redis/v9"
)
// Message represents the data structure to be published
type Message struct {
ID string `json:"id"`
Content string `json:"content"`
Timestamp time.Time `json:"timestamp"`
}
func main() {
// Retrieve configuration from environment variables with default fallback
redisAddr := os.Getenv("REDIS_ADDR")
if redisAddr == "" {
redisAddr = "localhost:6379"
}
channel := os.Getenv("CHANNEL")
if channel == "" {
channel = "messages"
}
// Set up logger
logger := log.New(os.Stdout, "[PUBLISHER] ", log.LstdFlags)
logger.Printf("Starting publisher service. Publishing to channel: %s", channel)
// Connect to DragonFlyDB
client := redis.NewClient(&redis.Options{
Addr: redisAddr,
Password: "", // no password set
DB: 0, // use default DB
})
// Create a context that will be canceled on SIGINT or SIGTERM
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Set up signal handling for graceful shutdown
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
// Check connection
_, err := client.Ping(ctx).Result()
if err != nil {
logger.Fatalf("Failed to connect to DragonFlyDB: %v", err)
}
logger.Println("Connected to DragonFlyDB successfully")
// Start publishing in a separate goroutine
go func() {
messageCount := 0
for {
select {
case <-ctx.Done():
return
default:
messageCount++
// Create a new message
msg := Message{
ID: fmt.Sprintf("msg-%d", messageCount),
Content: fmt.Sprintf("This is message #%d", messageCount),
Timestamp: time.Now(),
}
// Convert message to JSON
jsonMsg, err := json.Marshal(msg)
if err != nil {
logger.Printf("Error marshaling message: %v", err)
continue
}
// Publish message to channel
err = client.Publish(ctx, channel, jsonMsg).Err()
if err != nil {
logger.Printf("Error publishing message: %v", err)
} else {
logger.Printf("Published message: %s", string(jsonMsg))
}
// Wait before sending the next message
time.Sleep(2 * time.Second)
}
}
}()
// Wait for termination signal
<-sigChan
logger.Println("Shutdown signal received, closing connections...")
// Close the Redis client connection
if err := client.Close(); err != nil {
logger.Printf("Error closing Redis connection: %v", err)
}
logger.Println("Publisher service stopped")
}