sp4-verify-1770325799/services/chat-svc/internal/client/queue.go
rdev-worker 36d73dd23d
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
build: /implement-feature mesh-interop --requirements 'Chat Service must cal...
2026-02-05 21:40:58 +00:00

69 lines
1.8 KiB
Go

package client
import (
"context"
"fmt"
"os"
"github.com/redis/go-redis/v9"
"git.threesix.ai/jordan/sp4-verify-1770325799/pkg/logging"
"git.threesix.ai/jordan/sp4-verify-1770325799/pkg/queue"
)
// QueueClient provides access to the Redis job queue for pushing tasks.
type QueueClient struct {
producer *queue.RedisQueue
redis *redis.Client
}
// NewQueueClient creates a new Redis queue client.
// Uses REDIS_URL environment variable for connection.
func NewQueueClient(logger *logging.Logger) (*QueueClient, error) {
redisURL := os.Getenv("REDIS_URL")
if redisURL == "" {
return nil, fmt.Errorf("REDIS_URL environment variable not set")
}
opts, err := redis.ParseURL(redisURL)
if err != nil {
return nil, fmt.Errorf("invalid REDIS_URL: %w", err)
}
client := redis.NewClient(opts)
// Test connection
if err := client.Ping(context.Background()).Err(); err != nil {
return nil, fmt.Errorf("redis connection failed: %w", err)
}
return &QueueClient{
producer: queue.NewRedisQueue(client, logger),
redis: client,
}, nil
}
// PushTask enqueues a task for the worker to process.
func (c *QueueClient) PushTask(ctx context.Context, taskType string, payload map[string]any) (string, error) {
return c.producer.Enqueue(ctx, taskType, payload)
}
// PushTaskWithPriority enqueues a task with a specific priority (higher = more urgent).
func (c *QueueClient) PushTaskWithPriority(ctx context.Context, taskType string, payload map[string]any, priority int) (string, error) {
return c.producer.EnqueueWithOptions(ctx, queue.Job{
Type: taskType,
Payload: payload,
Priority: priority,
})
}
// Close closes the Redis connection.
func (c *QueueClient) Close() error {
return c.redis.Close()
}
// HealthCheck verifies the Redis connection.
func (c *QueueClient) HealthCheck(ctx context.Context) error {
return c.redis.Ping(ctx).Err()
}