Merge branch 'dev-lwl/consul' into test

This commit is contained in:
lwl0608 2024-05-17 14:35:23 +08:00
commit 56bca2291c
1 changed files with 30 additions and 19 deletions

View File

@ -39,28 +39,39 @@ func StartHttpServer(srv *http.Server, cfg *configcenter.DefaultConfig, ip strin
setServerStatusFD(ServerStatusFDRun) setServerStatusFD(ServerStatusFDRun)
}() }()
quit := make(chan os.Signal) done := make(chan int)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) go func() {
<-quit quit := make(chan os.Signal)
logger.Info("Shutdown Server ...") signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
for {
select {
case <-quit:
logger.Info("Shutdown Server ...")
if err := servicediscovery.DeRegister(ip, port); err != nil {
logger.Fatal("DeRegister fail: %v", err)
}
if err := servicediscovery.DeRegister(ip, port); err != nil { ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
logger.Fatal("DeRegister fail: %v", err) if err := srv.Shutdown(ctx); err != nil {
} logger.Fatal("Server Shutdown:", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) select {
defer cancel() case <-ctx.Done():
if err := srv.Shutdown(ctx); err != nil { logger.Info("timeout of 1 seconds.")
logger.Fatal("Server Shutdown:", err) }
}
select { logger.Info("Server exited")
case <-ctx.Done(): setServerStatusFD(ServerStatusFDStop)
logger.Info("timeout of 1 seconds.") cancel()
} done <- 1
return
}
}
}()
logger.Info("Server exited") <-done
setServerStatusFD(ServerStatusFDStop) logger.Info("Server done")
} }
func registerSD(ip string, port int) error { func registerSD(ip string, port int) error {
@ -92,7 +103,7 @@ func registerSD(ip string, port int) error {
} }
func setServerStatusFD(status string) { func setServerStatusFD(status string) {
serverStatusFdPath := "/app/SERVER_STATUS_FD" serverStatusFdPath := "/Users/erwin/SERVER_STATUS_FD"
cmd := exec.Command("sh", "-c", fmt.Sprintf("echo %s > %s", status, serverStatusFdPath)) cmd := exec.Command("sh", "-c", fmt.Sprintf("echo %s > %s", status, serverStatusFdPath))
err := cmd.Run() err := cmd.Run()
if err != nil { if err != nil {