-
Notifications
You must be signed in to change notification settings - Fork 105
/
watch.go
224 lines (199 loc) · 5.9 KB
/
watch.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
package main
import (
"os"
"path/filepath"
"time"
log "github.com/sirupsen/logrus"
mbtiles "github.com/brendan-ward/mbtiles-go"
"github.com/consbio/mbtileserver/handlers"
"github.com/fsnotify/fsnotify"
)
// debounce debounces requests to a callback function to occur no more
// frequently than interval; once this is reached, the callback is called.
//
// Unique values sent to the channel are stored in an internal map and all
// are processed once the the interval is up.
func debounce(interval time.Duration, input chan string, firstCallback func(arg string), callback func(arg string)) {
// keep a log of unique paths
var items = make(map[string]bool)
var item string
timer := time.NewTimer(interval)
for {
select {
case item = <-input:
if _, ok := items[item]; !ok {
// first time we see a given path, we need to call lockHandler
// to lock it (unlocked by callback)
firstCallback(item)
}
items[item] = true
timer.Reset(interval)
case <-timer.C:
for path := range items {
callback(path)
delete(items, path)
}
}
}
}
// FSWatcher provides a filesystem watcher to detect when mbtiles files are
// created, updated, or removed on the filesystem.
type FSWatcher struct {
watcher *fsnotify.Watcher
svcSet *handlers.ServiceSet
generateID handlers.IDGenerator
}
// NewFSWatcher creates a new FSWatcher to watch the filesystem for changes to
// mbtiles files and updates the ServiceSet accordingly.
//
// The generateID function needs to be of the same type used when the tilesets
// were originally added to the ServiceSet.
func NewFSWatcher(svcSet *handlers.ServiceSet, generateID handlers.IDGenerator) (*FSWatcher, error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
return &FSWatcher{
watcher: watcher,
svcSet: svcSet,
generateID: generateID,
}, nil
}
// Close closes the FSWatcher and stops watching the filesystem.
func (w *FSWatcher) Close() {
if w.watcher != nil {
w.watcher.Close()
}
}
// WatchDir sets up the filesystem watcher for baseDir and all existing subdirectories
func (w *FSWatcher) WatchDir(baseDir string) error {
c := make(chan string)
// debounced call to create / update tileset
go debounce(500*time.Millisecond, c, func(path string) {
// callback for first time path is debounced
id, err := w.generateID(path, baseDir)
if err != nil {
log.Errorf("Could not create ID for tileset %q\n%v", path, err)
return
}
// lock tileset for writing, if it exists
w.svcSet.LockTileset(id)
}, func(path string) {
// callback after debouncing incoming requests
// Verify that file can be opened with mbtiles-go, which runs
// validation on open.
// If file cannot be opened, assume it is still being written / copied.
db, err := mbtiles.Open(path)
if err != nil {
return
}
db.Close()
// determine file ID for tileset
id, err := w.generateID(path, baseDir)
if err != nil {
log.Errorf("Could not create ID for tileset %q\n%v", path, err)
return
}
// update existing tileset
if w.svcSet.HasTileset(id) {
err = w.svcSet.UpdateTileset(id)
if err != nil {
log.Errorf("Could not update tileset %q with ID %q\n%v", path, id, err)
} else {
// only unlock if successfully updated
w.svcSet.UnlockTileset(id)
log.Infof("Updated tileset %q with ID %q\n", path, id)
}
return
}
// create new tileset
err = w.svcSet.AddTileset(path, id)
if err != nil {
log.Errorf("Could not add tileset for %q with ID %q\n%v", path, id, err)
} else {
log.Infof("Updated tileset %q with ID %q\n", path, id)
}
return
})
go func() {
for {
select {
case event, ok := <-w.watcher.Events:
if !ok {
log.Errorf("error in filewatcher for %q, exiting filewatcher", event.Name)
return
}
if !((event.Op&fsnotify.Create == fsnotify.Create) ||
(event.Op&fsnotify.Write == fsnotify.Write) ||
(event.Op&fsnotify.Remove == fsnotify.Remove) ||
(event.Op&fsnotify.Rename == fsnotify.Rename)) {
continue
}
path := event.Name
if ext := filepath.Ext(path); ext != ".mbtiles" {
continue
}
if _, err := os.Stat(path + "-journal"); err == nil {
// Don't try to load .mbtiles files that are being written
log.Debugf("Tileset %q is currently being created or is incomplete\n", path)
continue
}
if (event.Op&fsnotify.Create == fsnotify.Create) ||
(event.Op&fsnotify.Write == fsnotify.Write) {
// This event may get called multiple times while a file is being copied into a watched directory,
// so we debounce this instead.
c <- path
continue
}
if (event.Op&fsnotify.Remove == fsnotify.Remove) || (event.Op&fsnotify.Rename == fsnotify.Rename) {
// some file move events trigger remove / rename, so if the file still exists, assume it is
// one of these
_, err := os.Stat(path)
if err == nil {
// debounce to give it a little more time to update, if needed
c <- path
continue
}
// remove tileset immediately so that there are not other errors in request handlers
id, err := w.generateID(path, baseDir)
if err != nil {
log.Errorf("Could not create ID for tileset %q\n%v", path, err)
}
err = w.svcSet.RemoveTileset(id)
if err != nil {
log.Errorf("Could not remove tileset %q with ID %q\n%v", path, id, err)
} else {
log.Infof("Removed tileset %q with ID %q\n", path, id)
}
}
case err, ok := <-w.watcher.Errors:
if !ok {
log.Errorf("error in filewatcher, exiting filewatcher")
return
}
log.Error(err)
}
}
}()
err := filepath.Walk(baseDir,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.Mode().IsDir() {
return w.watcher.Add(path)
}
return nil
})
if err != nil {
return err
}
return nil
}
func exists(path string) bool {
_, err := os.Stat(path)
if err != nil {
return false
}
return true
}