未验证 提交 bcb13a5c 编写于 作者: C chris-sun-star 提交者: GitHub

Merge pull request #23 from chris-sun-star/optimize_startup

fix #22 quit when start http server failed
...@@ -16,13 +16,16 @@ import ( ...@@ -16,13 +16,16 @@ import (
"context" "context"
"net/http" "net/http"
"sync" "sync"
"time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/pkg/errors" "github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/oceanbase/obagent/api/route" "github.com/oceanbase/obagent/api/route"
"github.com/oceanbase/obagent/api/web" "github.com/oceanbase/obagent/api/web"
"github.com/oceanbase/obagent/config" "github.com/oceanbase/obagent/config"
"github.com/oceanbase/obagent/utils"
) )
var monitorAgentServer *MonitorAgentServer var monitorAgentServer *MonitorAgentServer
...@@ -38,8 +41,8 @@ type MonitorAgentServer struct { ...@@ -38,8 +41,8 @@ type MonitorAgentServer struct {
Server *web.HttpServer Server *web.HttpServer
// server of pprof // server of pprof
AdminServer *web.HttpServer AdminServer *web.HttpServer
// two servers concurrent waitGroup // server status map
wg *sync.WaitGroup serverStatusMap sync.Map
} }
// NewMonitorAgentServer init monagent server: init configs and logger, register routers // NewMonitorAgentServer init monagent server: init configs and logger, register routers
...@@ -60,7 +63,6 @@ func NewMonitorAgentServer(conf *config.MonitorAgentConfig) *MonitorAgentServer ...@@ -60,7 +63,6 @@ func NewMonitorAgentServer(conf *config.MonitorAgentConfig) *MonitorAgentServer
Server: &http.Server{}, Server: &http.Server{},
Address: conf.Server.AdminAddress, Address: conf.Server.AdminAddress,
}, },
wg: &sync.WaitGroup{},
} }
monitorAgentServer = monagentServer monitorAgentServer = monagentServer
return monitorAgentServer return monitorAgentServer
...@@ -68,21 +70,37 @@ func NewMonitorAgentServer(conf *config.MonitorAgentConfig) *MonitorAgentServer ...@@ -68,21 +70,37 @@ func NewMonitorAgentServer(conf *config.MonitorAgentConfig) *MonitorAgentServer
// Run start mongagent servers: admin server, monitor server // Run start mongagent servers: admin server, monitor server
func (server *MonitorAgentServer) Run() error { func (server *MonitorAgentServer) Run() error {
server.wg.Add(1) // check port available before start server
go func() { go func() {
defer server.wg.Done() defer server.serverStatusMap.Store("adminServer", false)
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
server.AdminServer.Cancel = cancel server.AdminServer.Cancel = cancel
server.AdminServer.Run(ctx) server.AdminServer.Run(ctx)
log.Info("start admin server")
}() }()
server.wg.Add(1)
go func() { go func() {
defer server.wg.Done() defer server.serverStatusMap.Store("server", false)
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
server.Server.Cancel = cancel server.Server.Cancel = cancel
server.Server.Run(ctx) server.Server.Run(ctx)
log.Info("start server")
}() }()
server.wg.Wait()
for {
adminServerStatus, _ := server.serverStatusMap.LoadOrStore("adminServer", true)
serverStatus, _ := server.serverStatusMap.LoadOrStore("server", true)
adminServerOk, convertAdminServerOk := utils.ConvertToBool(adminServerStatus)
serverOk, convertServerOk := utils.ConvertToBool(serverStatus)
if !(convertAdminServerOk && convertServerOk) {
return errors.New("check monagent server status failed")
}
if !(adminServerOk && serverOk) {
log.Infof("server status ok: %v", serverOk)
log.Infof("admin server status ok: %v", adminServerOk)
return errors.New("start monagent server failed")
}
time.Sleep(time.Second * 1)
}
return nil return nil
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册