增加启动过程标记

This commit is contained in:
GoEdgeLab
2022-09-07 15:57:31 +08:00
parent 763f658ad3
commit 312d94821a
2 changed files with 47 additions and 19 deletions

View File

@@ -46,7 +46,7 @@ import (
var sharedAPIConfig *configs.APIConfig = nil var sharedAPIConfig *configs.APIConfig = nil
type APINode struct { type APINode struct {
serviceInstanceMap map[string]interface{} serviceInstanceMap map[string]any
serviceInstanceLocker sync.Mutex serviceInstanceLocker sync.Mutex
sock *gosock.Sock sock *gosock.Sock
@@ -55,11 +55,13 @@ type APINode struct {
issues []*StartIssue issues []*StartIssue
issuesFile string issuesFile string
progress *utils.Progress
} }
func NewAPINode() *APINode { func NewAPINode() *APINode {
return &APINode{ return &APINode{
serviceInstanceMap: map[string]interface{}{}, serviceInstanceMap: map[string]any{},
sock: gosock.NewTmpSock(teaconst.ProcessName), sock: gosock.NewTmpSock(teaconst.ProcessName),
issues: []*StartIssue{}, issues: []*StartIssue{},
@@ -88,6 +90,7 @@ func (this *APINode) Start() {
} }
// 启动IP库 // 启动IP库
this.setProgress("IP_LIBRARY", "开始初始化IP库")
remotelogs.Println("API_NODE", "initializing ip library ...") remotelogs.Println("API_NODE", "initializing ip library ...")
err = iplibrary.InitDefault() err = iplibrary.InitDefault()
if err != nil { if err != nil {
@@ -95,6 +98,7 @@ func (this *APINode) Start() {
} }
// 检查数据库连接 // 检查数据库连接
this.setProgress("DATABASE", "正在检查数据库连接")
err = this.checkDB() err = this.checkDB()
if err != nil { if err != nil {
var errString = "check database connection failed: " + err.Error() var errString = "check database connection failed: " + err.Error()
@@ -105,6 +109,7 @@ func (this *APINode) Start() {
// 自动升级 // 自动升级
logs.Println("[API_NODE]auto upgrading ...") logs.Println("[API_NODE]auto upgrading ...")
this.setProgress("DATABASE", "正在升级数据库")
err = this.autoUpgrade() err = this.autoUpgrade()
if err != nil { if err != nil {
var errString = "auto upgrade failed: " + err.Error() var errString = "auto upgrade failed: " + err.Error()
@@ -114,6 +119,7 @@ func (this *APINode) Start() {
} }
// 自动设置数据库 // 自动设置数据库
this.setProgress("DATABASE", "正在设置数据库")
logs.Println("[API_NODE]setup database ...") logs.Println("[API_NODE]setup database ...")
err = this.setupDB() err = this.setupDB()
if err != nil { if err != nil {
@@ -127,6 +133,7 @@ func (this *APINode) Start() {
dbs.NotifyReady() dbs.NotifyReady()
// 读取配置 // 读取配置
this.setProgress("DATABASE", "加载API配置")
logs.Println("[API_NODE]reading api config ...") logs.Println("[API_NODE]reading api config ...")
config, err := configs.SharedAPIConfig() config, err := configs.SharedAPIConfig()
if err != nil { if err != nil {
@@ -166,9 +173,11 @@ func (this *APINode) Start() {
}) })
// 访问日志存储管理器 // 访问日志存储管理器
this.setProgress("ACCESS_LOG_STORAGES", "正在启动访问日志存储器")
this.startAccessLogStorages() this.startAccessLogStorages()
// 监听RPC服务 // 监听RPC服务
this.setProgress("LISTEN_PORT", "正在启动监听端口")
remotelogs.Println("API_NODE", "starting RPC server ...") remotelogs.Println("API_NODE", "starting RPC server ...")
var isListening = this.listenPorts(apiNode) var isListening = this.listenPorts(apiNode)
@@ -182,6 +191,7 @@ func (this *APINode) Start() {
// 结束启动 // 结束启动
this.isStarting = false this.isStarting = false
this.progress = nil
// 保持进程 // 保持进程
select {} select {}
@@ -599,24 +609,24 @@ func (this *APINode) listenSock() error {
goman.New(func() { goman.New(func() {
this.sock.OnCommand(func(cmd *gosock.Command) { this.sock.OnCommand(func(cmd *gosock.Command) {
switch cmd.Code { switch cmd.Code {
case "pid": case "pid": // 查询PID
_ = cmd.Reply(&gosock.Command{ _ = cmd.Reply(&gosock.Command{
Code: "pid", Code: "pid",
Params: map[string]interface{}{ Params: map[string]any{
"pid": os.Getpid(), "pid": os.Getpid(),
}, },
}) })
case "info": case "info": // 进程相关信息
exePath, _ := os.Executable() exePath, _ := os.Executable()
_ = cmd.Reply(&gosock.Command{ _ = cmd.Reply(&gosock.Command{
Code: "info", Code: "info",
Params: map[string]interface{}{ Params: map[string]any{
"pid": os.Getpid(), "pid": os.Getpid(),
"version": teaconst.Version, "version": teaconst.Version,
"path": exePath, "path": exePath,
}, },
}) })
case "stop": case "stop": // 停止
_ = cmd.ReplyOk() _ = cmd.ReplyOk()
// 退出主进程 // 退出主进程
@@ -625,8 +635,9 @@ func (this *APINode) listenSock() error {
case "starting": // 是否正在启动 case "starting": // 是否正在启动
_ = cmd.Reply(&gosock.Command{ _ = cmd.Reply(&gosock.Command{
Code: "starting", Code: "starting",
Params: map[string]interface{}{ Params: map[string]any{
"isStarting": this.isStarting, "isStarting": this.isStarting,
"progress": this.progress,
}, },
}) })
case "goman": case "goman":
@@ -655,35 +666,35 @@ func (this *APINode) listenSock() error {
}) })
_ = cmd.Reply(&gosock.Command{ _ = cmd.Reply(&gosock.Command{
Params: map[string]interface{}{ Params: map[string]any{
"total": runtime.NumGoroutine(), "total": runtime.NumGoroutine(),
"result": result, "result": result,
}, },
}) })
case "debug": case "debug": // 进入|取消调试模式
teaconst.Debug = !teaconst.Debug teaconst.Debug = !teaconst.Debug
_ = cmd.Reply(&gosock.Command{ _ = cmd.Reply(&gosock.Command{
Params: map[string]interface{}{"debug": teaconst.Debug}, Params: map[string]any{"debug": teaconst.Debug},
}) })
case "db.stmt.prepare": case "db.stmt.prepare": // 显示prepared的语句
dbs.ShowPreparedStatements = !dbs.ShowPreparedStatements dbs.ShowPreparedStatements = !dbs.ShowPreparedStatements
_ = cmd.Reply(&gosock.Command{ _ = cmd.Reply(&gosock.Command{
Params: map[string]interface{}{"isOn": dbs.ShowPreparedStatements}, Params: map[string]any{"isOn": dbs.ShowPreparedStatements},
}) })
case "db.stmt.count": case "db.stmt.count": // 查询prepared语句数量
db, _ := dbs.Default() db, _ := dbs.Default()
if db != nil { if db != nil {
_ = cmd.Reply(&gosock.Command{ _ = cmd.Reply(&gosock.Command{
Params: map[string]interface{}{"count": db.StmtManager().Len()}, Params: map[string]any{"count": db.StmtManager().Len()},
}) })
} else { } else {
_ = cmd.Reply(&gosock.Command{ _ = cmd.Reply(&gosock.Command{
Params: map[string]interface{}{"count": 0}, Params: map[string]any{"count": 0},
}) })
} }
case "instance": case "instance": // 获取实例代号
_ = cmd.Reply(&gosock.Command{ _ = cmd.Reply(&gosock.Command{
Params: map[string]interface{}{ Params: map[string]any{
"code": teaconst.InstanceCode, "code": teaconst.InstanceCode,
}, },
}) })
@@ -705,7 +716,7 @@ func (this *APINode) listenSock() error {
} }
// 服务过滤器 // 服务过滤器
func (this *APINode) unaryInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) { func (this *APINode) unaryInterceptor(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) {
if teaconst.Debug { if teaconst.Debug {
var before = time.Now() var before = time.Now()
var traceCtx = rpc.NewContext(ctx) var traceCtx = rpc.NewContext(ctx)
@@ -792,3 +803,11 @@ func (this *APINode) saveIssues() {
_ = os.WriteFile(this.issuesFile, issuesJSON, 0666) _ = os.WriteFile(this.issuesFile, issuesJSON, 0666)
} }
} }
// 设置启动进度
func (this *APINode) setProgress(name, description string) {
this.progress = &utils.Progress{
Name: name,
Description: description,
}
}

View File

@@ -0,0 +1,9 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package utils
// Progress 进度表示
type Progress struct {
Name string `json:"name"`
Description string `json:"description"`
}