This commit is contained in:
lwl0608 2024-05-17 16:13:50 +08:00
parent 99f71d400b
commit b2cc85ce8a
3 changed files with 24 additions and 121 deletions

View File

@ -1,13 +1,10 @@
package main package main
import ( import (
"context"
"fmt" "fmt"
"net" "net"
"net/http" "net/http"
"os" "os"
"os/exec"
"os/signal"
"runtime" "runtime"
"service/api/consts" "service/api/consts"
"service/app/mix/conf" "service/app/mix/conf"
@ -21,6 +18,7 @@ import (
videomoderation "service/library/contentaudit/video_moderation" videomoderation "service/library/contentaudit/video_moderation"
"service/library/dingtalk" "service/library/dingtalk"
"service/library/httpengine" "service/library/httpengine"
"service/library/httpserver"
"service/library/logger" "service/library/logger"
"service/library/middleware" "service/library/middleware"
"service/library/redis" "service/library/redis"
@ -28,7 +26,6 @@ import (
"service/library/sms" "service/library/sms"
"service/library/validator" "service/library/validator"
"strings" "strings"
"syscall"
"time" "time"
) )
@ -134,43 +131,7 @@ func main() {
Handler: router, Handler: router,
} }
go func() { httpserver.StartHttpServer(srv, cfg.DefaultConfig, ip, port)
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.Fatal("listen: %v", err)
return
}
}()
// 注册服务
go func() {
err := registerSD(ip, port)
if err != nil {
logger.Fatal("registerSD: %v", err)
return
}
setServerStatusFD(ServerStatusFDRun)
}()
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
if err := servicediscovery.DeRegister(ip, port); err != nil {
logger.Fatal("DeRegister fail: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
if err := srv.Shutdown(ctx); err != nil {
logger.Fatal("Server Shutdown:", err)
}
defer cancel()
select {
case <-ctx.Done():
logger.Info("timeout of 1 seconds.")
}
logger.Info("Server exited")
setServerStatusFD(ServerStatusFDStop)
} }
func PrintAndExit(msg string) { func PrintAndExit(msg string) {
@ -203,47 +164,3 @@ func GetIp() string {
} }
return retIp return retIp
} }
func registerSD(ip string, port int) error {
// 要等服务起来后再注册到consul否则会有问题
// 方法:调用 healthcheck
idx := 0
timer := time.NewTimer(time.Second * 5)
timerRetry := time.NewTicker(time.Second * 1)
for {
select {
case <-timer.C:
return fmt.Errorf("registerSD fail, ip: %v, port: %v", ip, port)
case <-timerRetry.C:
idx += 1
resp, err := http.Get(fmt.Sprintf("http://%s:%v/healthcheck", ip, port))
if err != nil {
logger.Error("healthcheck fail, err: %v", err)
continue
}
if resp.StatusCode == http.StatusOK {
if err := servicediscovery.Register(ip, port); err != nil {
logger.Error("registerSD fail, ip: %v, port: %v, err: %v", ip, port, err)
continue
}
return nil
}
}
}
}
func setServerStatusFD(status string) {
serverStatusFdPath := "/app/SERVER_STATUS_FD"
cmd := exec.Command("sh", "-c", fmt.Sprintf("echo %s > %s", status, serverStatusFdPath))
err := cmd.Run()
if err != nil {
logger.Info("run cmd fail, p: %v, err: %v", serverStatusFdPath, err)
return
}
logger.Info("SetServerStatusFD success: %v", status)
}
const (
ServerStatusFDRun = "RUN" // 服务运行中
ServerStatusFDStop = "STOP" // 服务停止
)

View File

@ -26,7 +26,6 @@ func StartHttpServer(srv *http.Server, cfg *configcenter.DefaultConfig, ip strin
logger.Fatal("listen: %v", err) logger.Fatal("listen: %v", err)
return return
} }
}() }()
// 注册服务 // 注册服务
@ -39,44 +38,32 @@ func StartHttpServer(srv *http.Server, cfg *configcenter.DefaultConfig, ip strin
setServerStatusFD(ServerStatusFDRun) setServerStatusFD(ServerStatusFDRun)
}() }()
done := make(chan int) quit := make(chan os.Signal, 1)
go func() { signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
quit := make(chan os.Signal, 1) <-quit
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) logger.Info("Shutdown Server ...")
for { for i := 0; i < 10; i++ {
select { logger.Info("__Shutdown sleep %v", i+1)
case <-quit: time.Sleep(time.Second)
logger.Info("Shutdown Server ...") }
for i := 0; i < 10; i++ {
logger.Info("__Shutdown sleep %v", i+1)
time.Sleep(time.Second)
}
if err := servicediscovery.DeRegister(ip, port); err != nil { if err := servicediscovery.DeRegister(ip, port); err != nil {
logger.Fatal("DeRegister fail: %v", err) logger.Fatal("DeRegister fail: %v", err)
} }
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
if err := srv.Shutdown(ctx); err != nil { if err := srv.Shutdown(ctx); err != nil {
logger.Fatal("Server Shutdown:", err) logger.Fatal("Server Shutdown:", err)
} }
defer cancel()
select { select {
case <-ctx.Done(): case <-ctx.Done():
logger.Info("timeout of 1 seconds.") logger.Info("timeout of 1 seconds.")
} }
logger.Info("Server exited") logger.Info("Server exited")
setServerStatusFD(ServerStatusFDStop) setServerStatusFD(ServerStatusFDStop)
cancel()
done <- 1
return
}
}
}()
<-done
logger.Info("Server done")
} }
func registerSD(ip string, port int) error { func registerSD(ip string, port int) error {

View File

@ -816,7 +816,6 @@ func (c *Client) closePool() {
go func() { go func() {
<-ch <-ch
c.pool.Close() c.pool.Close()
os.Exit(0)
}() }()
} }