summaryrefslogtreecommitdiff
path: root/pkg/hooks/monitor.go
blob: ba5e0f2468158e292478274595c451b72f67a42c (plain)
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
package hooks

import (
	"context"
	"path/filepath"

	"github.com/fsnotify/fsnotify"
	"github.com/sirupsen/logrus"
)

// Monitor dynamically monitors hook directories for additions,
// updates, and removals.
//
// This function write two empty structs to the sync channel: the
// first is written after the watchers are established and the second
// when this function exits.  The expected usage is:
//
//   ctx, cancel := context.WithCancel(context.Background())
//   sync := make(chan error, 2)
//   go m.Monitor(ctx, sync)
//   err := <-sync // block until writers are established
//   if err != nil {
//     return err // failed to establish watchers
//   }
//   // do stuff
//   cancel()
//   err = <-sync // block until monitor finishes
func (m *Manager) Monitor(ctx context.Context, sync chan<- error) {
	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		sync <- err
		return
	}
	defer watcher.Close()

	for _, dir := range m.directories {
		err = watcher.Add(dir)
		if err != nil {
			logrus.Errorf("failed to watch %q for hooks", dir)
			sync <- err
			return
		}
		logrus.Debugf("monitoring %q for hooks", dir)
	}

	sync <- nil

	for {
		select {
		case event := <-watcher.Events:
			if event.Op&fsnotify.Remove == fsnotify.Remove {
				ok := m.remove(filepath.Base(event.Name))
				if ok {
					logrus.Debugf("removed hook %s", event.Name)
				}
			}
			if event.Op&fsnotify.Create == fsnotify.Create || event.Op&fsnotify.Write == fsnotify.Write {
				err = m.add(event.Name)
				if err == nil {
					logrus.Debugf("added hook %s", event.Name)
				} else if err != ErrNoJSONSuffix {
					logrus.Errorf("failed to add hook %s: %v", event.Name, err)
				}
			}
		case <-ctx.Done():
			err = ctx.Err()
			logrus.Debugf("hook monitoring canceled: %v", err)
			sync <- err
			close(sync)
			return
		}
	}
}