Skip to content

Commit

Permalink
Compatible with PIPE directory names
Browse files Browse the repository at this point in the history
  • Loading branch information
whiteCcinn committed May 24, 2021
1 parent f3bcf0e commit 776ee01
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 13 deletions.
4 changes: 2 additions & 2 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
36 changes: 27 additions & 9 deletions daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import (
"time"
)

const (
defaultChroot = "./"
)

var defaultOption = &options{
exit: true,
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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++
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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...)
Expand Down
1 change: 1 addition & 0 deletions example/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func main() {

ctx := context.Background()
dctx := daemon.Context{
//Chroot: "./pipe",
ProcAttr: syscall.SysProcAttr{},
//Logger: os.Stdout,
Logger: stdout,
Expand Down
5 changes: 4 additions & 1 deletion example/named-pipe-ipc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Empty file added example/pipe/.gitkeep
Empty file.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

0 comments on commit 776ee01

Please sign in to comment.