forked from openshift/library-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
observer.go
66 lines (54 loc) · 1.79 KB
/
observer.go
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
package fileobserver
import (
"fmt"
"os"
"time"
"k8s.io/klog"
)
type Observer interface {
Run(stopChan <-chan struct{})
HasSynced() bool
AddReactor(reaction ReactorFn, startingFileContent map[string][]byte, files ...string) Observer
}
// ActionType define a type of action observed on the file
type ActionType int
const (
// FileModified means the file content was modified.
FileModified ActionType = iota
// FileCreated means the file was just created.
FileCreated
// FileDeleted means the file was deleted.
FileDeleted
)
// String returns human readable form of action taken on a file.
func (t ActionType) String(filename string) string {
switch t {
case FileCreated:
return fmt.Sprintf("file %s was created", filename)
case FileDeleted:
return fmt.Sprintf("file %s was deleted", filename)
case FileModified:
return fmt.Sprintf("file %s was modified", filename)
}
return ""
}
// ReactorFn define a reaction function called when an observed file is modified.
type ReactorFn func(file string, action ActionType) error
// ExitOnChangeReactor provides reactor function that causes the process to exit when the change is detected.
// DEPRECATED: Using this function cause process to exit immediately without proper shutdown (context close/etc.)
// Use the TerminateOnChangeReactor() instead.
var ExitOnChangeReactor = TerminateOnChangeReactor(func() { os.Exit(0) })
func TerminateOnChangeReactor(terminateFn func()) ReactorFn {
return func(filename string, action ActionType) error {
klog.Infof("Triggering shutdown because %s", action.String(filename))
terminateFn()
return nil
}
}
func NewObserver(interval time.Duration) (Observer, error) {
return &pollingObserver{
interval: interval,
reactors: map[string][]ReactorFn{},
files: map[string]fileHashAndState{},
}, nil
}