-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain.go
79 lines (66 loc) · 1.68 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
package main
import (
"flag"
"log"
"os"
"os/signal"
"syscall"
"github.com/Clever/sphinx/config"
"github.com/Clever/sphinx/daemon"
"gopkg.in/Clever/kayvee-go.v6/logger"
)
var (
configfile = flag.String("config", "example.yaml", "/path/to/configuration.yaml")
validate = flag.Bool("validate", false, "Validate configuration and exit")
)
func init() {
if err := logger.SetGlobalRouting("kvconfig.yml"); err != nil {
log.Fatalf("error setting kv config: %s", err)
}
}
func main() {
flag.Parse()
conf, err := config.New(*configfile)
if err != nil {
log.Fatalf("LOAD_CONFIG_FAILED: %s", err.Error())
}
sphinxd, err := daemon.New(conf)
if err != nil {
log.Fatal(err)
}
setupSighupHandler(sphinxd, sighupHandler)
if *validate {
print("configuration parsed and Sphinx loaded fine. not starting dameon.")
return
}
sphinxd.Start()
}
// sighupHandler is the default HUP signal handler. It is defined separately so tests can
// overwrite it with their own handler.
func sighupHandler(d daemon.Daemon) {
conf, err := config.New(*configfile)
if err != nil {
log.Println("RELOAD_CONFIG_FILE_FAILED: " + err.Error())
return
}
err = d.LoadConfig(conf)
if err != nil {
log.Println("RELOAD_CONFIG_FILE_FAILED: " + err.Error())
return
}
log.Println("Reloaded config file")
}
// setupSighupHandler creates a channel to listen for HUP signals and process them.
func setupSighupHandler(d daemon.Daemon, handler func(daemon.Daemon)) {
sigc := make(chan os.Signal)
signal.Notify(sigc, syscall.SIGHUP)
go func() {
// Listen for HUP signals "forever", calling the hup-handler each time
// one is received.
for {
<-sigc
log.Println("Received SIGHUP")
handler(d)
}
}()
}