-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathmain.go
87 lines (76 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
82
83
84
85
86
87
package main
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"go.uber.org/automaxprocs/maxprocs"
"github.com/mgtv-tech/redis-GunYu/cmd"
"github.com/mgtv-tech/redis-GunYu/config"
"github.com/mgtv-tech/redis-GunYu/pkg/log"
"github.com/mgtv-tech/redis-GunYu/pkg/sync"
"github.com/mgtv-tech/redis-GunYu/pkg/util"
)
func main() {
maxprocs.Set()
panicIfError(config.LoadFlags())
panicIfError(runCmd())
}
func runCmd() error {
gracefullTimeout := 5 * time.Second
var cmder cmd.Cmd
switch config.GetFlag().Cmd {
case "sync":
if config.GetFlag().ConfigPath != "" {
panicIfError(config.InitSyncerConfig(config.GetFlag().ConfigPath))
}
panicIfError(log.InitLog(*config.GetSyncerConfig().Log))
cmder = cmd.NewSyncerCmd()
gracefullTimeout = config.GetSyncerConfig().Server.GracefullStopTimeout
case "rdb":
if config.GetFlag().ConfigPath != "" {
panicIfError(config.InitRdbConfig(config.GetFlag().ConfigPath))
}
cmder = cmd.NewRdbCmd()
case "aof":
cmder = cmd.NewAofCmd()
default:
panicIfError(fmt.Errorf("does not support command(%s)", config.GetFlag().Cmd))
}
sync.SafeGo(func() {
handleSignal(cmder, gracefullTimeout)
}, nil)
return cmder.Run()
}
func handleSignal(c cmd.Cmd, gracefullTimeout time.Duration) {
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGPIPE, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGABRT)
for {
sig := <-signals
log.Infof("received signal: %s", sig)
switch sig {
case syscall.SIGPIPE:
default:
ctx, cancel := context.WithTimeout(context.Background(), gracefullTimeout)
defer cancel()
util.StopWithCtx(ctx, func() {
log.Infof("stop cmd(%s)", c.Name())
err := c.Stop()
if err != nil {
log.Errorf("cmd(%s) stopped with error : %v", c.Name(), err)
}
})
log.Sync()
os.Exit(0)
return
}
}
}
func panicIfError(err error) {
if err == nil {
return
}
log.Panic(err)
}