From 7ebea00f7dd1a616536317edb43d6a9cff30cfcb Mon Sep 17 00:00:00 2001 From: audetv Date: Sun, 8 Oct 2023 10:04:30 +0300 Subject: [PATCH 1/9] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8F=20?= =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D0=B8=20=D0=BD=D0=B0?= =?UTF-8?q?=20=D0=BE=D1=82=D1=81=D1=83=D1=82=D1=81=D1=82=D0=B2=D0=B8=D0=B5?= =?UTF-8?q?=20=D0=BE=D1=87=D0=B5=D1=80=D0=B5=D0=B4=D0=B8=20=D0=B8=20=D0=B2?= =?UTF-8?q?=D1=8B=D1=85=D0=BE=D0=B4=D0=B0=20=D0=B8=D0=B7=20=D1=81=D0=B8?= =?UTF-8?q?=D1=81=D1=82=D0=B5=D0=BC=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- consumer/internal/infra/msgreceiver/msgreceiver.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/consumer/internal/infra/msgreceiver/msgreceiver.go b/consumer/internal/infra/msgreceiver/msgreceiver.go index a6e536b..d53e7b5 100644 --- a/consumer/internal/infra/msgreceiver/msgreceiver.go +++ b/consumer/internal/infra/msgreceiver/msgreceiver.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "os" + "strings" "sync" "tg-svodd-bot/consumer/internal/infra/msghandler" @@ -36,7 +37,16 @@ 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)) + // Осуществляется проверка по содержимому строки ошибки на совпадение текста. + // Не нашел способа лучше, без привязки к содержимому строки ошибки. + // На самом деле такая ошибка не должна возникать, т.к когда запущен сервер очередей, уже должна быть создана вся структура и очереди + // Надо добавить создание очереди q1 на сервисах svodd + if strings.Contains(fmt.Sprint(err), "NOT_FOUND - no queue 'q1' in vhost '/'") { + log.Printf("failure, restart required: %v", err) + os.Exit(1) + } log.Printf("Receiving message: %v", err) break } From f0ca883d91ed1a0ef9f6658993b01b6900d242a9 Mon Sep 17 00:00:00 2001 From: audetv Date: Sun, 8 Oct 2023 10:05:20 +0300 Subject: [PATCH 2/9] removed max_attempts from docker compose --- docker-compose-production.yml | 2 +- docker-compose.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docker-compose-production.yml b/docker-compose-production.yml index a92c50d..ba8f029 100644 --- a/docker-compose-production.yml +++ b/docker-compose-production.yml @@ -29,7 +29,7 @@ services: restart_policy: condition: on-failure delay: 5s - max_attempts: 5 +# max_attempts: 5 window: 120s secrets: diff --git a/docker-compose.yml b/docker-compose.yml index 52fa1a2..6b56fa6 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -36,7 +36,7 @@ services: restart_policy: condition: on-failure delay: 5s - max_attempts: 5 +# max_attempts: 5 window: 120s secrets: From b9086f719cdb5f4fb2b1973743359da4f6e71153 Mon Sep 17 00:00:00 2001 From: agusev Date: Mon, 9 Oct 2023 16:14:01 +0300 Subject: [PATCH 3/9] =?UTF-8?q?=D0=92=D0=BE=D0=B7=D0=B2=D1=80=D0=B0=D1=89?= =?UTF-8?q?=D0=B0=D0=B5=D0=BC=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D1=83=20?= =?UTF-8?q?=D0=B2=20=D1=81=D0=BB=D1=83=D1=87=D0=B0=D0=B5=20=D0=B5=D0=B5=20?= =?UTF-8?q?=D0=B2=D0=BE=D0=B7=D0=BD=D0=B8=D0=BA=D0=BD=D0=BE=D0=B2=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- consumer/internal/infra/msgreceiver/msgreceiver.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/consumer/internal/infra/msgreceiver/msgreceiver.go b/consumer/internal/infra/msgreceiver/msgreceiver.go index d53e7b5..fe5eadc 100644 --- a/consumer/internal/infra/msgreceiver/msgreceiver.go +++ b/consumer/internal/infra/msgreceiver/msgreceiver.go @@ -14,7 +14,7 @@ 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. @@ -48,7 +48,7 @@ func Run(ctx context.Context, chout chan msghandler.Request, wg *sync.WaitGroup) os.Exit(1) } log.Printf("Receiving message: %v", err) - break + return err } select { case <-ctx.Done(): From 2788fc77d05ece50e136bc2395c9d27acb8f0a9a Mon Sep 17 00:00:00 2001 From: agusev Date: Mon, 9 Oct 2023 16:15:03 +0300 Subject: [PATCH 4/9] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=BF=D0=B0=D1=80=D0=B0=D0=BC=D0=B5=D1=82=D1=80=20?= =?UTF-8?q?=D1=80=D0=B5=D0=B6=D0=B8=D0=BC=D0=B0=20=D0=BF=D1=80=D0=B8=D0=BB?= =?UTF-8?q?=D0=BE=D0=B6=D0=B5=D0=BD=D0=B8=D1=8F=20dev/prod?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker-compose-production.yml | 3 ++- docker-compose.yml | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/docker-compose-production.yml b/docker-compose-production.yml index ba8f029..d1ffb0e 100644 --- a/docker-compose-production.yml +++ b/docker-compose-production.yml @@ -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" @@ -42,4 +43,4 @@ networks: rmq-net: name: rmq-net external: true - tg-bot-net: \ No newline at end of file + tg-bot-net: diff --git a/docker-compose.yml b/docker-compose.yml index 6b56fa6..41ca03b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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" From 06b9986de1f7ebbc5b42d54ee25b62d8df9a71af Mon Sep 17 00:00:00 2001 From: agusev Date: Mon, 9 Oct 2023 16:16:25 +0300 Subject: [PATCH 5/9] =?UTF-8?q?=D0=9E=D0=B1=D1=80=D0=B0=D0=B1=D0=B0=D1=82?= =?UTF-8?q?=D1=8B=D0=B2=D0=B0=D0=B5=D0=BC=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA?= =?UTF-8?q?=D1=83=20=D0=B8=20=D0=B2=D1=8B=D1=85=D0=BE=D0=B4=D0=B8=D0=BC=20?= =?UTF-8?q?=D1=81=20=D0=BD=D0=B5=D0=BD=D1=83=D0=BB=D0=B5=D0=B2=D1=8B=D0=BC?= =?UTF-8?q?=20=D0=BA=D0=BE=D0=B4=D0=BE=D0=BC=20=D0=B8=D0=B7=20=D0=BF=D1=80?= =?UTF-8?q?=D0=B8=D0=BB=D0=BE=D0=B6=D0=B5=D0=BD=D0=B8=D1=8F,=20=D1=87?= =?UTF-8?q?=D1=82=D0=BE=D0=B1=D1=8B=20=D0=B8=D0=BD=D0=B8=D1=86=D0=B8=D0=B8?= =?UTF-8?q?=D1=80=D0=BE=D0=B2=D0=B0=D1=82=D1=8C=20=D0=BF=D0=B5=D1=80=D0=B5?= =?UTF-8?q?=D0=B7=D0=B0=D0=BF=D1=83=D1=81=D0=BA=20=D0=BA=D0=BE=D0=BD=D1=82?= =?UTF-8?q?=D0=B5=D0=B9=D0=BD=D0=B5=D1=80=D0=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- consumer/main.go | 50 +++++++++++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/consumer/main.go b/consumer/main.go index e74cf17..12ed8fa 100644 --- a/consumer/main.go +++ b/consumer/main.go @@ -21,31 +21,45 @@ 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) + if err != nil { + log.Printf("failure, restart required: %v", 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() From 6bd58f26319e45b75afb44b7d77ffb38a5e92629 Mon Sep 17 00:00:00 2001 From: agusev Date: Mon, 9 Oct 2023 16:19:32 +0300 Subject: [PATCH 6/9] =?UTF-8?q?=D0=A3=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=B4=20=D0=B2=D1=8B=D1=85=D0=BE=D0=B4=D0=B0=20?= =?UTF-8?q?=D0=B8=D0=B7=20=D0=BF=D1=80=D0=B8=D0=BB=D0=BE=D0=B6=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F,=20=D0=BE=D1=81=D1=83=D1=89=D0=B5=D1=81=D1=82?= =?UTF-8?q?=D0=B2=D0=BB=D1=8F=D0=B5=D0=BC=20=D0=B2=D0=BE=D0=B7=D0=B2=D1=80?= =?UTF-8?q?=D0=B0=D1=82=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- consumer/internal/infra/msgreceiver/msgreceiver.go | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/consumer/internal/infra/msgreceiver/msgreceiver.go b/consumer/internal/infra/msgreceiver/msgreceiver.go index fe5eadc..10925c8 100644 --- a/consumer/internal/infra/msgreceiver/msgreceiver.go +++ b/consumer/internal/infra/msgreceiver/msgreceiver.go @@ -5,7 +5,6 @@ import ( "fmt" "log" "os" - "strings" "sync" "tg-svodd-bot/consumer/internal/infra/msghandler" @@ -37,16 +36,7 @@ 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)) - // Осуществляется проверка по содержимому строки ошибки на совпадение текста. - // Не нашел способа лучше, без привязки к содержимому строки ошибки. - // На самом деле такая ошибка не должна возникать, т.к когда запущен сервер очередей, уже должна быть создана вся структура и очереди - // Надо добавить создание очереди q1 на сервисах svodd - if strings.Contains(fmt.Sprint(err), "NOT_FOUND - no queue 'q1' in vhost '/'") { - log.Printf("failure, restart required: %v", err) - os.Exit(1) - } log.Printf("Receiving message: %v", err) return err } From aea761d48d0fbbb99154bb2e6efe16faa5ede787 Mon Sep 17 00:00:00 2001 From: agusev Date: Mon, 9 Oct 2023 16:51:18 +0300 Subject: [PATCH 7/9] =?UTF-8?q?=D0=A3=D0=B1=D1=80=D0=B0=D0=BD=D1=8B=20?= =?UTF-8?q?=D0=BE=D0=B3=D1=80=D0=B0=D0=BD=D0=B8=D1=87=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F=20=D0=BD=D0=B0=20=D1=80=D0=B5=D1=81=D1=82=D0=B0=D1=80?= =?UTF-8?q?=D1=82=20=D0=BA=D0=BE=D0=BD=D1=82=D0=B5=D0=B9=D0=BD=D0=B5=D1=80?= =?UTF-8?q?=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker-compose-production.yml | 4 ++-- docker-compose.yml | 3 --- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/docker-compose-production.yml b/docker-compose-production.yml index d1ffb0e..fb51b16 100644 --- a/docker-compose-production.yml +++ b/docker-compose-production.yml @@ -29,8 +29,8 @@ services: constraints: [ node.role == manager ] restart_policy: condition: on-failure - delay: 5s -# max_attempts: 5 + delay: 10s + max_attempts: 5 window: 120s secrets: diff --git a/docker-compose.yml b/docker-compose.yml index 41ca03b..de8acfa 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -36,9 +36,6 @@ services: deploy: restart_policy: condition: on-failure - delay: 5s -# max_attempts: 5 - window: 120s secrets: tg_bot_token: From a79ee5f16e0e84c23d304de61ff0591a6b06d3ce Mon Sep 17 00:00:00 2001 From: agusev Date: Mon, 9 Oct 2023 16:52:52 +0300 Subject: [PATCH 8/9] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=B4=D0=B0=D1=87?= =?UTF-8?q?=D0=B0=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BE=D0=BA=20sentry.CaptureM?= =?UTF-8?q?essage=20=D0=B2=D1=8B=D0=BD=D0=B5=D1=81=D0=B5=D0=BD=D0=B0=20?= =?UTF-8?q?=D0=B2=20main?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- consumer/internal/infra/msgreceiver/msgreceiver.go | 4 +--- consumer/main.go | 5 ++++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/consumer/internal/infra/msgreceiver/msgreceiver.go b/consumer/internal/infra/msgreceiver/msgreceiver.go index 10925c8..af83a79 100644 --- a/consumer/internal/infra/msgreceiver/msgreceiver.go +++ b/consumer/internal/infra/msgreceiver/msgreceiver.go @@ -21,8 +21,7 @@ func Run(ctx context.Context, chout chan msghandler.Request, wg *sync.WaitGroup) // 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) { @@ -36,7 +35,6 @@ 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) return err } diff --git a/consumer/main.go b/consumer/main.go index 12ed8fa..c025925 100644 --- a/consumer/main.go +++ b/consumer/main.go @@ -48,8 +48,11 @@ func main() { go func() { err := msgreceiver.Run(ctx, ch, wg) + // Обрабатываем ошибку и выходим с кодом 1, для того чтобы инициировать перезапуск докер контейнера. + // Возможно тут имеет смысл сделать сервис проверки health, но пока так if err != nil { - log.Printf("failure, restart required: %v", err) + log.Printf("%v\r\n failure, restart required", err) + sentry.CaptureMessage(fmt.Sprint(err)) os.Exit(1) } }() From 6c929efe08d735b0ce4d9c4cd50ed51df7e3861b Mon Sep 17 00:00:00 2001 From: agusev Date: Mon, 9 Oct 2023 16:52:52 +0300 Subject: [PATCH 9/9] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=B4=D0=B0=D1=87?= =?UTF-8?q?=D0=B0=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BE=D0=BA=20sentry.CaptureM?= =?UTF-8?q?essage=20=D0=B2=D1=8B=D0=BD=D0=B5=D1=81=D0=B5=D0=BD=D0=B0=20?= =?UTF-8?q?=D0=B2=20main?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- consumer/internal/infra/msgreceiver/msgreceiver.go | 4 +--- consumer/main.go | 5 ++++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/consumer/internal/infra/msgreceiver/msgreceiver.go b/consumer/internal/infra/msgreceiver/msgreceiver.go index 10925c8..af83a79 100644 --- a/consumer/internal/infra/msgreceiver/msgreceiver.go +++ b/consumer/internal/infra/msgreceiver/msgreceiver.go @@ -21,8 +21,7 @@ func Run(ctx context.Context, chout chan msghandler.Request, wg *sync.WaitGroup) // 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) { @@ -36,7 +35,6 @@ 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) return err } diff --git a/consumer/main.go b/consumer/main.go index 12ed8fa..c025925 100644 --- a/consumer/main.go +++ b/consumer/main.go @@ -48,8 +48,11 @@ func main() { go func() { err := msgreceiver.Run(ctx, ch, wg) + // Обрабатываем ошибку и выходим с кодом 1, для того чтобы инициировать перезапуск докер контейнера. + // Возможно тут имеет смысл сделать сервис проверки health, но пока так if err != nil { - log.Printf("failure, restart required: %v", err) + log.Printf("%v\r\n failure, restart required", err) + sentry.CaptureMessage(fmt.Sprint(err)) os.Exit(1) } }()