-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpath_monitor.go
45 lines (37 loc) · 942 Bytes
/
path_monitor.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
package main
import (
"log"
"time"
)
type PathMonitor interface {
StartMonitoring(repoPath string, watcher Watcher, git Git)
scheduleUpdate(repoPath string, channel chan string)
}
type GitRepoMonitor struct {
scheduledUpdateInterval time.Duration
}
func (g *GitRepoMonitor) scheduleUpdate(repoPath string, channel chan string) {
time.AfterFunc(g.scheduledUpdateInterval, func() {
channel <- repoPath
g.scheduleUpdate(repoPath, channel)
})
}
func (g *GitRepoMonitor) StartMonitoring(repoPath string, watcher Watcher, git Git) {
var channel = make(chan string)
err := git.Sync(repoPath)
if err != nil {
log.Printf("Syncing failed. Err: %v", err)
}
g.scheduleUpdate(repoPath, channel)
watcher.Watch(repoPath, channel)
go func() {
for {
path := <-channel
err = git.Sync(path)
if err != nil {
log.Printf("Syncing failed. Err: %v", err)
}
}
}()
log.Printf("Git notes is monitoring %s", repoPath)
}