-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaemon.go
74 lines (59 loc) · 1.21 KB
/
daemon.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package starter
import (
"errors"
"log"
"os"
"syscall"
"github.com/sevlyar/go-daemon"
)
var ErrProcessNotExists = errors.New("process not exists")
type daemonWrap struct {
pid int
dmx *daemon.Context
dmp *os.Process
}
func newDaemon(pid, log string) *daemonWrap {
dmx := &daemon.Context{
PidFileName: pid,
LogFileName: log,
}
dmp, err := dmx.Search()
if dmp != nil {
err = dmp.Signal(syscall.Signal(0))
}
run := 0
if err == nil && dmp != nil {
run = dmp.Pid
}
return &daemonWrap{run, dmx, dmp}
}
func (s *daemonWrap) runs() int {
return s.pid
}
func (s *daemonWrap) start() (bool, error) {
if child, err := s.dmx.Reborn(); err != nil {
return false, err
} else if child != nil {
log.Printf("[starter] process started, pid = %d\n", child.Pid)
return true, nil
} else {
return false, nil
}
}
func (s *daemonWrap) free() {
if err := s.dmx.Release(); err != nil {
log.Printf("[starter] daemon release failed: %s\n", err.Error())
}
}
func (s *daemonWrap) reload() error {
if s.pid > 0 {
return s.dmp.Signal(syscall.SIGHUP)
}
return ErrProcessNotExists
}
func (s *daemonWrap) shutdown() error {
if s.pid > 0 {
return s.dmp.Signal(syscall.SIGTERM)
}
return ErrProcessNotExists
}