-
Notifications
You must be signed in to change notification settings - Fork 32
/
main.go
67 lines (54 loc) · 1.32 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
package main
import (
"log"
"os"
"os/signal"
"path"
"syscall"
"github.com/natefinch/lumberjack"
"github.com/arigatomachine/cli/daemon/config"
)
func main() {
arigatoRoot, err := config.CreateArigatoRoot(os.Getenv("ARIGATO_ROOT"))
if err != nil {
log.Fatalf("Failed to initialize Arigato root dir: %s", err)
}
log.SetOutput(&lumberjack.Logger{
Filename: path.Join(arigatoRoot, "daemon.log"),
MaxSize: 10, // megabytes
MaxBackups: 3,
MaxAge: 28, // days
})
cfg, err := config.NewConfig(arigatoRoot)
if err != nil {
log.Fatalf("Failed to load config: %s", err)
}
daemon, err := NewDaemon(cfg)
if err != nil {
log.Fatalf("Failed to create daemon: %s", err)
}
go watch(daemon)
defer daemon.Shutdown()
log.Printf("v%s of the Daemon is now listening on %s", cfg.Version, daemon.Addr())
err = daemon.Run()
if err != nil {
log.Printf("Error while running daemon: %s", err)
}
}
func watch(daemon *Daemon) {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
s := <-c
log.Printf("Caught a signal: %s", s)
shutdown(daemon)
}
func shutdown(daemon *Daemon) {
err := daemon.Shutdown()
if err != nil {
log.Printf("Did not shutdown cleanly, error: %s", err)
}
if r := recover(); r != nil {
log.Printf("Failed shutting down; caught panic: %v", r)
panic(r)
}
}