-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
81 lines (69 loc) · 1.9 KB
/
main.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
75
76
77
78
79
80
81
package main
import (
"fmt"
"os"
"os/signal"
"sync"
"syscall"
"github.com/prashantv/autobld/config"
"github.com/prashantv/autobld/log"
"github.com/prashantv/autobld/proxy"
"github.com/prashantv/autobld/task"
"gopkg.in/fsnotify.v1"
)
func main() {
c, err := config.Parse()
if err != nil {
log.Fatalf("Configuration error: %v", err)
}
watcher, err := config.SetupWatcher(c)
if err != nil {
log.Fatalf("Change detection failed: %v", err)
}
var (
// errC is used to report errors. Any error will cause a log.Fatal
errC = make(chan error)
// signnalC is used for signals to this process (ctrl+c, etc).
signalC = make(chan os.Signal)
)
signal.Notify(signalC, syscall.SIGINT, syscall.SIGKILL)
// Start all the proxy listeners, and ensure they block initially till the task starts.
var blockRequests sync.WaitGroup
blockRequests.Add(1)
for _, pc := range c.ProxyConfigs {
proxy.Start(pc, &blockRequests, errC)
}
if err := eventLoop(c, errC, signalC, &blockRequests, watcher); err != nil {
log.Fatalf("Error: %v", err)
}
}
func eventLoop(c *config.Config, errC <-chan error, signalC <-chan os.Signal, blockRequests *sync.WaitGroup, watcher *fsnotify.Watcher) error {
taskSM := task.NewSM(c, blockRequests)
defer taskSM.Close()
for {
// Any change in the task state should make the proxy try reconnecting.
proxy.RetryConnect()
// Run the task state machine.
for rerun := true; rerun; {
var err error
rerun, err = taskSM.Execute()
if err != nil {
return fmt.Errorf("task error: %v", err)
}
}
select {
case err := <-errC:
return err
case err := <-watcher.Errors:
return fmt.Errorf("watcher error: %v", err)
case <-signalC:
return nil
case event := <-watcher.Events:
if config.IsMatch(c, event.Name) && !taskSM.PendingClose() {
taskSM.Reload()
}
case <-taskSM.Reprocess:
// Nothing needs to be done, just the standard reprocess.
}
}
}