Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/weni-ai/courier into feat/w…
Browse files Browse the repository at this point in the history
…pp-buttons
  • Loading branch information
paulobernardoaf committed Nov 27, 2024
2 parents 1b009f2 + a2e98ce commit 028056c
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 60 deletions.
23 changes: 23 additions & 0 deletions WENI-CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
1.18.5
----------
* Fix: document name on template message for whatsapp cloud

1.18.4
----------
* Fix: Handle error for Whatsapp messages without text but with quick replies and attachments

1.18.3
----------
* Hotfix: MsgParts index out of range

1.18.2
----------
* Feat: configuration for billing queue name on env var

1.18.1
----------
* Fix: document name when message have quick replies on whatsapp handler
* Fix: document name when message have quick replies on facebookapp handler for whatsapp cloud api
* Fix: backslashes for headerText, footer and ctaMessage
* Fix: handle empty flow data to not be sent as empty object in request

1.18.0
----------
* Search for contact and update a Teams contact with the serviceURL
Expand Down
10 changes: 5 additions & 5 deletions billing/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
"github.com/sirupsen/logrus"
)

const QUEUE_NAME = "billing-backup"

// Message represents a object that is sent to the billing service
//
// {
Expand Down Expand Up @@ -61,10 +59,11 @@ type Client interface {
type rabbitmqRetryClient struct {
publisher rabbitroutine.Publisher
conn *rabbitroutine.Connector
queueName string
}

// NewRMQBillingResilientClient creates a new billing service client implementation using RabbitMQ with publish retry and reconnect features
func NewRMQBillingResilientClient(url string, retryAttempts int, retryDelay int) (Client, error) {
func NewRMQBillingResilientClient(url string, retryAttempts int, retryDelay int, queueName string) (Client, error) {
cconn, err := amqp.Dial(url)
if err != nil {
return nil, err
Expand All @@ -77,7 +76,7 @@ func NewRMQBillingResilientClient(url string, retryAttempts int, retryDelay int)
}
defer ch.Close()
_, err = ch.QueueDeclare(
QUEUE_NAME,
queueName,
false,
false,
false,
Expand Down Expand Up @@ -126,6 +125,7 @@ func NewRMQBillingResilientClient(url string, retryAttempts int, retryDelay int)
return &rabbitmqRetryClient{
publisher: pub,
conn: conn,
queueName: queueName,
}, nil
}

Expand All @@ -135,7 +135,7 @@ func (c *rabbitmqRetryClient) Send(msg Message) error {
err := c.publisher.Publish(
ctx,
"",
QUEUE_NAME,
c.queueName,
amqp.Publishing{
ContentType: "application/json",
Body: msgMarshalled,
Expand Down
22 changes: 12 additions & 10 deletions billing/billing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/stretchr/testify/assert"
)

const billingTestQueueName = "testqueue"

func TestInitialization(t *testing.T) {
connURL := "amqp://localhost:5672/"
conn, err := amqp.Dial(connURL)
Expand All @@ -25,7 +27,7 @@ func TestInitialization(t *testing.T) {
}
defer conn.Close()
defer ch.Close()
defer ch.QueueDelete(QUEUE_NAME, false, false, false)
defer ch.QueueDelete(billingTestQueueName, false, false, false)
}

func TestBillingResilientClient(t *testing.T) {
Expand All @@ -37,7 +39,7 @@ func TestBillingResilientClient(t *testing.T) {
t.Fatal(errors.Wrap(err, "failed to declare a channel for consumer"))
}
defer ch.Close()
defer ch.QueueDelete(QUEUE_NAME, false, false, false)
defer ch.QueueDelete(billingTestQueueName, false, false, false)

msgUUID, _ := uuid.NewV4()
msg := NewMessage(
Expand All @@ -53,14 +55,14 @@ func TestBillingResilientClient(t *testing.T) {
nil,
)

billingClient, err := NewRMQBillingResilientClient(connURL, 3, 1000)
billingClient, err := NewRMQBillingResilientClient(connURL, 3, 1000, billingTestQueueName)
time.Sleep(1 * time.Second)
assert.NoError(t, err)
err = billingClient.Send(msg)
assert.NoError(t, err)

msgs, err := ch.Consume(
QUEUE_NAME,
billingTestQueueName,
"",
true,
false,
Expand Down Expand Up @@ -101,7 +103,7 @@ func TestBillingResilientClientSendAsync(t *testing.T) {
t.Fatal(errors.Wrap(err, "failed to declare a channel for consumer"))
}
defer ch.Close()
defer ch.QueueDelete(QUEUE_NAME, false, false, false)
defer ch.QueueDelete(billingTestQueueName, false, false, false)

msgUUID, _ := uuid.NewV4()
msg := NewMessage(
Expand All @@ -117,14 +119,14 @@ func TestBillingResilientClientSendAsync(t *testing.T) {
nil,
)

billingClient, err := NewRMQBillingResilientClient(connURL, 3, 1000)
billingClient, err := NewRMQBillingResilientClient(connURL, 3, 1000, billingTestQueueName)
time.Sleep(1 * time.Second)
assert.NoError(t, err)
billingClient.SendAsync(msg, nil, nil)

assert.NoError(t, err)
msgs, err := ch.Consume(
QUEUE_NAME,
billingTestQueueName,
"",
true,
false,
Expand Down Expand Up @@ -165,7 +167,7 @@ func TestBillingResilientClientSendAsyncWithPanic(t *testing.T) {
t.Fatal(errors.Wrap(err, "failed to declare a channel for consumer"))
}
defer ch.Close()
defer ch.QueueDelete(QUEUE_NAME, false, false, false)
defer ch.QueueDelete(billingTestQueueName, false, false, false)

msgUUID, _ := uuid.NewV4()
msg := NewMessage(
Expand All @@ -181,15 +183,15 @@ func TestBillingResilientClientSendAsyncWithPanic(t *testing.T) {
nil,
)

billingClient, err := NewRMQBillingResilientClient(connURL, 3, 1000)
billingClient, err := NewRMQBillingResilientClient(connURL, 3, 1000, billingTestQueueName)
time.Sleep(1 * time.Second)
assert.NoError(t, err)
time.Sleep(1 * time.Second)
billingClient.SendAsync(msg, nil, func() { panic("test panic") })

assert.NoError(t, err)
msgs, err := ch.Consume(
QUEUE_NAME,
billingTestQueueName,
"",
false,
false,
Expand Down
2 changes: 1 addition & 1 deletion cmd/courier/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func main() {

if config.RabbitmqURL != "" {
billingClient, err := billing.NewRMQBillingResilientClient(
config.RabbitmqURL, config.RabbitmqRetryPubAttempts, config.RabbitmqRetryPubDelay)
config.RabbitmqURL, config.RabbitmqRetryPubAttempts, config.RabbitmqRetryPubDelay, config.BillingQueueName)
if err != nil {
logrus.Fatalf("Error creating billing RabbitMQ client: %v", err)
}
Expand Down
2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type Config struct {
RabbitmqURL string `help:"rabbitmq url"`
RabbitmqRetryPubAttempts int `help:"rabbitmq retry attempts"`
RabbitmqRetryPubDelay int `help:"rabbitmq retry delay"`
BillingQueueName string `help:"billing queue name"`

EmailProxyURL string `help:"email proxy url"`
EmailProxyAuthToken string `help:"email proxy auth token"`
Expand Down Expand Up @@ -90,6 +91,7 @@ func NewConfig() *Config {
WaitMediaChannels: []string{},
RabbitmqRetryPubAttempts: 3,
RabbitmqRetryPubDelay: 1000,
BillingQueueName: "billing-backup",
EmailProxyURL: "http://localhost:9090",
EmailProxyAuthToken: "",
}
Expand Down
4 changes: 2 additions & 2 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
FROM golang:1.17.5-alpine3.14 AS builder
FROM golang:1.23.3-alpine3.19 AS builder

WORKDIR /app

RUN apk add --no-cache --virtual build-deps curl gcc
RUN apk add --no-cache --virtual build-deps curl gcc libc-dev

COPY go.sum go.mod ./
RUN --mount=type=cache,target=/go/pkg/mod/ \
Expand Down
1 change: 1 addition & 0 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (h *dummyHandler) Initialize(s Server) error {
"amqp://localhost:5672/",
3,
100,
s.Config().BillingQueueName,
)
if err != nil {
logrus.Fatalf("Error creating billing RabbitMQ client: %v", err)
Expand Down
Loading

0 comments on commit 028056c

Please sign in to comment.