-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjanitor.go
74 lines (64 loc) · 1.6 KB
/
janitor.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
67
68
69
70
71
72
73
74
// The janitor cleans up expired tokens.
package main
import (
"os"
"time"
"path/filepath"
"github.com/fsnotify/fsnotify"
)
func startJanitor() {
info("Starting janitor with cleanup interval %s.", cfg.CleanupInterval)
ticker := time.NewTicker(getTime(cfg.CleanupInterval))
flags := fsnotify.Create | fsnotify.Write | fsnotify.Remove
userwatcher, err := fsnotify.NewWatcher()
if err != nil {
crit("Error starting user watcher: %s", err.Error())
os.Exit(2)
}
triggerwatcher, err := fsnotify.NewWatcher()
if err != nil {
crit("Error starting trigger watcher: %s", err.Error())
os.Exit(2)
}
userwatcher.Add(cfg.Users)
triggerwatcher.Add(cfg.Triggers)
go func() {
for {
select {
case <-ticker.C:
// clearTokens()
case event := <-userwatcher.Events:
if flags&event.Op&fsnotify.Create == fsnotify.Create {
loadUser(event.Name)
continue
}
if flags&event.Op&fsnotify.Write == fsnotify.Write {
loadUser(event.Name)
continue
}
if flags&event.Op&fsnotify.Remove == fsnotify.Remove {
unloadUser(filepath.Base(event.Name))
continue
}
case event := <-triggerwatcher.Events:
if flags&event.Op&fsnotify.Create == fsnotify.Create {
loadTrigger(event.Name)
continue
}
if flags&event.Op&fsnotify.Write == fsnotify.Write {
loadTrigger(event.Name)
continue
}
if flags&event.Op&fsnotify.Remove == fsnotify.Remove {
unloadTrigger(filepath.Base(event.Name))
continue
}
case <-Channels.janitorquit:
info("Shutting down janitor.")
ticker.Stop()
userwatcher.Close()
return
}
}
}()
}