@@ -17,7 +17,7 @@ import (
17
17
//
18
18
// Unique values sent to the channel are stored in an internal map and all
19
19
// are processed once the the interval is up.
20
- func debounce (interval time.Duration , input chan string , firstCallback func (arg string ), callback func (arg string )) {
20
+ func debounce (interval time.Duration , input chan string , exit chan struct {}, firstCallback func (arg string ), callback func (arg string )) {
21
21
// keep a log of unique paths
22
22
var items = make (map [string ]bool )
23
23
var item string
@@ -37,6 +37,8 @@ func debounce(interval time.Duration, input chan string, firstCallback func(arg
37
37
callback (path )
38
38
delete (items , path )
39
39
}
40
+ case <- exit :
41
+ return
40
42
}
41
43
}
42
44
}
@@ -76,9 +78,9 @@ func (w *FSWatcher) Close() {
76
78
// WatchDir sets up the filesystem watcher for baseDir and all existing subdirectories
77
79
func (w * FSWatcher ) WatchDir (baseDir string ) error {
78
80
c := make (chan string )
79
-
81
+ exit := make ( chan struct {})
80
82
// debounced call to create / update tileset
81
- go debounce (500 * time .Millisecond , c , func (path string ) {
83
+ go debounce (500 * time .Millisecond , c , exit , func (path string ) {
82
84
// callback for first time path is debounced
83
85
id , err := w .generateID (path , baseDir )
84
86
if err != nil {
@@ -128,7 +130,6 @@ func (w *FSWatcher) WatchDir(baseDir string) error {
128
130
}
129
131
return
130
132
})
131
-
132
133
go func () {
133
134
for {
134
135
select {
@@ -146,9 +147,25 @@ func (w *FSWatcher) WatchDir(baseDir string) error {
146
147
}
147
148
148
149
path := event .Name
150
+ if ext := filepath .Ext (path ); ext == "" {
151
+ if event .Op & fsnotify .Create == fsnotify .Create || event .Op & fsnotify .Write == fsnotify .Write ||
152
+ event .Op & fsnotify .Remove == fsnotify .Remove || event .Op & fsnotify .Rename == fsnotify .Rename {
153
+
154
+ // NOTE: we cannot distinguish which incoming event paths
155
+ // correspond to directory events or file events, so we
156
+ // trigger a reload in all cases
157
+
158
+ exit <- struct {}{}
159
+ err := w .WatchDir (baseDir )
160
+ if err != nil {
161
+ return
162
+ }
163
+ log .Info ("Reload watch dir on directory change" )
164
+ return
165
+ } else {
166
+ continue
167
+ }
149
168
150
- if ext := filepath .Ext (path ); ext != ".mbtiles" {
151
- continue
152
169
}
153
170
154
171
if _ , err := os .Stat (path + "-journal" ); err == nil {
@@ -180,11 +197,13 @@ func (w *FSWatcher) WatchDir(baseDir string) error {
180
197
if err != nil {
181
198
log .Errorf ("Could not create ID for tileset %q\n %v" , path , err )
182
199
}
183
- err = w .svcSet .RemoveTileset (id )
184
- if err != nil {
185
- log .Errorf ("Could not remove tileset %q with ID %q\n %v" , path , id , err )
186
- } else {
187
- log .Infof ("Removed tileset %q with ID %q\n " , path , id )
200
+ if w .svcSet .HasTileset (id ) {
201
+ err = w .svcSet .RemoveTileset (id )
202
+ if err != nil {
203
+ log .Errorf ("Could not remove tileset %q with ID %q\n %v" , path , id , err )
204
+ } else {
205
+ log .Infof ("Removed tileset %q with ID %q\n " , path , id )
206
+ }
188
207
}
189
208
}
190
209
0 commit comments