forked from badgerodon/stack
/
watch.go
executable file
·43 lines (36 loc) · 899 Bytes
/
watch.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
package main
import (
"log"
"time"
"github.com/badgerodon/stack/storage"
"github.com/badgerodon/stack/sync"
"github.com/cenkalti/backoff"
)
func watch(src string) error {
loc, err := storage.ParseLocation(src)
if err != nil {
return err
}
watcher, err := sync.Watch(loc)
if err != nil {
return err
}
defer watcher.Stop()
// TODO: a better approach here would be to use a channel to retry on,
// then if you jacked up the config, it would pick up the change
// in the middle of all the retries. As it stands now it would take a
// minute to fix itself.
eb := backoff.NewExponentialBackOff()
eb.MaxElapsedTime = time.Minute
for range watcher.C {
log.Println("[watch] new version")
backoff.Retry(func() error {
err := apply(src)
if err != nil {
log.Printf("[watch] error installing: %v\n", err)
}
return err
}, eb)
}
return nil
}