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

11
main.go
View file

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