Skip to content

Commit

Permalink
Merge pull request #18 from terratensor/15-q1-not-found
Browse files Browse the repository at this point in the history
15 q1 not found
  • Loading branch information
audetv authored Oct 9, 2023
2 parents 056bb4f + 05746fe commit fb9eaa0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 29 deletions.
8 changes: 3 additions & 5 deletions consumer/internal/infra/msgreceiver/msgreceiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ import (
"gocloud.dev/pubsub"
)

func Run(ctx context.Context, chout chan msghandler.Request, wg *sync.WaitGroup) {
func Run(ctx context.Context, chout chan msghandler.Request, wg *sync.WaitGroup) error {
defer wg.Done()

// pubsub.OpenSubscription creates a *pubsub.Subscription from a URL.
// This URL will Dial the RabbitMQ server at the URL in the environment
// variable RABBIT_SERVER_URL and open the queue "myqueue".
subs, err := pubsub.OpenSubscription(ctx, os.Getenv("Q1"))
if err != nil {
sentry.CaptureMessage(fmt.Sprint(err))
log.Panic(err)
return err
}

defer func(subs *pubsub.Subscription, ctx context.Context) {
Expand All @@ -36,9 +35,8 @@ func Run(ctx context.Context, chout chan msghandler.Request, wg *sync.WaitGroup)
for {
msg, err := subs.Receive(ctx)
if err != nil {
sentry.CaptureMessage(fmt.Sprint(err))
log.Printf("Receiving message: %v", err)
break
return err
}
select {
case <-ctx.Done():
Expand Down
53 changes: 35 additions & 18 deletions consumer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,48 @@ func main() {

ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)

contents, err := os.ReadFile(os.Getenv("SENTRY_DSN_FILE"))
if err != nil {
log.Fatalf("can not read SENTRY_DSN_FILE")
}
dsn := fmt.Sprintf("%v", strings.Trim(string(contents), "\r\n"))

err = sentry.Init(sentry.ClientOptions{
Dsn: dsn,
// Set TracesSampleRate to 1.0 to capture 100%
// of transactions for performance monitoring.
// We recommend adjusting this value in production,
TracesSampleRate: 1.0,
})
if err != nil {
log.Fatalf("sentry.Init: %s", err)
mode := os.Getenv("APP_ENV")

if mode == "prod" {
contents, err := os.ReadFile(os.Getenv("SENTRY_DSN_FILE"))
if err != nil {
log.Fatalf("can not read SENTRY_DSN_FILE")
}
dsn := fmt.Sprintf("%v", strings.Trim(string(contents), "\r\n"))

err = sentry.Init(sentry.ClientOptions{
Dsn: dsn,
// Set TracesSampleRate to 1.0 to capture 100%
// of transactions for performance monitoring.
// We recommend adjusting this value in production,
TracesSampleRate: 1.0,
})
if err != nil {
log.Fatalf("sentry.Init: %s", err)
}
}

ch := make(chan msghandler.Request, 100)
wg := &sync.WaitGroup{}
wg.Add(2)
go msgreceiver.Run(ctx, ch, wg)

go func() {
err := msgreceiver.Run(ctx, ch, wg)
// Обрабатываем ошибку и выходим с кодом 1, для того чтобы инициировать перезапуск докер контейнера.
// Возможно тут имеет смысл сделать сервис проверки health, но пока так
if err != nil {
log.Printf("%v\r\n failure, restart required", err)
sentry.CaptureMessage(fmt.Sprint(err))
os.Exit(1)
}
}()

go msghandler.Handler(ctx, ch, wg)

// Flush buffered events before the program terminates.
defer sentry.Flush(2 * time.Second)
if mode == "PROD" {
// Flush buffered events before the program terminates.
defer sentry.Flush(2 * time.Second)
}

wg.Wait()
stop()
Expand Down
4 changes: 1 addition & 3 deletions docker-compose-production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ services:
- rmq-net
- tg-bot-net
environment:
APP_ENV: prod
RABBIT_SERVER_URL: amqp://rmq
RABBIT_EXCHANGE_NAME: ex1
TG_CHAT_ID: "@svoddru"
Expand All @@ -28,9 +29,6 @@ services:
constraints: [ node.role == manager ]
restart_policy:
condition: on-failure
delay: 10s
max_attempts: 100
window: 120s

secrets:
tg_bot_token:
Expand Down
4 changes: 1 addition & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ services:
- rmq-net
- tg-bot-net
environment:
APP_ENV: dev
RABBIT_SERVER_URL: amqp://rmq
RABBIT_EXCHANGE_NAME: ex1
TG_CHAT_ID: "-1001700331408"
Expand All @@ -35,9 +36,6 @@ services:
deploy:
restart_policy:
condition: on-failure
delay: 10s
max_attempts: 100
window: 120s

secrets:
tg_bot_token:
Expand Down

0 comments on commit fb9eaa0

Please sign in to comment.