This commit is contained in:
crusader 2018-03-22 21:38:00 +09:00
parent ba6bb15295
commit 1e7be8bc07

View File

@ -5,6 +5,7 @@ import (
"io/ioutil" "io/ioutil"
"net" "net"
"os" "os"
"path/filepath"
"strconv" "strconv"
"git.loafle.net/commons_go/logging" "git.loafle.net/commons_go/logging"
@ -12,8 +13,14 @@ import (
) )
func NewServerHandler(pidPath string, serverName string, socketHandler SocketHandler) ServerHandler { func NewServerHandler(pidPath string, serverName string, socketHandler SocketHandler) ServerHandler {
pidPathABS, err := filepath.Abs(pidPath)
if nil != err {
logging.Logger().Panicf("Container: pid file path[%s] is not valid %v", pidPath, err)
}
sh := &ServerHandlers{ sh := &ServerHandlers{
pidPath: pidPath, pidPath: pidPath,
pidPathABS: pidPathABS,
} }
sh.Name = serverName sh.Name = serverName
@ -25,6 +32,7 @@ type ServerHandlers struct {
server.ServerHandlers server.ServerHandlers
pidPath string pidPath string
pidPathABS string
port int port int
} }
@ -52,19 +60,19 @@ func (sh *ServerHandlers) Listen(serverCTX server.ServerContext) (net.Listener,
func (sh *ServerHandlers) OnStart(serverCTX server.ServerContext) { func (sh *ServerHandlers) OnStart(serverCTX server.ServerContext) {
sh.ServerHandlers.OnStart(serverCTX) sh.ServerHandlers.OnStart(serverCTX)
if _, err := os.Stat(sh.pidPath); os.IsExist(err) { if _, err := os.Stat(sh.pidPathABS); os.IsExist(err) {
if err := os.Remove(sh.pidPath); nil != err { if err := os.Remove(sh.pidPathABS); nil != err {
logging.Logger().Errorf("Container: Removing pid file has been failed [%v]", err) logging.Logger().Errorf("Container: Removing pid file has been failed [%v]", err)
} }
} }
s := strconv.FormatInt(int64(sh.port), 10) s := strconv.FormatInt(int64(sh.port), 10)
ioutil.WriteFile(sh.pidPath, []byte(s), os.ModePerm) ioutil.WriteFile(sh.pidPathABS, []byte(s), os.ModePerm)
} }
func (sh *ServerHandlers) OnStop(serverCTX server.ServerContext) { func (sh *ServerHandlers) OnStop(serverCTX server.ServerContext) {
if _, err := os.Stat(sh.pidPath); os.IsExist(err) { if _, err := os.Stat(sh.pidPathABS); os.IsExist(err) {
if err := os.Remove(sh.pidPath); nil != err { if err := os.Remove(sh.pidPathABS); nil != err {
logging.Logger().Errorf("Container: Removing pid file has been failed [%v]", err) logging.Logger().Errorf("Container: Removing pid file has been failed [%v]", err)
} }
} }