@@ -12,7 +12,7 @@ import (
1212 "code.gitea.io/gitea/modules/log"
1313 "code.gitea.io/gitea/modules/process"
1414
15- "github.com/fsnotify/fsnotify "
15+ "github.com/syncthing/notify "
1616)
1717
1818type CreateWatcherOpts struct {
@@ -39,61 +39,57 @@ func run(ctx context.Context, desc string, opts *CreateWatcherOpts) {
3939 log .Trace ("Watcher loop starting for %s" , desc )
4040 defer log .Trace ("Watcher loop ended for %s" , desc )
4141
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+
4746 if err := opts .PathsCallback (func (path , _ string , _ fs.DirEntry , err error ) error {
4847 if err != nil && ! os .IsNotExist (err ) {
4948 return err
5049 }
5150 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+ }
5354 return nil
5455 }); err != nil {
5556 log .Error ("Unable to create watcher for %s: %v" , desc , err )
56- _ = watcher . Close ( )
57+ notify . Stop ( events )
5758 return
5859 }
5960
6061 // Note we don't call the BetweenCallback here
6162
6263 for {
6364 select {
64- case event , ok := <- watcher . Events :
65+ case event , ok := <- events :
6566 if ! ok {
66- _ = watcher . Close ( )
67+ notify . Stop ( events )
6768 return
6869 }
70+
6971 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 )
7672 case <- ctx .Done ():
77- _ = watcher . Close ( )
73+ notify . Stop ( events )
7874 return
7975 }
8076
8177 // 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+
8881 if err := opts .PathsCallback (func (path , _ string , _ fs.DirEntry , err error ) error {
8982 if err != nil {
9083 return err
9184 }
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+ }
9389 return nil
9490 }); err != nil {
9591 log .Error ("Unable to create watcher for %s: %v" , desc , err )
96- _ = watcher . Close ( )
92+ notify . Stop ( events )
9793 return
9894 }
9995
0 commit comments