Skip to content

Commit

Permalink
fix: add Redis initialization and error handling in main server logic
Browse files Browse the repository at this point in the history
  • Loading branch information
PlusOne committed Dec 31, 2024
1 parent 2245430 commit 6b7c03c
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3208,3 +3208,88 @@ func verifyAndRepairThumbnails(thumbnailPaths []string, redisClient *redis.Clien
}
}
}

func initializeRedis(cfg RedisConfig) error {
if !cfg.RedisEnabled {
log.Info("Redis is disabled in the configuration.")
return nil
}

redisClient = redis.NewClient(&redis.Options{
Addr: cfg.RedisAddr,
Password: cfg.RedisPassword,
DB: cfg.RedisDBIndex,
PoolSize: 10,
MinIdleConns: 3,
MaxRetries: 3,
})

// Test the connection
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

_, err := redisClient.Ping(ctx).Result()
if err != nil {
log.Errorf("Failed to connect to Redis at %s: %v", cfg.RedisAddr, err)
return err
}

log.Info("Successfully connected to Redis.")
return nil
}

func main() {
// ...existing setup code...

// Initialize Redis
err := initializeRedis(conf.Redis)
if err != nil {
log.Fatalf("Redis initialization failed: %v", err)
}

// ...rest of the server setup...
}

// ...existing code...

func verifyAndRepairThumbnails(thumbnailPaths []string, redisClient *redis.Client, originalDir string) {
if conf.Redis.RedisEnabled && redisClient == nil {
log.Error("Redis client is nil. Cannot verify and repair thumbnails.")
return
}

// ...existing logic to verify and repair thumbnails...

for _, thumbPath := range thumbnailPaths {
file, err := os.Open(thumbPath)
if err != nil {
log.Warnf("Error opening regenerated thumbnail %s: %v", thumbPath, err)
continue
}
defer file.Close()

h := sha256.New()
h.Reset()
if _, err := io.Copy(h, file); err != nil {
log.Warnf("Error hashing regenerated thumbnail %s: %v", thumbPath, err)
continue
}

newHash := hex.EncodeToString(h.Sum(nil))

// Store new hash in Redis if enabled
if conf.Redis.RedisEnabled && redisClient != nil {
err = redisClient.Set(context.Background(), thumbPath, newHash, 0).Err()
if err != nil {
log.Warnf("Error storing new hash for thumbnail %s in Redis: %v", thumbPath, err)
continue
}
} else {
log.Warnf("Redis is disabled or client is nil. Skipping storing hash for thumbnail %s", thumbPath)
}

log.Infof("Successfully regenerated and updated thumbnail %s", thumbPath)
}
}

// ...existing code...

0 comments on commit 6b7c03c

Please sign in to comment.