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"
"net"
"os"
"path/filepath"
"strconv"
"git.loafle.net/commons_go/logging"
@ -12,8 +13,14 @@ import (
)
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{
pidPath: pidPath,
pidPath: pidPath,
pidPathABS: pidPathABS,
}
sh.Name = serverName
@ -24,8 +31,9 @@ func NewServerHandler(pidPath string, serverName string, socketHandler SocketHan
type ServerHandlers struct {
server.ServerHandlers
pidPath string
port int
pidPath string
pidPathABS string
port int
}
func (sh *ServerHandlers) Init(serverCTX server.ServerContext) error {
@ -52,19 +60,19 @@ func (sh *ServerHandlers) Listen(serverCTX server.ServerContext) (net.Listener,
func (sh *ServerHandlers) OnStart(serverCTX server.ServerContext) {
sh.ServerHandlers.OnStart(serverCTX)
if _, err := os.Stat(sh.pidPath); os.IsExist(err) {
if err := os.Remove(sh.pidPath); nil != err {
if _, err := os.Stat(sh.pidPathABS); os.IsExist(err) {
if err := os.Remove(sh.pidPathABS); nil != err {
logging.Logger().Errorf("Container: Removing pid file has been failed [%v]", err)
}
}
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) {
if _, err := os.Stat(sh.pidPath); os.IsExist(err) {
if err := os.Remove(sh.pidPath); nil != err {
if _, err := os.Stat(sh.pidPathABS); os.IsExist(err) {
if err := os.Remove(sh.pidPathABS); nil != err {
logging.Logger().Errorf("Container: Removing pid file has been failed [%v]", err)
}
}