Skip to content

Commit 23df132

Browse files
committed
fix: don't create events on the first sync
1 parent 5d1b58b commit 23df132

File tree

5 files changed

+124
-109
lines changed

5 files changed

+124
-109
lines changed

dropbox.go

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,11 @@ func (w *DropboxWatcher) Start() error {
9393
w.ticker = time.NewTicker(w.pollingTime)
9494
go func() {
9595
// launch synchronization also the first time
96-
w.sync()
96+
w.sync(true)
9797
for {
9898
select {
9999
case <-w.ticker.C:
100-
w.sync()
100+
w.sync(false)
101101

102102
case <-w.stop:
103103
close(w.Events)
@@ -129,7 +129,7 @@ func (w *DropboxWatcher) initDropboxClient() {
129129
w.client = files.New(config)
130130
}
131131

132-
func (w *DropboxWatcher) sync() {
132+
func (w *DropboxWatcher) sync(firstSync bool) {
133133
// allow only one sync at same time
134134
if !atomic.CompareAndSwapUint32(&w.syncing, 0, 1) {
135135
return
@@ -142,28 +142,30 @@ func (w *DropboxWatcher) sync() {
142142

143143
fileList := make(map[string]*DropboxObject, 0)
144144
err := w.enumerateFiles(w.watchDir, func(obj *DropboxObject) bool {
145-
// Store the files to check the deleted one
146-
fileList[obj.Key] = obj
147-
// Check if the object is cached by Key
148-
cached := w.getCachedObject(obj)
149-
// Object has been cached previously by Key
150-
if cached != nil {
151-
// Check if the LastModified has been changed
152-
if !cached.LastModified.Equal(obj.LastModified) || cached.Hash != obj.Hash || cached.Size != obj.Size {
145+
if !firstSync {
146+
// Store the files to check the deleted one
147+
fileList[obj.Key] = obj
148+
// Check if the object is cached by Key
149+
cached := w.getCachedObject(obj)
150+
// Object has been cached previously by Key
151+
if cached != nil {
152+
// Check if the LastModified has been changed
153+
if !cached.LastModified.Equal(obj.LastModified) || cached.Hash != obj.Hash || cached.Size != obj.Size {
154+
event := Event{
155+
Key: obj.Key,
156+
Type: FileChanged,
157+
Object: obj,
158+
}
159+
w.Events <- event
160+
}
161+
} else {
153162
event := Event{
154163
Key: obj.Key,
155-
Type: FileChanged,
164+
Type: FileCreated,
156165
Object: obj,
157166
}
158167
w.Events <- event
159168
}
160-
} else {
161-
event := Event{
162-
Key: obj.Key,
163-
Type: FileCreated,
164-
Object: obj,
165-
}
166-
w.Events <- event
167169
}
168170
w.cache[obj.Key] = obj
169171
return true

gdrive.go

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,11 @@ func (w *GDriveWatcher) Start() error {
9595
w.ticker = time.NewTicker(w.pollingTime)
9696
go func() {
9797
// launch synchronization also the first time
98-
w.sync()
98+
w.sync(true)
9999
for {
100100
select {
101101
case <-w.ticker.C:
102-
w.sync()
102+
w.sync(false)
103103

104104
case <-w.stop:
105105
close(w.Events)
@@ -118,7 +118,7 @@ func (w *GDriveWatcher) Close() {
118118
}
119119
}
120120

121-
func (w *GDriveWatcher) sync() {
121+
func (w *GDriveWatcher) sync(firstSync bool) {
122122
// allow only one sync at same time
123123
if !atomic.CompareAndSwapUint32(&w.syncing, 0, 1) {
124124
return
@@ -128,28 +128,31 @@ func (w *GDriveWatcher) sync() {
128128
fileList := make(map[string]*GDriveObject, 0)
129129

130130
err := w.enumerateFiles(w.watchDir, func(obj *GDriveObject) bool {
131-
// Store the files to check the deleted one
132-
fileList[obj.ID] = obj
133-
// Check if the object is cached by Key
134-
cached := w.getCachedObject(obj)
135-
// Object has been cached previously by Key
136-
if cached != nil {
137-
// Check if the LastModified has been changed
138-
if !cached.LastModified.Equal(obj.LastModified) || cached.Hash != obj.Hash {
131+
// With the first sync we need to cache all the files
132+
if !firstSync {
133+
// Store the files to check the deleted one
134+
fileList[obj.ID] = obj
135+
// Check if the object is cached by Key
136+
cached := w.getCachedObject(obj)
137+
// Object has been cached previously by Key
138+
if cached != nil {
139+
// Check if the LastModified has been changed
140+
if !cached.LastModified.Equal(obj.LastModified) || cached.Hash != obj.Hash {
141+
event := Event{
142+
Key: obj.Key,
143+
Type: FileChanged,
144+
Object: obj,
145+
}
146+
w.Events <- event
147+
}
148+
} else {
139149
event := Event{
140150
Key: obj.Key,
141-
Type: FileChanged,
151+
Type: FileCreated,
142152
Object: obj,
143153
}
144154
w.Events <- event
145155
}
146-
} else {
147-
event := Event{
148-
Key: obj.Key,
149-
Type: FileCreated,
150-
Object: obj,
151-
}
152-
w.Events <- event
153156
}
154157
w.cache[obj.ID] = obj
155158
return true

git.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,11 @@ func (w *GitWatcher) Start() error {
139139
w.ticker = time.NewTicker(w.pollingTime)
140140
go func() {
141141
// launch synchronization also the first time
142-
w.sync()
142+
w.sync(true)
143143
for {
144144
select {
145145
case <-w.ticker.C:
146-
w.sync()
146+
w.sync(false)
147147

148148
case <-w.stop:
149149
close(w.Events)
@@ -169,7 +169,7 @@ func (w *GitWatcher) getCachedObject(o *GitObject) *GitObject {
169169
return nil
170170
}
171171

172-
func (w *GitWatcher) sync() {
172+
func (w *GitWatcher) sync(firstSync bool) {
173173
// allow only one sync at same time
174174
if !atomic.CompareAndSwapUint32(&w.syncing, 0, 1) {
175175
return
@@ -184,13 +184,17 @@ func (w *GitWatcher) sync() {
184184

185185
// default behaviour is file
186186
if w.config.MonitorType == "repo" {
187-
// if we don't have commits in the cache this is the first time it is being executed
188-
firstSync := len(w.branchCache) == 0
189187
w.checkCommits(firstSync)
190188
w.checkTags(firstSync)
191189
} else {
192190
fileList := make(map[string]*GitObject, 0)
193191
err := w.enumerateFiles(w.watchDir, func(obj *GitObject) bool {
192+
// With the first sync we need to cache all the files
193+
if firstSync {
194+
w.fileCache[obj.Key] = obj
195+
return true
196+
}
197+
194198
// Store the files to check the deleted one
195199
fileList[obj.Key] = obj
196200
// Check if the object is cached by Key

local.go

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ func (w *LocalWatcher) Start() error {
8282
w.ticker = time.NewTicker(w.pollingTime)
8383
go func() {
8484
// launch synchronization also the first time
85-
w.sync()
85+
w.sync(true)
8686
for {
8787
select {
8888
case <-w.ticker.C:
89-
w.sync()
89+
w.sync(false)
9090

9191
case <-w.stop:
9292
close(w.Events)
@@ -203,7 +203,7 @@ func (w *LocalWatcher) Close() {
203203
w.stop <- true
204204
}
205205

206-
func (w *LocalWatcher) sync() {
206+
func (w *LocalWatcher) sync(firstSync bool) {
207207
// allow only one sync at same time
208208
if !atomic.CompareAndSwapUint32(&w.syncing, 0, 1) {
209209
return
@@ -233,57 +233,61 @@ func (w *LocalWatcher) sync() {
233233
FileMode: fi.Mode(),
234234
}
235235

236-
fileList[walkPath] = obj
237-
238-
// Check if the object is cached by Key
239-
cached := w.getCachedObject(obj)
240-
// Object has been cached previously by Key
241-
if cached != nil {
242-
// Check if the LastModified has been changed
243-
if !cached.LastModified.Equal(obj.LastModified) || (cached.Size != obj.Size) {
244-
event := Event{
245-
Key: obj.Key,
246-
Type: FileChanged,
247-
Object: obj,
236+
if firstSync {
237+
fileList[walkPath] = obj
238+
239+
// Check if the object is cached by Key
240+
cached := w.getCachedObject(obj)
241+
// Object has been cached previously by Key
242+
if cached != nil {
243+
// Check if the LastModified has been changed
244+
if !cached.LastModified.Equal(obj.LastModified) || (cached.Size != obj.Size) {
245+
event := Event{
246+
Key: obj.Key,
247+
Type: FileChanged,
248+
Object: obj,
249+
}
250+
w.Events <- event
248251
}
249-
w.Events <- event
250-
}
251-
// Check if the file modes have been updated
252-
if cached.FileMode != obj.FileMode {
252+
// Check if the file modes have been updated
253+
if cached.FileMode != obj.FileMode {
254+
event := Event{
255+
Key: obj.Key,
256+
Type: TagsChanged,
257+
Object: obj,
258+
}
259+
w.Events <- event
260+
}
261+
} else {
253262
event := Event{
254263
Key: obj.Key,
255-
Type: TagsChanged,
264+
Type: FileCreated,
256265
Object: obj,
257266
}
258267
w.Events <- event
259268
}
260-
} else {
261-
event := Event{
262-
Key: obj.Key,
263-
Type: FileCreated,
264-
Object: obj,
265-
}
266-
w.Events <- event
267269
}
268-
w.cache[obj.Key] = obj
269270

271+
w.cache[obj.Key] = obj
270272
return nil
271273
})
272274
if err != nil {
273275
w.Errors <- err
274276
return
275277
}
276278

277-
for k, o := range w.cache {
278-
if _, found := fileList[k]; !found {
279-
// file not found in the list...deleting it
280-
delete(w.cache, k)
281-
event := Event{
282-
Key: o.Key,
283-
Type: FileDeleted,
284-
Object: o,
279+
if len(fileList) != 0 {
280+
for k, o := range w.cache {
281+
if _, found := fileList[k]; !found {
282+
// file not found in the list...deleting it
283+
delete(w.cache, k)
284+
event := Event{
285+
Key: o.Key,
286+
Type: FileDeleted,
287+
Object: o,
288+
}
289+
w.Events <- event
285290
}
286-
w.Events <- event
287291
}
288292
}
289293
}

s3.go

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ func (u *S3Watcher) Start() error {
9999
u.ticker = time.NewTicker(u.pollingTime)
100100
go func() {
101101
// launch synchronization also the first time
102-
u.sync()
102+
u.sync(true)
103103
for {
104104
select {
105105
case <-u.ticker.C:
106-
u.sync()
106+
u.sync(false)
107107

108108
case <-u.stop:
109109
close(u.Events)
@@ -147,7 +147,7 @@ func (u *S3Object) areTagsChanged(new *S3Object) bool {
147147
return false
148148
}
149149

150-
func (u *S3Watcher) sync() {
150+
func (u *S3Watcher) sync(firstSync bool) {
151151
// allow only one sync at same time
152152
if !atomic.CompareAndSwapUint32(&u.syncing, 0, 1) {
153153
return
@@ -168,41 +168,43 @@ func (u *S3Watcher) sync() {
168168
return true // continue
169169
}
170170

171-
// Store the files to check the deleted one
172-
fileList[upd.Key] = upd
173-
174-
// Check if the object is cached by Key
175-
cached := u.getCachedObject(upd)
176-
// Object has been cached previously by Key
177-
if cached != nil {
178-
// Check if the LastModified has been changed
179-
if !cached.LastModified.Equal(upd.LastModified) || cached.Size != upd.Size {
180-
event := Event{
181-
Key: upd.Key,
182-
Type: FileChanged,
183-
Object: upd,
171+
if !firstSync {
172+
// Store the files to check the deleted one
173+
fileList[upd.Key] = upd
174+
175+
// Check if the object is cached by Key
176+
cached := u.getCachedObject(upd)
177+
// Object has been cached previously by Key
178+
if cached != nil {
179+
// Check if the LastModified has been changed
180+
if !cached.LastModified.Equal(upd.LastModified) || cached.Size != upd.Size {
181+
event := Event{
182+
Key: upd.Key,
183+
Type: FileChanged,
184+
Object: upd,
185+
}
186+
u.Events <- event
184187
}
185-
u.Events <- event
186-
}
187-
// Check if the tags have been updated
188-
if cached.areTagsChanged(upd) {
188+
// Check if the tags have been updated
189+
if cached.areTagsChanged(upd) {
190+
event := Event{
191+
Key: upd.Key,
192+
Type: TagsChanged,
193+
Object: upd,
194+
}
195+
u.Events <- event
196+
}
197+
} else {
189198
event := Event{
190199
Key: upd.Key,
191-
Type: TagsChanged,
200+
Type: FileCreated,
192201
Object: upd,
193202
}
194203
u.Events <- event
195204
}
196-
} else {
197-
event := Event{
198-
Key: upd.Key,
199-
Type: FileCreated,
200-
Object: upd,
201-
}
202-
u.Events <- event
203205
}
204-
u.cache[upd.Key] = upd
205206

207+
u.cache[upd.Key] = upd
206208
return true
207209
})
208210
if err != nil {

0 commit comments

Comments
 (0)