@@ -12,7 +12,7 @@ import (
12
12
"code.gitea.io/gitea/modules/log"
13
13
"code.gitea.io/gitea/modules/process"
14
14
15
- "github.com/fsnotify/fsnotify "
15
+ "github.com/syncthing/notify "
16
16
)
17
17
18
18
type CreateWatcherOpts struct {
@@ -39,61 +39,57 @@ func run(ctx context.Context, desc string, opts *CreateWatcherOpts) {
39
39
log .Trace ("Watcher loop starting for %s" , desc )
40
40
defer log .Trace ("Watcher loop ended for %s" , desc )
41
41
42
- watcher , err := fsnotify .NewWatcher ()
43
- if err != nil {
44
- log .Error ("Unable to create watcher for %s: %v" , desc , err )
45
- return
46
- }
42
+ // Make the channel buffered to ensure no event is dropped. Notify will drop
43
+ // an event if the receiver is not able to keep up the sending pace.
44
+ events := make (chan notify.EventInfo , 1 )
45
+
47
46
if err := opts .PathsCallback (func (path , _ string , _ fs.DirEntry , err error ) error {
48
47
if err != nil && ! os .IsNotExist (err ) {
49
48
return err
50
49
}
51
50
log .Trace ("Watcher: %s watching %q" , desc , path )
52
- _ = watcher .Add (path )
51
+ if err := notify .Watch (path , events , notify .All ); err != nil {
52
+ log .Trace ("Watcher: %s unable to watch %q: error %v" , desc , path , err )
53
+ }
53
54
return nil
54
55
}); err != nil {
55
56
log .Error ("Unable to create watcher for %s: %v" , desc , err )
56
- _ = watcher . Close ( )
57
+ notify . Stop ( events )
57
58
return
58
59
}
59
60
60
61
// Note we don't call the BetweenCallback here
61
62
62
63
for {
63
64
select {
64
- case event , ok := <- watcher . Events :
65
+ case event , ok := <- events :
65
66
if ! ok {
66
- _ = watcher . Close ( )
67
+ notify . Stop ( events )
67
68
return
68
69
}
70
+
69
71
log .Debug ("Watched file for %s had event: %v" , desc , event )
70
- case err , ok := <- watcher .Errors :
71
- if ! ok {
72
- _ = watcher .Close ()
73
- return
74
- }
75
- log .Error ("Error whilst watching files for %s: %v" , desc , err )
76
72
case <- ctx .Done ():
77
- _ = watcher . Close ( )
73
+ notify . Stop ( events )
78
74
return
79
75
}
80
76
81
77
// Recreate the watcher - only call the BetweenCallback after the new watcher is set-up
82
- _ = watcher .Close ()
83
- watcher , err = fsnotify .NewWatcher ()
84
- if err != nil {
85
- log .Error ("Unable to create watcher for %s: %v" , desc , err )
86
- return
87
- }
78
+ notify .Stop (events )
79
+ events = make (chan notify.EventInfo , 1 )
80
+
88
81
if err := opts .PathsCallback (func (path , _ string , _ fs.DirEntry , err error ) error {
89
82
if err != nil {
90
83
return err
91
84
}
92
- _ = watcher .Add (path )
85
+ log .Trace ("Watcher: %s watching %q" , desc , path )
86
+ if err := notify .Watch (path , events , notify .All ); err != nil {
87
+ log .Trace ("Watcher: %s unable to watch %q: error %v" , desc , path , err )
88
+ }
93
89
return nil
94
90
}); err != nil {
95
91
log .Error ("Unable to create watcher for %s: %v" , desc , err )
96
- _ = watcher . Close ( )
92
+ notify . Stop ( events )
97
93
return
98
94
}
99
95
0 commit comments