Add RabbitMQ connect retry and healthcheck

This commit is contained in:
Nicolas Will
2024-06-15 00:39:21 +02:00
parent ea10403f6c
commit 1a574c2f7f
2 changed files with 26 additions and 3 deletions

View File

@@ -100,11 +100,32 @@ func InitializeQueue(jobsQueueName, resultsQueueName string) (*RabbitMQQueue, er
rabbitMQURL := fmt.Sprintf("amqp://%s:%s@%s:%s/", rabbitMQUser, rabbitMQPassword, rabbitMQHost, rabbitMQPort)
conn, err := amqp.Dial(rabbitMQURL)
const (
tryCount = 5
retryDelaySec = 3
)
var conn *amqp.Connection
var err error
for i := 0; i < tryCount; i++ {
slog.Info("Attempting to connect to RabbitMQ", slog.Int("attempt", i+1))
conn, err = amqp.Dial(rabbitMQURL)
if err != nil {
slog.Warn("Failed to connect to RabbitMQ: %w", err)
if i < tryCount-1 {
slog.Info("Retrying in %d seconds", retryDelaySec)
time.Sleep(retryDelaySec * time.Second)
}
}
}
if err != nil {
return nil, fmt.Errorf("failed to connect to RabbitMQ: %w", err)
}
slog.Info("Connected to RabbitMQ")
ch, err := conn.Channel()
if err != nil {
conn.Close()

View File

@@ -32,8 +32,10 @@ services:
networks:
- backend
healthcheck:
test: [ "CMD", "rabbitmqctl", "status" ]
interval: 1s
test: [ "CMD", "nc", "-z", "localhost", "5672" ]
interval: 5s
timeout: 15s
retries: 1
server:
build: