2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-12-27 08:12:19 +08:00

Display cluster info in stats command when --cluster flag is passed

This commit is contained in:
Ken Hibino 2020-09-07 12:55:32 -07:00
parent 818c2d6f35
commit c26b7469bd
2 changed files with 41 additions and 4 deletions

View File

@ -222,8 +222,21 @@ func (r *RDB) RedisInfo() (map[string]string, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
return parseInfo(res)
}
// RedisClusterInfo returns a map of redis cluster info.
func (r *RDB) RedisClusterInfo() (map[string]string, error) {
res, err := r.client.ClusterInfo().Result()
if err != nil {
return nil, err
}
return parseInfo(res)
}
func parseInfo(infoStr string) (map[string]string, error) {
info := make(map[string]string) info := make(map[string]string)
lines := strings.Split(res, "\r\n") lines := strings.Split(infoStr, "\r\n")
for _, l := range lines { for _, l := range lines {
kv := strings.Split(l, ":") kv := strings.Split(l, ":")
if len(kv) == 2 { if len(kv) == 2 {

View File

@ -6,6 +6,7 @@ package cmd
import ( import (
"fmt" "fmt"
"io"
"os" "os"
"strconv" "strconv"
"strings" "strings"
@ -88,7 +89,12 @@ func stats(cmd *cobra.Command, args []string) {
aggStats.Timestamp = s.Timestamp aggStats.Timestamp = s.Timestamp
stats = append(stats, s) stats = append(stats, s)
} }
info, err := r.RedisInfo() var info map[string]string
if useRedisCluster {
info, err = r.RedisClusterInfo()
} else {
info, err = r.RedisInfo()
}
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)
@ -105,8 +111,13 @@ func stats(cmd *cobra.Command, args []string) {
printSuccessFailureStats(&aggStats) printSuccessFailureStats(&aggStats)
fmt.Println() fmt.Println()
fmt.Println("REDIS INFO") if useRedisCluster {
printInfo(info) fmt.Println("REDIS CLUSTER INFO")
printClusterInfo(info)
} else {
fmt.Println("REDIS INFO")
printInfo(info)
}
fmt.Println() fmt.Println()
} }
@ -174,6 +185,19 @@ func printInfo(info map[string]string) {
tw.Flush() tw.Flush()
} }
func printClusterInfo(info map[string]string) {
printTable(
[]string{"State", "Known Nodes", "Cluster Size"},
func(w io.Writer, tmpl string) {
fmt.Fprintf(w, tmpl,
strings.ToUpper(info["cluster_state"]),
info["cluster_known_nodes"],
info["cluster_size"],
)
},
)
}
func toInterfaceSlice(strs []string) []interface{} { func toInterfaceSlice(strs []string) []interface{} {
var res []interface{} var res []interface{}
for _, s := range strs { for _, s := range strs {