This commit is contained in:
lwl0608 2024-05-15 00:34:23 +08:00
parent 2ae1c452b3
commit b3836120e2
1 changed files with 37 additions and 1 deletions

View File

@ -14,6 +14,11 @@ import (
"service/library/logger"
)
const (
ServerStatusFDRun = "RUN" // 服务运行中
ServerStatusFDStop = "STOP" // 服务停止
)
func StartHttpServer(srv *http.Server, cfg *configcenter.DefaultConfig, ip string, port int) {
quit := make(chan os.Signal)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
@ -29,9 +34,12 @@ func StartHttpServer(srv *http.Server, cfg *configcenter.DefaultConfig, ip strin
// 注册服务
go func() {
if err := registerSD(ip, port); err != nil {
err := registerSD(ip, port)
if err != nil {
quit <- syscall.SIGTERM
return
}
setServerStatusFD(ServerStatusFDRun)
}()
<-quit
@ -53,6 +61,7 @@ func StartHttpServer(srv *http.Server, cfg *configcenter.DefaultConfig, ip strin
}
logger.Info("Server exited")
setServerStatusFD(ServerStatusFDStop)
}
func registerSD(ip string, port int) error {
@ -82,3 +91,30 @@ func registerSD(ip string, port int) error {
}
}
}
func setServerStatusFD(status string) {
os.Setenv("SERVER_STATUS_FD", status)
fmt.Println(os.Getenv("SERVER_STATUS_FD"))
return
serverStatusFdPath := "/Users/erwin/SERVER_STATUS_FD"
file, err := os.OpenFile(serverStatusFdPath, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
logger.Info("OpenFile fail, p: %v, err: %v", serverStatusFdPath, err)
return
}
defer func() {
_ = file.Close()
}()
err = file.Truncate(0)
if err != nil {
logger.Info("file Truncate fail, p: %v, err: %v", serverStatusFdPath, err)
return
}
_, err = file.WriteString(status)
if err != nil {
logger.Info("file WriteString fail, p: %v, v: %v, err: %v", serverStatusFdPath, status, err)
return
}
logger.Info("SetServerStatusFD success: %v", status)
}