-
Notifications
You must be signed in to change notification settings - Fork 0
/
hot-reload.go
130 lines (117 loc) · 2.66 KB
/
hot-reload.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"fmt"
"bufio"
"os"
"log"
"strconv"
"syscall"
"strings"
"github.com/pborman/getopt/v2"
"github.com/fsnotify/fsnotify"
)
func readPidFile(pidFile string) int {
f, err := os.Open(pidFile)
defer f.Close()
if err != nil {
log.Println("WARNING: could not open pid file")
return -1
}
r := bufio.NewReader(f)
line, _ := r.ReadString('\n')
s := strings.TrimSuffix(line, "\n")
pid, err := strconv.Atoi(s)
if err != nil {
log.Println("WARNING: invalid pid file")
return -1
}
return pid
}
func MapPidArgs(pidStrings []string) []int {
var err error
pids := make([]int, len(pidStrings))
for i, pidString := range pidStrings {
pids[i], err = strconv.Atoi(pidString)
if err != nil {
panic(err)
}
}
return pids
}
func notifyProcess(pid int) {
p, err := os.FindProcess(pid)
if err != nil {
panic(err)
}
p.Signal(syscall.SIGHUP)
}
func notifyPids(pids []int) {
for _, pid := range pids {
notifyProcess(pid)
}
}
func notifyPidFiles(pidfiles []string) {
for _, pidFile := range pidfiles {
pid := readPidFile(pidFile)
notifyProcess(pid)
}
}
func main() {
optWatch := getopt.ListLong("watch", 'w', "", "Directory or file to watch")
optPid := getopt.ListLong("pid", 'p', "", "A pid to send a SIGHUP when watched files change")
optPidFile := getopt.ListLong("pidfile", 'f', "", "A pid file to take the pid from")
optHelp := getopt.BoolLong("help", 0, "Help")
getopt.Parse()
if *optHelp {
getopt.Usage()
os.Exit(0)
}
if len(*optWatch) == 0 {
fmt.Printf("Error: must watch at least one item\n")
getopt.Usage()
os.Exit(1)
}
if len(*optPid) == 0 && len(*optPidFile) == 0 {
fmt.Printf("Error: must include at least one of --pid or --pidfile\n")
getopt.Usage()
os.Exit(1)
}
pids := MapPidArgs(*optPid)
watchNotify(*optWatch, pids, *optPidFile)
}
func watchNotify(watches []string, pids []int, pidfiles []string) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
done := make(chan bool)
go func() {
for {
select {
case _, ok := <-watcher.Events:
if !ok {
return
}
notifyPids(pids)
notifyPidFiles(pidfiles)
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("error:", err)
}
}
}()
for i, watch := range watches {
fmt.Printf("watchlist item [%d] %s\n", i, watch)
watcher.Add(watch)
}
for i, pidFile := range pidfiles {
fmt.Printf("pidFile item [%d] %s\n", i, pidFile)
}
if err != nil {
log.Fatal(err)
}
<-done
}