-
Notifications
You must be signed in to change notification settings - Fork 0
/
integration.go
185 lines (150 loc) · 4.41 KB
/
integration.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
package watch
import (
"log"
"os"
"strings"
"sync"
"time"
"github.com/smartystreets/goconvey/web/server/messaging"
)
type Watcher struct {
nap time.Duration
rootFolder string
folderDepth int
ignoredFolders map[string]struct{}
fileSystemState int64
paused bool
stopped bool
watchSuffixes []string
excludedDirs []string
input chan messaging.WatcherCommand
output chan messaging.Folders
lock sync.RWMutex
}
func NewWatcher(rootFolder string, folderDepth int, nap time.Duration,
input chan messaging.WatcherCommand, output chan messaging.Folders, watchSuffixes string, excludedDirs []string) *Watcher {
return &Watcher{
nap: nap,
rootFolder: rootFolder,
folderDepth: folderDepth,
input: input,
output: output,
watchSuffixes: strings.Split(watchSuffixes, ","),
excludedDirs: excludedDirs,
ignoredFolders: make(map[string]struct{}),
}
}
func (this *Watcher) Listen() {
for {
if this.stopped {
return
}
select {
case command := <-this.input:
this.respond(command)
default:
if !this.paused {
this.scan()
}
time.Sleep(nap)
}
}
}
func (this *Watcher) respond(command messaging.WatcherCommand) {
switch command.Instruction {
case messaging.WatcherAdjustRoot:
log.Println("Adjusting root...")
this.rootFolder = command.Details
this.execute()
case messaging.WatcherIgnore:
log.Println("Ignoring specified folders")
this.ignore(command.Details)
// Prevent a filesystem change due to the number of active folders changing
_, checksum := this.gather()
this.set(checksum)
case messaging.WatcherReinstate:
log.Println("Reinstating specified folders")
this.reinstate(command.Details)
// Prevent a filesystem change due to the number of active folders changing
_, checksum := this.gather()
this.set(checksum)
case messaging.WatcherPause:
log.Println("Pausing watcher...")
this.paused = true
case messaging.WatcherResume:
log.Println("Resuming watcher...")
this.paused = false
case messaging.WatcherExecute:
log.Println("Gathering folders for immediate execution...")
this.execute()
case messaging.WatcherStop:
log.Println("Stopping the watcher...")
close(this.output)
this.stopped = true
default:
log.Println("Unrecognized command from server:", command.Instruction)
}
}
func (this *Watcher) execute() {
folders, _ := this.gather()
this.sendToExecutor(folders)
}
func (this *Watcher) scan() {
folders, checksum := this.gather()
if checksum == this.fileSystemState {
return
}
log.Println("File system state modified, publishing current folders...", this.fileSystemState, checksum)
defer this.set(checksum)
this.sendToExecutor(folders)
}
func (this *Watcher) gather() (folders messaging.Folders, checksum int64) {
items := YieldFileSystemItems(this.rootFolder, this.excludedDirs)
folderItems, profileItems, goFileItems := Categorize(items, this.rootFolder, this.watchSuffixes)
for _, item := range profileItems {
// TODO: don't even bother if the item's size is over a few hundred bytes...
contents := ReadContents(item.Path)
item.ProfileDisabled, item.ProfileTags, item.ProfileArguments = ParseProfile(contents)
}
folders = CreateFolders(folderItems)
LimitDepth(folders, this.folderDepth)
AttachProfiles(folders, profileItems)
this.protectedRead(func() { MarkIgnored(folders, this.ignoredFolders) })
active := ActiveFolders(folders)
checksum = int64(len(active))
checksum += Sum(active, profileItems)
checksum += Sum(active, goFileItems)
return folders, checksum
}
func (this *Watcher) set(state int64) {
this.fileSystemState = state
}
func (this *Watcher) sendToExecutor(folders messaging.Folders) {
this.output <- folders
}
func (this *Watcher) ignore(paths string) {
this.protectedWrite(func() {
for _, folder := range strings.Split(paths, string(os.PathListSeparator)) {
this.ignoredFolders[folder] = struct{}{}
log.Println("Currently ignored folders:", this.ignoredFolders)
}
})
}
func (this *Watcher) reinstate(paths string) {
this.protectedWrite(func() {
for _, folder := range strings.Split(paths, string(os.PathListSeparator)) {
delete(this.ignoredFolders, folder)
}
})
}
func (this *Watcher) protectedWrite(protected func()) {
this.lock.Lock()
defer this.lock.Unlock()
protected()
}
func (this *Watcher) protectedRead(protected func()) {
this.lock.RLock()
defer this.lock.RUnlock()
protected()
}
const nap = time.Millisecond * 250