fix: admin 初始化在数据库未就绪时静默失败
initAdmin 改为后台 goroutine 并重试最多 30 秒,避免 users-rpc 启动时 PostgreSQL 尚未接受连接导致管理员账户创建失败。
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"juwan-backend/app/snowflake/rpc/snowflake"
|
"juwan-backend/app/snowflake/rpc/snowflake"
|
||||||
"juwan-backend/app/users/rpc/internal/models/users"
|
"juwan-backend/app/users/rpc/internal/models/users"
|
||||||
@@ -21,11 +22,23 @@ func initAdmin(svcCtx *svc.ServiceContext) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
exists, _ := svcCtx.UsersModelRW.Users.Query().Where(users.UsernameEQ(username)).Exist(ctx)
|
for i := range 30 {
|
||||||
|
exists, err := svcCtx.UsersModelRW.Users.Query().Where(users.UsernameEQ(username)).Exist(ctx)
|
||||||
|
if err != nil {
|
||||||
|
if i < 29 {
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
logx.Errorf("check admin user: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
if exists {
|
if exists {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
hashedPassword, err := pwdUtils.HashPassword(password)
|
hashedPassword, err := pwdUtils.HashPassword(password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -57,4 +70,5 @@ func initAdmin(svcCtx *svc.ServiceContext) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
logx.Infof("initialized admin user: %s", username)
|
logx.Infof("initialized admin user: %s", username)
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user