From 776ee01914c06cb98a10855cc4877e46dc661c71 Mon Sep 17 00:00:00 2001 From: whiteCcinn <471113744@qq.com> Date: Mon, 24 May 2021 18:11:05 +0800 Subject: [PATCH] Compatible with PIPE directory names --- api.go | 4 ++-- daemon.go | 36 +++++++++++++++++++++++++++--------- example/daemon.go | 1 + example/named-pipe-ipc.go | 5 ++++- example/pipe/.gitkeep | 0 go.mod | 2 +- 6 files changed, 35 insertions(+), 13 deletions(-) create mode 100644 example/pipe/.gitkeep diff --git a/api.go b/api.go index 67ec480..17d3fbd 100644 --- a/api.go +++ b/api.go @@ -2,6 +2,6 @@ package daemon import "fmt" -func (dctx *Context) Information() string{ - return fmt.Sprintf("count:%d/%d; errNum:%d/%d", dctx.Count, dctx.MaxCount, dctx.ErrNum, dctx.MaxError) +func (dctx *Context) Information() string { + return fmt.Sprintf("[supervisor-pid: %d] [pid: %d] [count: %d/%d] [errNum: %d/%d]", dctx.Pid, dctx.CPid, dctx.Count-1, dctx.MaxCount, dctx.ErrNum, dctx.MaxError) } diff --git a/daemon.go b/daemon.go index 47e2dbe..16b7562 100644 --- a/daemon.go +++ b/daemon.go @@ -16,6 +16,10 @@ import ( "time" ) +const ( + defaultChroot = "./" +) + var defaultOption = &options{ exit: true, } @@ -45,9 +49,7 @@ const EnvName = "_DAEMON" var runIdx int = 0 type Context struct { - //PidFileName string - //PidFilePerm os.FileMode - + Chroot string ProcAttr syscall.SysProcAttr Logger io.Writer @@ -106,6 +108,9 @@ func attachContext(dctx *Context) (isChild bool) { env = append(env, fmt.Sprintf("%s=%d", EnvName, runIdx)) dctx.Env = env dctx.Args = os.Args + if len(dctx.Chroot) == 0 { + dctx.Chroot = defaultChroot + } return false } @@ -190,10 +195,12 @@ func (dctx *Context) Run(ctx context.Context) error { //daemon information if dctx.ErrNum > dctx.MaxError { dctx.log("[supervisor(%d)] [child process fails too many times]\n", dctx.Pid) + dctx.clean() os.Exit(1) } if dctx.MaxCount > 0 && dctx.Count > dctx.MaxCount { dctx.log("[supervisor(%d)] [reboot too many times quit]\n", dctx.Pid) + dctx.clean() os.Exit(0) } dctx.Count++ @@ -282,24 +289,27 @@ func (dctx *Context) Run(ctx context.Context) error { // named-pipe-ipc dctx.namedPipeOnce.Do(func() { - dctx.namedPipeCtx, err = named_pipe_ipc.NewContext(context.Background(), "./", named_pipe_ipc.S) + dctx.namedPipeCtx, err = named_pipe_ipc.NewContext(context.Background(), dctx.Chroot, named_pipe_ipc.S) if err != nil { log.Fatal(err) } dctx.log("[supervisor(%d)] [named-pipe-ipc] [listen]\n", dctx.Pid) + SetSigHandler(func(sig os.Signal) (err error) { + dctx.clean() + return + }, syscall.SIGINT, syscall.SIGTERM) + go ServeSignals() + go func() { go func() { for { msg, err := dctx.namedPipeCtx.Recv(false) - if err != nil && err.Error() != named_pipe_ipc.NoMessageMessage { + if err != nil && (err.Error() != named_pipe_ipc.NoMessageMessage && err.Error() != named_pipe_ipc.PipeClosedMessage) { dctx.log("[supervisor(%d)] [named-pipe-ipc] [err:%v]\n", dctx.Pid, err) os.Exit(4) } if msg == nil { - if ctx.Err() != nil { - return - } time.Sleep(500 * time.Millisecond) continue } @@ -312,7 +322,7 @@ func (dctx *Context) Run(ctx context.Context) error { if epm.Api == PrintInformation { ret := dctx.Information() - _, err = dctx.namedPipeCtx.Send(named_pipe_ipc.Message(ret + "\n")) + _, err = dctx.namedPipeCtx.Send(named_pipe_ipc.Message(ret)) if err != nil { dctx.log("[supervisor(%d)] [named-pipe-ipc] [send-error:%v]\n", dctx.Pid, err) } @@ -355,6 +365,14 @@ func (dctx *Context) Run(ctx context.Context) error { return nil } +func (dctx *Context) clean() { + if dctx.namedPipeCtx != nil { + if err := dctx.namedPipeCtx.Close(); err != nil { + dctx.log("[supervisor(%d)] [named-pipe-ipc] [close failed] [%v]\n", dctx.Pid, err) + } + } +} + // output log-message to Context.Logger func (dctx *Context) log(format string, args ...interface{}) { _, fe := fmt.Fprintf(dctx.Logger, format, args...) diff --git a/example/daemon.go b/example/daemon.go index c5aa69c..7c27516 100644 --- a/example/daemon.go +++ b/example/daemon.go @@ -18,6 +18,7 @@ func main() { ctx := context.Background() dctx := daemon.Context{ + //Chroot: "./pipe", ProcAttr: syscall.SysProcAttr{}, //Logger: os.Stdout, Logger: stdout, diff --git a/example/named-pipe-ipc.go b/example/named-pipe-ipc.go index c22331b..bb98b36 100644 --- a/example/named-pipe-ipc.go +++ b/example/named-pipe-ipc.go @@ -23,7 +23,10 @@ func main() { log.Fatal(err) } - _, _ = nctx.Send(named_pipe_ipc.Message(string(data))) + _, err = nctx.Send(data) + if err != nil { + log.Fatal(err) + } msg, err := nctx.Recv(true) if err != nil { log.Fatal(err) diff --git a/example/pipe/.gitkeep b/example/pipe/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/go.mod b/go.mod index dce85b9..7f9ec5d 100644 --- a/go.mod +++ b/go.mod @@ -4,5 +4,5 @@ go 1.15 require ( github.com/erikdubbelboer/gspt v0.0.0-20201015204752-6cb2489021da - github.com/whiteCcinn/named-pipe-ipc v0.0.3 + github.com/whiteCcinn/named-pipe-ipc v0.0.5 )