move publish privilege to gorutine with retry if have some troubles

This commit is contained in:
Pavel 2024-03-25 16:24:14 +03:00
parent d645d50f6c
commit f044fb553f
2 changed files with 27 additions and 5 deletions

@ -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()

23
tools/publishPriv.go Normal file

@ -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")
}