Skip to content

Commit

Permalink
fix(redis-client) make sure the server is ready to write (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
wsalles authored Jan 14, 2022
1 parent 79d5a44 commit 8ab490b
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pkg/common/clients/redis_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package clients

import (
"context"
"time"

"github.com/go-redis/redis/v8"
"github.com/ydataai/go-core/pkg/common/logging"
Expand All @@ -14,12 +15,18 @@ type RedisClient struct {

// NewRedisClient creates a new RedisClient (redis.Client) instance.
func NewRedisClient(config RedisConfiguration, logger logging.Logger) RedisClient {
ctx := context.Background()
client := redis.NewClient(&redis.Options{
Addr: config.Address,
})
err := client.Ping(context.Background()).Err()
err := client.Ping(ctx).Err()
if err != nil {
logger.Fatalf("Error while connect to Redis: %s", config.Address)
}
// make sure the redis server is ready to write.
err = client.Set(ctx, "lastUpdate", time.Now(), time.Minute).Err()
if err != nil {
logger.Fatalf("Redis Server is ready-only. Err: %v", err)
}
return RedisClient{client}
}

0 comments on commit 8ab490b

Please sign in to comment.