package broker import ( "context" "encoding/json" "fmt" "sync" "time" "mtusi2026lab5/internal/domain" amqp "github.com/rabbitmq/amqp091-go" ) const QueueName = "attachment.process.v1" type RabbitMQ struct { connection *amqp.Connection channel *amqp.Channel confirms <-chan amqp.Confirmation publishMu sync.Mutex } func Open(url string, prefetch int) (*RabbitMQ, error) { if url == "" { return nil, fmt.Errorf("broker URL is required") } connection, err := amqp.DialConfig(url, amqp.Config{Heartbeat: 10 * time.Second, Locale: "en_US"}) if err != nil { return nil, fmt.Errorf("connect RabbitMQ: %w", err) } channel, err := connection.Channel() if err != nil { _ = connection.Close() return nil, fmt.Errorf("open RabbitMQ channel: %w", err) } if _, err = channel.QueueDeclare(QueueName, true, false, false, false, nil); err != nil { _ = channel.Close() _ = connection.Close() return nil, fmt.Errorf("declare RabbitMQ queue: %w", err) } if prefetch > 0 { if err = channel.Qos(prefetch, 0, false); err != nil { _ = channel.Close() _ = connection.Close() return nil, fmt.Errorf("set RabbitMQ prefetch: %w", err) } } client := &RabbitMQ{connection: connection, channel: channel} if err = channel.Confirm(false); err != nil { client.Close() return nil, fmt.Errorf("enable RabbitMQ publisher confirms: %w", err) } client.confirms = channel.NotifyPublish(make(chan amqp.Confirmation, 1)) return client, nil } func (client *RabbitMQ) Publish(ctx context.Context, job domain.AttachmentJob) error { body, err := json.Marshal(job.Message()) if err != nil { return fmt.Errorf("encode attachment job: %w", err) } client.publishMu.Lock() defer client.publishMu.Unlock() if err = client.channel.PublishWithContext(ctx, "", QueueName, false, false, amqp.Publishing{ DeliveryMode: amqp.Persistent, ContentType: "application/json", Type: domain.AttachmentJobType, MessageId: job.JobID, Timestamp: time.Now().UTC(), Body: body, }); err != nil { return fmt.Errorf("publish attachment job: %w", err) } select { case confirmation, ok := <-client.confirms: if !ok { return fmt.Errorf("publisher confirm channel closed") } if !confirmation.Ack { return fmt.Errorf("broker rejected attachment job") } return nil case <-ctx.Done(): return fmt.Errorf("wait for publisher confirm: %w", ctx.Err()) } } func (client *RabbitMQ) Consume(consumerName string) (<-chan amqp.Delivery, error) { deliveries, err := client.channel.Consume(QueueName, consumerName, false, false, false, false, nil) if err != nil { return nil, fmt.Errorf("consume attachment jobs: %w", err) } return deliveries, nil } func Decode(delivery amqp.Delivery) (domain.AttachmentJobMessage, error) { var message domain.AttachmentJobMessage if err := json.Unmarshal(delivery.Body, &message); err != nil { return message, fmt.Errorf("decode attachment job: %w", err) } if message.SchemaVersion != 1 || message.Type != domain.AttachmentJobType || message.JobID == "" || message.AttachmentID == "" || message.PipelineVersion < 1 || message.Attempt < 1 { return message, fmt.Errorf("attachment job has invalid required fields") } return message, nil } func (client *RabbitMQ) Close() error { if client == nil { return nil } if client.channel != nil { _ = client.channel.Close() } if client.connection != nil { return client.connection.Close() } return nil }