PLSPLSPLS
This commit is contained in:
parent
c7bfda3f8e
commit
9e5da7a63b
2 changed files with 14 additions and 10 deletions
|
@ -2,6 +2,7 @@ package adapters
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
|
||||
"atri.dad/lib"
|
||||
|
@ -21,20 +22,24 @@ 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")
|
||||
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)
|
||||
|
||||
return RedisClient
|
||||
return RedisClient, nil
|
||||
}
|
||||
|
||||
func (m *RedisPubSubMessage) ReceiveMessage(ctx context.Context) (*pubsub.Message, error) {
|
||||
|
|
9
main.go
9
main.go
|
@ -29,15 +29,14 @@ func main() {
|
|||
godotenv.Load(".env")
|
||||
|
||||
// Initialize Redis client
|
||||
adapters.RedisClient = adapters.NewRedisClient()
|
||||
redisClient, redisError := adapters.NewRedisClient()
|
||||
|
||||
// Test Redis connection
|
||||
_, err := adapters.RedisClient.Ping(context.Background()).Result()
|
||||
adapters.RedisClient = redisClient
|
||||
|
||||
// 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 {
|
||||
|
|
Loading…
Add table
Reference in a new issue