-
-
Notifications
You must be signed in to change notification settings - Fork 99
/
gowatch.go
399 lines (348 loc) · 8.36 KB
/
gowatch.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
path "path/filepath"
"regexp"
"sort"
"strings"
"time"
"github.com/fsnotify/fsnotify"
"github.com/mitchellh/go-ps"
"github.com/silenceper/log"
)
var (
cmd *exec.Cmd
eventTime = make(map[string]int64)
scheduleTime time.Time
)
// NewWatcher new watcher
func NewWatcher(paths []string, files []string) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Errorf(" Fail to create new Watcher[ %s ]\n", err)
os.Exit(2)
}
go func() {
for {
select {
case e := <-watcher.Events:
isbuild := true
// Skip ignored files
if shouldIgnoreFile(e.Name) {
continue
}
if !checkIfWatchExt(e.Name) {
continue
}
mt := getFileModTime(e.Name)
if t := eventTime[e.Name]; mt == t {
// log.Infof("[SKIP] # %s #\n", e.String())
isbuild = false
}
eventTime[e.Name] = mt
if isbuild {
go func() {
// Wait 1s before autobuild util there is no file change.
scheduleTime = time.Now().Add(1 * time.Second)
for {
time.Sleep(time.Until(scheduleTime))
if time.Now().After(scheduleTime) {
break
}
}
Autobuild(files)
}()
}
case err := <-watcher.Errors:
log.Errorf("%v", err)
log.Warnf(" %s\n", err.Error()) // No need to exit here
}
}
}()
log.Infof("Initializing watcher...\n")
for _, path := range paths {
log.Infof("Directory( %s )\n", path)
err = watcher.Add(path)
if err != nil {
log.Errorf("Fail to watch directory[ %s ]\n", err)
os.Exit(2)
}
}
}
// getFileModTime retuens unix timestamp of `os.File.ModTime` by given path.
func getFileModTime(path string) int64 {
path = strings.ReplaceAll(path, "\\", "/")
f, err := os.Open(path)
if err != nil {
log.Errorf("Fail to open file[ %s ]\n", err)
return time.Now().Unix()
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
log.Errorf("Fail to get file information[ %s ]\n", err)
return time.Now().Unix()
}
return fi.ModTime().Unix()
}
var building bool
// Autobuild auto build
//
//nolint:funlen
func Autobuild(files []string) {
if building {
log.Infof("still in building...\n")
return
}
building = true
defer func() {
building = false
}()
log.Infof("Start building...\n")
if err := os.Chdir(currpath); err != nil {
log.Errorf("Chdir Error: %+v\n", err)
return
}
for _, prevCmd := range cfg.PrevBuildCmds {
log.Infof("Run external cmd '%s'", prevCmd)
cmdArr := strings.Split(prevCmd, " ")
prevCmdExec := exec.Command(cmdArr[0])
prevCmdExec.Env = append(os.Environ(), cfg.Envs...)
prevCmdExec.Args = cmdArr
prevCmdExec.Stdout = os.Stdout
prevCmdExec.Stderr = os.Stderr
err := prevCmdExec.Run()
if err != nil {
panic(err)
}
}
cmdName := "go"
var err error
args := []string{"build"}
args = append(args, "-o", cfg.Output)
args = append(args, cfg.BuildArgs...)
if cfg.BuildTags != "" {
args = append(args, "-tags", cfg.BuildTags)
}
args = append(args, files...)
bcmd := exec.Command(cmdName, args...)
bcmd.Env = append(os.Environ(), "GOGC=off")
bcmd.Stdout = os.Stdout
bcmd.Stderr = os.Stderr
log.Infof("Build Args: %s %s", cmdName, strings.Join(args, " "))
err = bcmd.Run()
if err != nil {
log.Errorf("============== Build failed ===================\n")
log.Errorf("%+v\n", err)
return
}
log.Infof("Build was successful\n")
if !cfg.DisableRun {
if len(cfg.RunCmd) != 0 {
Restart(cfg.RunCmd, true)
} else {
Restart(cfg.Output, false)
}
}
}
// Kill kill main process and all its children
func Kill() {
defer func() {
if e := recover(); e != nil {
fmt.Println("Kill.recover -> ", e)
}
}()
if cmd != nil && cmd.Process != nil {
// err := cmd.Process.Kill()
err := killAllProcesses(cmd.Process.Pid)
if err != nil {
fmt.Println("Kill -> ", err)
}
}
}
// kill main process and all its children
func killAllProcesses(pid int) (err error) {
hasAllKilled := make(chan bool)
go func() {
pids, err := psTree(pid)
if err != nil {
log.Fatalf("getting all sub processes error: %v\n", err)
return
}
log.Debugf("main pid: %d", pid)
log.Debugf("pids: %+v", pids)
for _, subPid := range pids {
_ = killProcess(subPid)
}
waitForProcess(pid, hasAllKilled)
}()
// finally kill the main process
<-hasAllKilled
log.Debugf("killing MAIN process pid: %d", pid)
err = cmd.Process.Kill()
if err != nil {
return
}
log.Debugf("kill MAIN process succeed")
return
}
func killProcess(pid int) (err error) {
log.Debugf("killing process pid: %d", pid)
ps, err := os.FindProcess(pid)
if err != nil {
log.Errorf("find process %d error: %v\n", pid, err)
return
}
err = ps.Kill()
if err != nil {
log.Errorf("killing process %d error: %v\n", pid, err)
// retry
time.AfterFunc(2*time.Second, func() {
log.Debugf("retry killing process pid: %d", pid)
_ = killProcess(pid)
})
return
}
return
}
// implement pstree based on the cross-platform ps utility in go, go-ps
func psTree(rootPid int) (res []int, err error) {
pidOfInterest := map[int]struct{}{rootPid: {}}
pss, err := ps.Processes()
if err != nil {
fmt.Println("ERROR: ", err)
return
}
// we must sort the ps by ppid && pid first, otherwise we probably will miss some sub-processes
// of the root process during for-range searching
sort.Slice(pss, func(i, j int) bool {
ppidLess := pss[i].PPid() < pss[j].PPid()
pidLess := pss[i].PPid() == pss[j].PPid() && pss[i].Pid() < pss[j].Pid()
return ppidLess || pidLess
})
for _, ps := range pss {
ppid := ps.PPid()
if _, exists := pidOfInterest[ppid]; exists {
pidOfInterest[ps.Pid()] = struct{}{}
}
}
for pid := range pidOfInterest {
if pid != rootPid {
res = append(res, pid)
}
}
return
}
func waitForProcess(pid int, hasAllKilled chan bool) {
pids, _ := psTree(pid)
if len(pids) == 0 {
hasAllKilled <- true
return
}
log.Infof("still waiting for %d processes %+v to exit", len(pids), pids)
time.AfterFunc(time.Second, func() {
waitForProcess(pid, hasAllKilled)
})
}
// Restart restart app
func Restart(appname string, isCmd bool) {
// log.Debugf("kill running process")
Kill()
go Start(appname, isCmd)
}
// Start start app
func Start(appname string, isCmd bool) {
log.Infof("Restarting %s ...\n", appname)
if !strings.HasPrefix(appname, "./") && !isCmd {
appname = "./" + appname
}
cmd = exec.Command(appname)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Args = append([]string{appname}, cfg.CmdArgs...)
cmd.Env = append(os.Environ(), cfg.Envs...)
log.Infof("Run %s", strings.Join(cmd.Args, " "))
go func() {
_ = cmd.Run()
}()
log.Infof("%s is running...\n", appname)
started <- true
}
// Should ignore filenames generated by
// Emacs, Vim or SublimeText
func shouldIgnoreFile(filename string) bool {
for _, regex := range ignoredFilesRegExps {
r, err := regexp.Compile(regex)
if err != nil {
panic("Could not compile the regex: " + regex)
}
if r.MatchString(filename) {
return true
}
continue
}
return false
}
// checkIfWatchExt returns true if the name HasSuffix <watch_ext>.
func checkIfWatchExt(name string) bool {
for _, s := range cfg.WatchExts {
if strings.HasSuffix(name, s) {
return true
}
}
return false
}
func readAppDirectories(directory string, paths *[]string) {
fileInfos, err := ioutil.ReadDir(directory)
if err != nil {
return
}
useDirectory := false
for _, fileInfo := range fileInfos {
if strings.HasSuffix(fileInfo.Name(), "docs") {
continue
}
if strings.HasSuffix(fileInfo.Name(), "swagger") {
continue
}
if !cfg.VendorWatch && strings.HasSuffix(fileInfo.Name(), "vendor") {
continue
}
if isExcluded(path.Join(directory, fileInfo.Name())) {
continue
}
if fileInfo.IsDir() && fileInfo.Name()[0] != '.' {
readAppDirectories(directory+"/"+fileInfo.Name(), paths)
continue
}
if useDirectory {
continue
}
*paths = append(*paths, directory)
useDirectory = true
}
}
// If a file is excluded
func isExcluded(filePath string) bool {
for _, p := range cfg.ExcludedPaths {
absP, err := path.Abs(p)
if err != nil {
log.Errorf("err =%v", err)
log.Errorf("Can not get absolute path of [ %s ]\n", p)
continue
}
absFilePath, err := path.Abs(filePath)
if err != nil {
log.Errorf("Can not get absolute path of [ %s ]\n", filePath)
break
}
if strings.HasPrefix(absFilePath, absP) {
log.Infof("Excluding from watching [ %s ]\n", filePath)
return true
}
}
return false
}