Skip to content

Commit

Permalink
Improve wait-for
Browse files Browse the repository at this point in the history
  • Loading branch information
m110 committed Oct 18, 2024
1 parent 83e97d8 commit 224ea1b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ test_reconnect:

wait:
go run github.com/ThreeDotsLabs/wait-for@latest localhost:5672
go run github.com/ThreeDotsLabs/wait-for@latest localhost:15672
go run ./internal/wait-for

build:
go build ./...
Expand Down
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ services:
restart: unless-stopped
ports:
- 5672:5672
- 15672:15672
56 changes: 56 additions & 0 deletions internal/wait-for/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"fmt"
"os"
"time"

"github.com/ThreeDotsLabs/watermill"
"github.com/ThreeDotsLabs/watermill-amqp/v3/pkg/amqp"
)

func main() {
for i := 0; i < 10; i++ {
err := tryConnecting()
if err == nil {
os.Exit(0)
}

time.Sleep(1 * time.Second)
}

fmt.Println("Failed to connect to AMQP")
os.Exit(1)
}

func amqpURI() string {
uri := os.Getenv("WATERMILL_TEST_AMQP_URI")
if uri != "" {
return uri
}

return "amqp://guest:guest@localhost:5672/"
}

func tryConnecting() error {
logger := watermill.NewStdLogger(false, false)
uri := amqpURI()

_, err := amqp.NewPublisher(
amqp.NewDurablePubSubConfig(uri, nil),
logger,
)
if err != nil {
return err
}

_, err = amqp.NewSubscriber(
amqp.NewDurablePubSubConfig(uri, amqp.GenerateQueueNameTopicNameWithSuffix("wait-for")),
logger,
)
if err != nil {
return err
}

return nil
}

0 comments on commit 224ea1b

Please sign in to comment.