-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
89 lines (66 loc) · 1.53 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
82
83
84
85
86
87
88
89
package main
import (
"flag"
"github.com/go-yaml/yaml"
"github.com/ipchama/dupligator/config"
"github.com/ipchama/dupligator/manager"
"io/ioutil"
"os"
"os/signal"
"syscall"
)
type SignalType int
const (
stopSignalType SignalType = iota
reloadSignalType
)
func main() {
internalSignalsChann := make(chan SignalType)
configPathPtr := flag.String("config", "./config.yml", "A yaml file containing config options.")
flag.Parse()
catchSignals(internalSignalsChann)
for {
configData, err := ioutil.ReadFile(*configPathPtr)
if err != nil {
panic(err)
}
config := &config.Config{}
err = yaml.Unmarshal(configData, config)
if err != nil {
panic(err)
}
streamManager := manager.New(config)
err = streamManager.Init()
if err != nil {
panic(err)
}
err = streamManager.Run()
if err != nil {
panic(err)
}
iSig := <-internalSignalsChann
if iSig == stopSignalType {
println("Attempting graceful shutdown...")
streamManager.Stop()
break
} else if iSig == reloadSignalType {
println("Attempting graceful reload...")
streamManager.Stop()
}
}
}
func catchSignals(iChan chan SignalType) {
osSignalsChann := make(chan os.Signal)
signal.Notify(osSignalsChann, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
go func() {
var caughtSig os.Signal
for ; ; caughtSig = <-osSignalsChann {
if caughtSig == syscall.SIGTERM || caughtSig == syscall.SIGINT {
iChan <- stopSignalType
break
} else if caughtSig == syscall.SIGHUP {
iChan <- reloadSignalType
}
}
}()
}