PLSPLSPLS

This commit is contained in:
Atridad Lahiji 2024-04-09 11:58:08 -06:00
parent c7bfda3f8e
commit 9e5da7a63b
No known key found for this signature in database
2 changed files with 14 additions and 10 deletions

View file

@ -2,6 +2,7 @@ package adapters
import ( import (
"context" "context"
"errors"
"os" "os"
"atri.dad/lib" "atri.dad/lib"
@ -21,20 +22,24 @@ 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")
redis_url := os.Getenv("REDIS_URL") redis_url := os.Getenv("REDIS_URL")
opts, _ := redis.ParseURL(redis_url) opts, err := redis.ParseURL(redis_url)
lib.LogInfo.Printf("\n[PUBSUB/REDIS]Connecting to Redis at") if err != nil {
return nil, errors.New("math: square root of negative number")
}
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) {

View file

@ -29,15 +29,14 @@ 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 adapters.RedisClient = redisClient
_, 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 {