2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-09-20 19:06:46 +08:00
asynq/tools/asynqmon/cmd/stats.go

74 lines
1.9 KiB
Go
Raw Normal View History

2019-12-06 22:51:55 +08:00
package cmd
import (
"fmt"
"log"
"os"
"strings"
"text/tabwriter"
"github.com/go-redis/redis/v7"
"github.com/hibiken/asynq/internal/rdb"
"github.com/spf13/cobra"
)
2019-12-06 23:36:08 +08:00
// Flags
var uri string
var db int
2019-12-06 22:51:55 +08:00
// statsCmd represents the stats command
var statsCmd = &cobra.Command{
Use: "stats",
Short: "Shows current state of the queues",
Long: `Stats command shows the number of tasks in each queue at that instant.
To monitor the queues continuously, it's recommended that you run this
command in conjunction with the watch command.
Example: watch -n 5 asynqmon stats`,
Run: func(cmd *cobra.Command, args []string) {
stats(cmd, args)
},
}
func init() {
rootCmd.AddCommand(statsCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// statsCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// statsCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
2019-12-06 23:36:08 +08:00
statsCmd.Flags().StringVarP(&uri, "uri", "u", "127.0.0.1:6379", "Redis server URI")
statsCmd.Flags().IntVarP(&db, "db", "n", 0, "Redis database number")
2019-12-06 22:51:55 +08:00
}
func stats(cmd *cobra.Command, args []string) {
c := redis.NewClient(&redis.Options{
2019-12-06 23:36:08 +08:00
Addr: uri,
DB: db,
2019-12-06 22:51:55 +08:00
})
r := rdb.NewRDB(c)
stats, err := r.CurrentStats()
if err != nil {
log.Fatal(err)
}
printStats(stats)
fmt.Println()
}
func printStats(s *rdb.Stats) {
format := strings.Repeat("%v\t", 5) + "\n"
tw := new(tabwriter.Writer).Init(os.Stdout, 0, 8, 2, ' ', 0)
fmt.Fprintf(tw, format, "Enqueued", "InProgress", "Scheduled", "Retry", "Dead")
fmt.Fprintf(tw, format, "--------", "----------", "---------", "-----", "----")
fmt.Fprintf(tw, format, s.Enqueued, s.InProgress, s.Scheduled, s.Retry, s.Dead)
tw.Flush()
}