forked from ivpusic/rerun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher.go
150 lines (125 loc) · 3.21 KB
/
watcher.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
package main
import (
"os"
"path/filepath"
"strings"
"time"
"gopkg.in/fsnotify.v1"
)
type watcher struct {
pm *processManager
queue chan fsnotify.Event
exit chan bool
}
// if we got relevant file event, and file extension is supported, and we didn't ignore changed file
// then we can confirm this file is important for us, and we should do required actions on it
func (w *watcher) isEventImportant(ev fsnotify.Event) bool {
file := ev.Name
// if change file is actually build file, than skip it
if strings.HasSuffix(file, w.pm.conf.build) {
return false
}
importantEvent := ev.Op == fsnotify.Write || ev.Op == fsnotify.Rename || ev.Op == fsnotify.Remove || (w.pm.conf.Attrib && ev.Op == fsnotify.Chmod)
if !importantEvent {
return false
}
supportedFile := false
for _, suffix := range w.pm.conf.Suffixes {
if strings.HasSuffix(file, suffix) {
supportedFile = true
break
}
}
if !supportedFile {
logger.Debugf("File %s changed but file extension is not enabled, so skip it!", file)
return false
}
abs, err := filepath.Abs(file)
if err != nil {
logger.Errorf("Error while converting file %s to absolute path! %s", file, err.Error())
return false
}
if contains(w.pm.conf.Ignore, abs) {
logger.Debugf("File %s changed but it is in ignore list, so skip it!", file)
return false
}
// if we got rename event, but file exist, we should ignore event
if ev.Op == fsnotify.Rename {
if _, err := os.Stat(file); err == nil {
return false
}
}
return true
}
// we get many events when file changes,
// so wait a little bit to wait for clear situation
func (w *watcher) throttleEvents() {
time.Sleep(50 * time.Millisecond)
for {
select {
case <-w.queue:
default:
return
}
}
}
// if event is relevant, reload app
func (w *watcher) processFileEvents() {
for {
select {
case ev, ok := <-w.queue:
if !ok {
return
} else {
if w.isEventImportant(ev) {
w.throttleEvents()
logger.Debugf("reloading due to change in file: %s", ev.Name)
logger.Info("change detected, reloading...")
w.pm.stop()
w.pm.run()
}
}
}
}
}
// start watching changes on files
func (w *watcher) start() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
logger.Panicf("Error while trying to watch files! %s", err.Error())
}
defer watcher.Close()
w.exit = make(chan bool)
w.queue = make(chan fsnotify.Event, 100)
go w.processFileEvents()
go func() {
for {
select {
case ev := <-watcher.Events:
w.queue <- ev
case err := <-watcher.Errors:
logger.Errorf("error generated by watcher: %q", err.Error())
w.exit <- true
}
}
}()
root, err := filepath.Abs(".")
if err != nil {
logger.Panicf("Error while looking for absolute application path! %s", err.Error())
}
err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if contains(w.pm.conf.Ignore, path) {
logger.Debugf("Ignoring %s\n", path)
return filepath.SkipDir
} else if info.IsDir() && !contains(w.pm.conf.Ignore, path) {
return watcher.Add(path)
}
return nil
})
if err != nil {
logger.Panicf("Error while reading list of files to listen! %s", err.Error())
}
w.pm.run()
logger.Info("rerun started. watching for changes...")
<-w.exit
}