From f044fb553f6269fec42e18d4db76c6b29d981abc Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 25 Mar 2024 16:24:14 +0300 Subject: [PATCH] move publish privilege to gorutine with retry if have some troubles --- app/app.go | 9 ++++----- tools/publishPriv.go | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 tools/publishPriv.go diff --git a/app/app.go b/app/app.go index c36e940..1483f45 100644 --- a/app/app.go +++ b/app/app.go @@ -16,6 +16,8 @@ import ( "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/utils" "penahub.gitlab.yandexcloud.net/backend/quiz/core.git/clients/auth" "penahub.gitlab.yandexcloud.net/backend/quiz/core.git/service" + "penahub.gitlab.yandexcloud.net/backend/quiz/core.git/tools" + "time" ) type App struct { @@ -50,7 +52,7 @@ type Options struct { CrtFile string `env:"CRT" default:"server.crt"` KeyFile string `env:"KEY" default:"server.key"` PostgresCredentials string `env:"PG_CRED" default:"host=localhost port=5432 user=squiz password=Redalert2 dbname=squiz sslmode=disable"` - HubAdminUrl string `env:"HUB_ADMIN_URL"` + HubAdminUrl string `env:"HUB_ADMIN_URL" default:"http://localhost:8001/"` ServiceName string `env:"SERVICE_NAME" default:"squiz"` AuthServiceURL string `env:"AUTH_URL"` RedirectURL string `env:"REDIRECT_URL" default:"https://squiz.pena.digital"` @@ -112,10 +114,7 @@ func New(ctx context.Context, opts interface{}, ver appInit.Version) (appInit.Co } fiberClient := &fiber.Client{} privilegeController := privilege.NewPrivilege(clientData, fiberClient) - err = privilegeController.PublishPrivileges() - if err != nil { - fmt.Println("Failed to publish privileges", err) - } + go tools.PublishPrivilege(privilegeController, 10, 5*time.Minute) encrypt := utils.NewEncrypt(options.PubKey, options.PrivKey) app := fiber.New() diff --git a/tools/publishPriv.go b/tools/publishPriv.go new file mode 100644 index 0000000..6326e5b --- /dev/null +++ b/tools/publishPriv.go @@ -0,0 +1,23 @@ +package tools + +import ( + "fmt" + "penahub.gitlab.yandexcloud.net/backend/penahub_common/privilege" + "time" +) + +func PublishPrivilege(privilegeController *privilege.Controller, count int, interval time.Duration) { + for try := 0; try < count; try++ { + err := privilegeController.PublishPrivileges() + if err == nil { + fmt.Println("Privileges published success") + return + } + + fmt.Printf("Failed to publish privileges (try %d): %v\n", try, err) + + time.Sleep(interval) + } + + fmt.Println("Failed to publish privileges after all trys") +}