From 4e435d92d6fd5ca1713d8b521d6531d9dd2d73e8 Mon Sep 17 00:00:00 2001 From: kingecg Date: Sun, 10 Dec 2023 18:36:16 +0800 Subject: [PATCH] update logger --- godaemon.go | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/godaemon.go b/godaemon.go index 5e8ef46..93943bf 100644 --- a/godaemon.go +++ b/godaemon.go @@ -22,14 +22,15 @@ const ( ) type GoDaemon struct { - pidFile string - flag *string - Running *exec.Cmd - sigChan chan os.Signal - doneChan chan int - state string - StartFn func(*GoDaemon) - StopFn func(*GoDaemon) + pidFile string + flag *string + sigChan chan os.Signal + state string + gologger.Logger + Running *exec.Cmd + + StartFn func(*GoDaemon) + StopFn func(*GoDaemon) } func (g *GoDaemon) GetPid() int { @@ -68,28 +69,25 @@ func (g *GoDaemon) Start() { fmt.Println("Daemon process not found") return } - fmt.Println("Send signal:", p.Pid, sig) + g.Debug("Send signal:", p.Pid, sig) p.Signal(sig) } else if IsDaemon() { pid := os.Getpid() os.WriteFile(g.pidFile, []byte(strconv.Itoa(pid)), 0644) g.sigChan = make(chan os.Signal) - g.doneChan = make(chan int) + signal.Notify(g.sigChan, syscall.SIGTERM, syscall.SIGHUP) go g.serveSignal() - l := gologger.GetLogger("daemon") for { - if g.state == "running" { - l.Debug("Due to for loop ") - break - } - l.Debug("Starting task new") + + g.Debug("Starting new task") g.Running = g.startTask() g.state = "running" g.Running.Process.Wait() if g.state == "stopped" { + g.Debug("daemon is stopped, exit now") break } } @@ -107,8 +105,8 @@ func (g *GoDaemon) serveSignal() { } else { g.state = "restart" } - l := gologger.GetLogger("daemon") - l.Debug("Stop it") + + g.Debug("Stop it") g.StopFn(g) } @@ -119,8 +117,7 @@ func (g *GoDaemon) getDaemonProcess() *os.Process { } p, err := os.FindProcess(pid) if err != nil { - l := gologger.GetLogger("default") - l.Debug(err) + g.Debug(err) } serr := p.Signal(syscall.Signal(0)) if serr != nil { @@ -175,7 +172,9 @@ func IsDaemonTask() bool { } func NewGoDaemon(start, stop func(*GoDaemon)) *GoDaemon { - godaemon := &GoDaemon{} + godaemon := &GoDaemon{ + Logger: gologger.GetLogger("daemon"), + } execName, _ := os.Executable() if filepath.Ext(execName) != "" {