This commit is contained in:
2025-05-24 07:24:17 +04:00
parent 5d241e9ade
commit e60b9f62f1
7 changed files with 454 additions and 366 deletions

View File

@@ -19,31 +19,51 @@ type RedisJobManager struct {
// NewRedisJobManager creates a new Redis job manager
func NewRedisJobManager(tcpPort int, unixSocketPath string) (*RedisJobManager, error) {
// Determine network type and address
var networkType, addr string
var client *redis.Client
var err error
// Try Unix socket first if provided
if unixSocketPath != "" {
networkType = "unix"
addr = unixSocketPath
} else {
networkType = "tcp"
addr = fmt.Sprintf("localhost:%d", tcpPort)
log.Printf("Attempting to connect to Redis via Unix socket: %s", unixSocketPath)
client = redis.NewClient(&redis.Options{
Network: "unix",
Addr: unixSocketPath,
DB: 0,
DialTimeout: 2 * time.Second,
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
})
// Test connection
ctx := context.Background()
_, pingErr := client.Ping(ctx).Result()
if pingErr != nil {
log.Printf("Failed to connect to Redis via Unix socket: %v, falling back to TCP", pingErr)
// Close the failed client
client.Close()
err = pingErr // Update the outer err variable
}
}
// Create Redis client
client := redis.NewClient(&redis.Options{
Network: networkType,
Addr: addr,
DB: 0,
DialTimeout: 5 * time.Second,
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
})
// If Unix socket connection failed or wasn't provided, use TCP
if unixSocketPath == "" || err != nil {
tcpAddr := fmt.Sprintf("localhost:%d", tcpPort)
log.Printf("Connecting to Redis via TCP: %s", tcpAddr)
client = redis.NewClient(&redis.Options{
Network: "tcp",
Addr: tcpAddr,
DB: 0,
DialTimeout: 5 * time.Second,
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
})
}
// Test connection
ctx := context.Background()
_, err := client.Ping(ctx).Result()
if err != nil {
return nil, fmt.Errorf("failed to connect to Redis: %w", err)
_, pingErr := client.Ping(ctx).Result()
if pingErr != nil {
return nil, fmt.Errorf("failed to connect to Redis: %w", pingErr)
}
return &RedisJobManager{