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

@ -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) != "" {