From 09360ba76e6a74ba84b87c9607f847295f8981c4 Mon Sep 17 00:00:00 2001 From: wangyiyang Date: Fri, 29 Mar 2019 17:47:16 +0800 Subject: [PATCH] add pid path --- conf/redis-shake.conf | 4 +++- src/redis-shake/common/mix.go | 9 ++++++-- src/redis-shake/configure/configure.go | 1 + src/redis-shake/main/main.go | 32 +++++++++++++------------- 4 files changed, 27 insertions(+), 19 deletions(-) diff --git a/conf/redis-shake.conf b/conf/redis-shake.conf index fff9078..5d51c18 100644 --- a/conf/redis-shake.conf +++ b/conf/redis-shake.conf @@ -2,8 +2,10 @@ # id id = redis-shake -# log file,日志文件,不配置将打印到stdout +# log file,日志文件,不配置将打印到stdout (e.g. /var/log/redis-shake.log ) log_file = +# pid path,进程文件存储地址,不配置将输出到项目目录下 (e.g. /var/run/ ) +pid_path = # pprof port system_profile = 9310 diff --git a/src/redis-shake/common/mix.go b/src/redis-shake/common/mix.go index 3945d14..1ad3de8 100644 --- a/src/redis-shake/common/mix.go +++ b/src/redis-shake/common/mix.go @@ -22,8 +22,13 @@ func WritePid(id string) (err error) { return nil } -func WritePidById(id string) error { - dir, _ := os.Getwd() +func WritePidById(id string, path string) error { + var dir string + if path == "" { + dir, _ = os.Getwd() + } else { + dir = path + } pidfile := filepath.Join(dir, id) + ".pid" if err := WritePid(pidfile); err != nil { return err diff --git a/src/redis-shake/configure/configure.go b/src/redis-shake/configure/configure.go index 44437ad..bff85bb 100644 --- a/src/redis-shake/configure/configure.go +++ b/src/redis-shake/configure/configure.go @@ -40,6 +40,7 @@ type Configuration struct { SenderCount uint `config:"sender.count"` SenderDelayChannelSize uint `config:"sender.delay_channel_size"` KeepAlive uint `config:"keep_alive"` + PidPath string `config:"pid_path"` // inner variables ReplaceHashTag bool `config:"replace_hash_tag"` diff --git a/src/redis-shake/main/main.go b/src/redis-shake/main/main.go index b556938..26dce4b 100644 --- a/src/redis-shake/main/main.go +++ b/src/redis-shake/main/main.go @@ -5,36 +5,36 @@ package main import ( + "encoding/json" "flag" "fmt" + "math" + _ "net/http/pprof" "os" "os/signal" - "syscall" - "runtime/debug" - "time" + "reflect" "runtime" - "math" - _ "net/http/pprof" - "strings" + "runtime/debug" "strconv" - "encoding/json" - "reflect" + "strings" + "syscall" + "time" + "pkg/libs/log" + "redis-shake" + "redis-shake/base" "redis-shake/common" "redis-shake/configure" "redis-shake/metric" - "redis-shake" - "redis-shake/base" "redis-shake/restful" - "pkg/libs/log" "github.com/gugemichael/nimo4go" logRotate "gopkg.in/natefinch/lumberjack.v2" ) -type Exit struct {Code int} +type Exit struct{ Code int } -const( +const ( TypeDecode = "decode" TypeRestore = "restore" TypeDump = "dump" @@ -83,7 +83,7 @@ func main() { utils.Welcome() utils.StartTime = fmt.Sprintf("%v", time.Now().Format(utils.GolangSecurityTime)) - if err = utils.WritePidById(conf.Options.Id); err != nil { + if err = utils.WritePidById(conf.Options.Id, conf.Options.PidPath); err != nil { crash(fmt.Sprintf("write pid failed. %v", err), -5) } @@ -240,7 +240,7 @@ func sanitizeOptions(tp string) error { if n, err := strconv.ParseInt(conf.Options.FakeTime[1:], 10, 64); err != nil { return fmt.Errorf("parse fake_time failed[%v]", err) } else { - conf.Options.ShiftTime = time.Duration(n * int64(time.Millisecond) - time.Now().UnixNano()) + conf.Options.ShiftTime = time.Duration(n*int64(time.Millisecond) - time.Now().UnixNano()) } default: if t, err := time.Parse("2006-01-02 15:04:05", conf.Options.FakeTime); err != nil { @@ -291,7 +291,7 @@ func sanitizeOptions(tp string) error { if tp == TypeRestore || tp == TypeSync { // get target redis version and set TargetReplace. if conf.Options.TargetRedisVersion, err = utils.GetRedisVersion(conf.Options.TargetAddress, - conf.Options.TargetAuthType, conf.Options.TargetPasswordRaw); err != nil { + conf.Options.TargetAuthType, conf.Options.TargetPasswordRaw); err != nil { return fmt.Errorf("get target redis version failed[%v]", err) } else { if strings.HasPrefix(conf.Options.TargetRedisVersion, "4.") ||