fix: admin 初始化在数据库未就绪时静默失败

initAdmin 改为后台 goroutine 并重试最多 30 秒,避免 users-rpc
启动时 PostgreSQL 尚未接受连接导致管理员账户创建失败。
This commit is contained in:
zetaloop
2026-04-05 17:30:35 +08:00
parent de32143b6d
commit 168cec6d57
+15 -1
View File
@@ -4,6 +4,7 @@ import (
"context"
"os"
"strings"
"time"
"juwan-backend/app/snowflake/rpc/snowflake"
"juwan-backend/app/users/rpc/internal/models/users"
@@ -21,11 +22,23 @@ func initAdmin(svcCtx *svc.ServiceContext) {
return
}
go func() {
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 {
return
}
break
}
hashedPassword, err := pwdUtils.HashPassword(password)
if err != nil {
@@ -57,4 +70,5 @@ func initAdmin(svcCtx *svc.ServiceContext) {
}
logx.Infof("initialized admin user: %s", username)
}()
}