forked from blastbao/again
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindows.go
111 lines (98 loc) · 2.18 KB
/
windows.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// +build windows
package again
import (
"fmt"
"io"
"log"
"os"
"os/signal"
"strings"
"syscall"
)
func (a *Again) Env() (m map[string]string, err error) {
var fds []string
var names []string
var fdNames []string
a.services.Range(func(k, value interface{}) bool {
s := value.(*Service)
names = append(names, s.Name)
fds = append(fds, fmt.Sprint(s.Descriptor))
fdNames = append(fdNames, s.FdName)
return true
})
if err != nil {
return
}
return map[string]string{
"GOAGAIN_FD": strings.Join(fds, ","),
"GOAGAIN_SERVICE_NAME": strings.Join(names, ","),
"GOAGAIN_NAME": strings.Join(fdNames, ","),
}, nil
}
// Kill process specified in the environment with the signal specified in the
// environment; default to SIGQUIT.
func Kill() error {
var (
pid int
sig syscall.Signal
)
_, err := fmt.Sscan(os.Getenv("GOAGAIN_PID"), &pid)
if io.EOF == err {
_, err = fmt.Sscan(os.Getenv("GOAGAIN_PPID"), &pid)
}
if nil != err {
return err
}
if _, err := fmt.Sscan(os.Getenv("GOAGAIN_SIGNAL"), &sig); nil != err {
sig = syscall.SIGQUIT
}
process, err := os.FindProcess(int(pid))
if nil != err {
return err
}
log.Println("sending signal", sig, "to process", pid)
return process.Signal(sig)
}
// Wait waits for signals
func Wait(a *Again) (syscall.Signal, error) {
ch := make(chan os.Signal, 2)
signal.Notify(
ch,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGQUIT,
syscall.SIGTERM,
)
for {
sig := <-ch
log.Println(sig.String())
switch sig {
// SIGHUP should reload configuration.
case syscall.SIGHUP:
if a.Hooks.OnSIGHUP != nil {
if err := a.Hooks.OnSIGHUP(a); err != nil {
log.Println("OnSIGHUP:", err)
}
}
// SIGINT should exit.
case syscall.SIGINT:
return syscall.SIGINT, nil
// SIGQUIT should exit gracefully.
case syscall.SIGQUIT:
if a.Hooks.OnSIGQUIT != nil {
if err := a.Hooks.OnSIGQUIT(a); err != nil {
log.Println("OnSIGQUIT:", err)
}
}
return syscall.SIGQUIT, nil
// SIGTERM should exit.
case syscall.SIGTERM:
if a.Hooks.OnSIGTERM != nil {
if err := a.Hooks.OnSIGHUP(a); err != nil {
log.Println("OnSIGTERM:", err)
}
}
return syscall.SIGTERM, nil
}
}
}