update logger

This commit is contained in:
kingecg 2023-12-10 18:36:16 +08:00
parent a3af539c91
commit 4e435d92d6
1 changed files with 20 additions and 21 deletions

View File

@ -24,10 +24,11 @@ const (
type GoDaemon struct { type GoDaemon struct {
pidFile string pidFile string
flag *string flag *string
Running *exec.Cmd
sigChan chan os.Signal sigChan chan os.Signal
doneChan chan int
state string state string
gologger.Logger
Running *exec.Cmd
StartFn func(*GoDaemon) StartFn func(*GoDaemon)
StopFn func(*GoDaemon) StopFn func(*GoDaemon)
} }
@ -68,28 +69,25 @@ func (g *GoDaemon) Start() {
fmt.Println("Daemon process not found") fmt.Println("Daemon process not found")
return return
} }
fmt.Println("Send signal:", p.Pid, sig) g.Debug("Send signal:", p.Pid, sig)
p.Signal(sig) p.Signal(sig)
} else if IsDaemon() { } else if IsDaemon() {
pid := os.Getpid() pid := os.Getpid()
os.WriteFile(g.pidFile, []byte(strconv.Itoa(pid)), 0644) os.WriteFile(g.pidFile, []byte(strconv.Itoa(pid)), 0644)
g.sigChan = make(chan os.Signal) g.sigChan = make(chan os.Signal)
g.doneChan = make(chan int)
signal.Notify(g.sigChan, syscall.SIGTERM, syscall.SIGHUP) signal.Notify(g.sigChan, syscall.SIGTERM, syscall.SIGHUP)
go g.serveSignal() go g.serveSignal()
l := gologger.GetLogger("daemon")
for { for {
if g.state == "running" {
l.Debug("Due to for loop ") g.Debug("Starting new task")
break
}
l.Debug("Starting task new")
g.Running = g.startTask() g.Running = g.startTask()
g.state = "running" g.state = "running"
g.Running.Process.Wait() g.Running.Process.Wait()
if g.state == "stopped" { if g.state == "stopped" {
g.Debug("daemon is stopped, exit now")
break break
} }
} }
@ -107,8 +105,8 @@ func (g *GoDaemon) serveSignal() {
} else { } else {
g.state = "restart" g.state = "restart"
} }
l := gologger.GetLogger("daemon")
l.Debug("Stop it") g.Debug("Stop it")
g.StopFn(g) g.StopFn(g)
} }
@ -119,8 +117,7 @@ func (g *GoDaemon) getDaemonProcess() *os.Process {
} }
p, err := os.FindProcess(pid) p, err := os.FindProcess(pid)
if err != nil { if err != nil {
l := gologger.GetLogger("default") g.Debug(err)
l.Debug(err)
} }
serr := p.Signal(syscall.Signal(0)) serr := p.Signal(syscall.Signal(0))
if serr != nil { if serr != nil {
@ -175,7 +172,9 @@ func IsDaemonTask() bool {
} }
func NewGoDaemon(start, stop func(*GoDaemon)) *GoDaemon { func NewGoDaemon(start, stop func(*GoDaemon)) *GoDaemon {
godaemon := &GoDaemon{} godaemon := &GoDaemon{
Logger: gologger.GetLogger("daemon"),
}
execName, _ := os.Executable() execName, _ := os.Executable()
if filepath.Ext(execName) != "" { if filepath.Ext(execName) != "" {