code.pablomurad.com

patchwork

Publishes the revisions of your pages as a feed, with notes and readable diffs, written in Go

internal/source/folder/watch.go raw
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
package folder

import (
	"context"
	"io/fs"
	"os"
	"path/filepath"
	"time"

	"github.com/fsnotify/fsnotify"
)

// Watch scans once, then again after file events settle (debounce), on
// every poll tick, and shortly after any scan that left files pending.
// fsnotify is a hint, not a source of truth: it misses events on network
// mounts and some Docker bind mounts, which is why polling always runs.
// onReport is called after every scan.
func (r *Runner) Watch(ctx context.Context, debounce, poll time.Duration, onReport func(Report, error)) error {
	events := make(chan struct{}, 1)
	if r.PollOnly {
		r.Log.Info("file events disabled; polling only")
	} else if w, err := fsnotify.NewWatcher(); err != nil {
		r.Log.Warn("file events unavailable; polling only", "err", err)
	} else {
		defer w.Close()
		r.addDirs(w)
		go r.forward(ctx, w, events)
	}

	var tick <-chan time.Time
	if poll > 0 {
		t := time.NewTicker(poll)
		defer t.Stop()
		tick = t.C
	}
	var timer *time.Timer
	var fire <-chan time.Time
	schedule := func(d time.Duration) {
		if timer != nil {
			timer.Stop()
		}
		timer = time.NewTimer(d)
		fire = timer.C
	}

	scan := func() {
		rep, err := r.Scan(ctx, false)
		onReport(rep, err)
		if len(rep.Pending) > 0 {
			schedule(r.Settle + 100*time.Millisecond)
		}
	}
	scan()
	for {
		select {
		case <-ctx.Done():
			return ctx.Err()
		case <-events:
			schedule(debounce)
		case <-fire:
			fire = nil
			scan()
		case <-tick:
			scan()
		}
	}
}

func (r *Runner) forward(ctx context.Context, w *fsnotify.Watcher, events chan<- struct{}) {
	for {
		select {
		case <-ctx.Done():
			return
		case ev, ok := <-w.Events:
			if !ok {
				return
			}
			// New directories need their own watch.
			if ev.Has(fsnotify.Create) {
				r.addDir(w, ev.Name)
			}
			select {
			case events <- struct{}{}:
			default:
			}
		case err, ok := <-w.Errors:
			if !ok {
				return
			}
			r.Log.Warn("file watcher error", "err", err)
		}
	}
}

func (r *Runner) addDirs(w *fsnotify.Watcher) {
	_ = filepath.WalkDir(r.Root, func(p string, d fs.DirEntry, err error) error {
		if err != nil || !d.IsDir() {
			return nil
		}
		if p != r.Root && r.Matcher.excluded(d.Name()) {
			return filepath.SkipDir
		}
		if err := w.Add(p); err != nil {
			r.Log.Warn("cannot watch directory", "dir", p, "err", err)
		}
		return nil
	})
}

func (r *Runner) addDir(w *fsnotify.Watcher, p string) {
	if fi, err := os.Stat(p); err == nil && fi.IsDir() && !r.Matcher.excluded(fi.Name()) {
		_ = w.Add(p)
	}
}