update logger
This commit is contained in:
parent
a3af539c91
commit
4e435d92d6
29
godaemon.go
29
godaemon.go
|
@ -24,10 +24,11 @@ const (
|
|||
type GoDaemon struct {
|
||||
pidFile string
|
||||
flag *string
|
||||
Running *exec.Cmd
|
||||
sigChan chan os.Signal
|
||||
doneChan chan int
|
||||
state string
|
||||
gologger.Logger
|
||||
Running *exec.Cmd
|
||||
|
||||
StartFn func(*GoDaemon)
|
||||
StopFn func(*GoDaemon)
|
||||
}
|
||||
|
@ -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) != "" {
|
||||
|
|
Loading…
Reference in New Issue