diff --git a/cmd/asynqmon/main.go b/cmd/asynqmon/main.go index d046fbc..0175bd6 100644 --- a/cmd/asynqmon/main.go +++ b/cmd/asynqmon/main.go @@ -83,23 +83,31 @@ func parseFlags(progname string, args []string) (cfg *Config, output string, err return &conf, buf.String(), nil } -// TODO: Write test and refactor this code. +// TODO: Refactor this code! func makeRedisConnOpt(cfg *Config) (asynq.RedisConnOpt, error) { var opts redis.UniversalOptions + sentinel := false if cfg.RedisClusterNodes != "" { opts.Addrs = strings.Split(cfg.RedisClusterNodes, ",") opts.Password = cfg.RedisPassword } else { if cfg.RedisURL != "" { - res, err := redis.ParseURL(cfg.RedisURL) + res, err := asynq.ParseRedisURI(cfg.RedisURL) if err != nil { return nil, err } - opts.Addrs = append(opts.Addrs, res.Addr) - opts.DB = res.DB - opts.Password = res.Password - + switch v := res.(type) { + case asynq.RedisClientOpt: + opts.Addrs = append(opts.Addrs, v.Addr) + opts.DB = v.DB + opts.Password = v.Password + case asynq.RedisFailoverClientOpt: + opts.Addrs = append(opts.Addrs, v.SentinelAddrs...) + opts.SentinelPassword = v.SentinelPassword + opts.MasterName = v.MasterName + sentinel = true + } } else { opts.Addrs = []string{cfg.RedisAddr} opts.DB = cfg.RedisDB @@ -124,6 +132,14 @@ func makeRedisConnOpt(cfg *Config) (asynq.RedisConnOpt, error) { TLSConfig: opts.TLSConfig, }, nil } + if sentinel { + return asynq.RedisFailoverClientOpt{ + MasterName: opts.MasterName, + SentinelAddrs: opts.Addrs, + SentinelPassword: opts.SentinelPassword, + TLSConfig: opts.TLSConfig, + }, nil + } return asynq.RedisClientOpt{ Addr: opts.Addrs[0], DB: opts.DB, diff --git a/cmd/asynqmon/main_test.go b/cmd/asynqmon/main_test.go index f430490..f603fea 100644 --- a/cmd/asynqmon/main_test.go +++ b/cmd/asynqmon/main_test.go @@ -97,6 +97,17 @@ func TestMakeRedisConnOpt(t *testing.T) { Password: "bar", }, }, + { + desc: "With redis-sentinel URL", + cfg: &Config{ + RedisURL: "redis-sentinel://:secretpassword@localhost:5000,localhost:5001,localhost:5002?master=mymaster", + }, + want: asynq.RedisFailoverClientOpt{ + MasterName: "mymaster", + SentinelAddrs: []string{ + "localhost:5000", "localhost:5001", "localhost:5002"}, + }, + }, { desc: "With cluster nodes", cfg: &Config{