/
main.go
131 lines (108 loc) · 2.71 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"github.com/2se/dolphin/config"
"github.com/2se/dolphin/core/cluster"
log "github.com/sirupsen/logrus"
"gopkg.in/urfave/cli.v1"
"os"
"os/signal"
"runtime/pprof"
"strings"
"syscall"
)
const (
ConfigUsage = "Path to config file. if empty, will finding in ${HOME}/.config/dolphin.d/config.toml and /etc/dolphin.d/config.toml"
FlagConfigKey = "config"
FlagPprofKey = "pprof"
FlagLoglvlKey = "loglvl"
PprofUsage = "File name to save profiling info to. Disabled if not set."
LoglvlUsage = "debug,info,warn"
)
func main() {
log.SetFormatter(&log.TextFormatter{ForceColors: true, FullTimestamp: true})
log.SetOutput(os.Stdout)
log.SetLevel(log.DebugLevel)
app := newApp()
app.Run(os.Args)
}
func newApp() (app *cli.App) {
app = cli.NewApp()
app.Action = run
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "c,config",
Usage: ConfigUsage,
},
cli.StringFlag{
Name: FlagPprofKey,
Usage: PprofUsage,
},
cli.StringFlag{
Name: FlagLoglvlKey,
Usage: LoglvlUsage,
},
}
return app
}
func run(context *cli.Context) error {
setLogLevel(context)
configPath := context.String(FlagConfigKey)
cnf, err := config.Load(configPath)
log.Infof("%s", cnf)
if err != nil || cnf == nil {
log.Fatalf("failed to load config file. may be error here: %v or else config is nil", err)
}
pprof := context.String(FlagPprofKey)
if len(pprof) > 0 {
runPprof(pprof)
}
_, err = cluster.Init(cnf.GetClusterConfig())
if err != nil {
log.Fatalf("初始化集群出错! %v", err)
}
cluster.Start(nil)
<-signalHandler()
cluster.Shutdown()
return nil
}
func runPprof(pprofFile string) {
log.Infof("pprof enabled. and it is path: %s", pprofFile)
var err error
cpuf, err := os.Create(pprofFile + ".cpu")
if err != nil {
log.Fatal("Failed to create CPU pprof file: ", err)
}
defer cpuf.Close()
memf, err := os.Create(pprofFile + ".mem")
if err != nil {
log.Fatal("Failed to create Mem pprof file: ", err)
}
defer memf.Close()
pprof.StartCPUProfile(cpuf)
defer pprof.StopCPUProfile()
defer pprof.WriteHeapProfile(memf)
log.Infof("Profiling info saved to '%s.(cpu|mem)'", pprofFile)
}
func setLogLevel(cliCtx *cli.Context) {
lglvl := cliCtx.String(FlagLoglvlKey)
switch strings.ToLower(lglvl) {
case "debug":
log.SetLevel(log.DebugLevel)
case "warn":
log.SetLevel(log.WarnLevel)
default:
log.SetLevel(log.InfoLevel)
}
}
func signalHandler() <-chan bool {
stop := make(chan bool)
signchan := make(chan os.Signal, 1)
signal.Notify(signchan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
go func() {
// Wait for a signal. Don't care which signal it is
sig := <-signchan
log.Infof("Signal received: '%s', shutting down", sig)
stop <- true
}()
return stop
}