Template
1
0
Fork 0

Pubsub fix

This commit is contained in:
Atridad Lahiji 2024-04-09 12:04:40 -06:00
parent b1714d8ec5
commit f010a88530
No known key found for this signature in database
2 changed files with 8 additions and 11 deletions

View file

@ -21,9 +21,9 @@ type RedisPubSub struct {
Client *redis.Client
}
func NewRedisClient() *redis.Client {
func NewRedisClient() (*redis.Client, error) {
if RedisClient != nil {
return RedisClient
return RedisClient, nil
}
godotenv.Load(".env")
@ -32,13 +32,13 @@ func NewRedisClient() *redis.Client {
opts, err := redis.ParseURL(redis_url)
if err != nil {
return nil
return nil, err
}
lib.LogInfo.Printf("\n[PUBSUB/REDIS]Connecting to Redis at %s\n", opts.Addr)
RedisClient = redis.NewClient(opts)
return RedisClient
return RedisClient, nil
}
func (m *RedisPubSubMessage) ReceiveMessage(ctx context.Context) (*pubsub.Message, error) {

11
main.go
View file

@ -1,7 +1,6 @@
package main
import (
"context"
"embed"
"flag"
"fmt"
@ -27,18 +26,16 @@ func main() {
godotenv.Load(".env")
// Initialize Redis client
adapters.RedisClient = adapters.NewRedisClient()
// Test Redis connection
_, err := adapters.RedisClient.Ping(context.Background()).Result()
redisClient, redisError := adapters.NewRedisClient()
// Initialize pubsub
var pubSub pubsub.PubSub
if err != nil {
lib.LogWarning.Printf("\n[PUBSUB/INIT] Failed to connect to Redis: %v\n", err)
if redisError != nil {
lib.LogWarning.Printf("\n[PUBSUB/INIT] Failed to connect to Redis: %v\n", redisError)
lib.LogWarning.Printf("\n[PUBSUB/INIT] Falling back to LocalPubSub\n")
pubSub = &adapters.LocalPubSub{}
} else {
adapters.RedisClient = redisClient
pubSub = &adapters.RedisPubSub{
Client: adapters.RedisClient,
}