mirror of
https://github.com/hibiken/asynq.git
synced 2024-12-25 23:32:17 +08:00
Sort processes by host and pid in ps output
This commit is contained in:
parent
eadfd5f8b4
commit
3744072e9b
@ -8,6 +8,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -27,11 +28,12 @@ backed by the specified redis instance.
|
|||||||
The command shows the following for each process:
|
The command shows the following for each process:
|
||||||
* Host and PID of the process
|
* Host and PID of the process
|
||||||
* Number of active workers out of worker pool
|
* Number of active workers out of worker pool
|
||||||
* Queues configuration
|
* Queue configuration
|
||||||
* State of the process ("running" | "stopped")
|
* State of the worker process ("running" | "stopped")
|
||||||
|
* Time the process was started
|
||||||
|
|
||||||
A "running" process is processing tasks in queues.
|
A "running" process is processing tasks in queues.
|
||||||
A "stopped" process are no longer processing new tasks.`,
|
A "stopped" process is no longer processing new tasks.`,
|
||||||
Args: cobra.NoArgs,
|
Args: cobra.NoArgs,
|
||||||
Run: ps,
|
Run: ps,
|
||||||
}
|
}
|
||||||
@ -41,12 +43,11 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ps(cmd *cobra.Command, args []string) {
|
func ps(cmd *cobra.Command, args []string) {
|
||||||
c := redis.NewClient(&redis.Options{
|
r := rdb.NewRDB(redis.NewClient(&redis.Options{
|
||||||
Addr: viper.GetString("uri"),
|
Addr: viper.GetString("uri"),
|
||||||
DB: viper.GetInt("db"),
|
DB: viper.GetInt("db"),
|
||||||
Password: viper.GetString("password"),
|
Password: viper.GetString("password"),
|
||||||
})
|
}))
|
||||||
r := rdb.NewRDB(c)
|
|
||||||
|
|
||||||
processes, err := r.ListProcesses()
|
processes, err := r.ListProcesses()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -57,6 +58,17 @@ func ps(cmd *cobra.Command, args []string) {
|
|||||||
fmt.Println("No processes")
|
fmt.Println("No processes")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// sort by hostname and pid
|
||||||
|
sort.Slice(processes, func(i, j int) bool {
|
||||||
|
x, y := processes[i], processes[j]
|
||||||
|
if x.Host != y.Host {
|
||||||
|
return x.Host < y.Host
|
||||||
|
}
|
||||||
|
return x.PID < y.PID
|
||||||
|
})
|
||||||
|
|
||||||
|
// print processes
|
||||||
cols := []string{"Host", "PID", "State", "Active Workers", "Queues", "Started"}
|
cols := []string{"Host", "PID", "State", "Active Workers", "Queues", "Started"}
|
||||||
printRows := func(w io.Writer, tmpl string) {
|
printRows := func(w io.Writer, tmpl string) {
|
||||||
for _, ps := range processes {
|
for _, ps := range processes {
|
||||||
@ -75,11 +87,28 @@ func timeAgo(since time.Time) string {
|
|||||||
return fmt.Sprintf("%v ago", d)
|
return fmt.Sprintf("%v ago", d)
|
||||||
}
|
}
|
||||||
|
|
||||||
func formatQueues(queues map[string]uint) string {
|
func formatQueues(qmap map[string]uint) string {
|
||||||
|
// sort queues by priority and name
|
||||||
|
type queue struct {
|
||||||
|
name string
|
||||||
|
priority uint
|
||||||
|
}
|
||||||
|
var queues []*queue
|
||||||
|
for qname, p := range qmap {
|
||||||
|
queues = append(queues, &queue{qname, p})
|
||||||
|
}
|
||||||
|
sort.Slice(queues, func(i, j int) bool {
|
||||||
|
x, y := queues[i], queues[j]
|
||||||
|
if x.priority != y.priority {
|
||||||
|
return x.priority > y.priority
|
||||||
|
}
|
||||||
|
return x.name < y.name
|
||||||
|
})
|
||||||
|
|
||||||
var b strings.Builder
|
var b strings.Builder
|
||||||
l := len(queues)
|
l := len(queues)
|
||||||
for qname, p := range queues {
|
for _, q := range queues {
|
||||||
fmt.Fprintf(&b, "%s:%d", qname, p)
|
fmt.Fprintf(&b, "%s:%d", q.name, q.priority)
|
||||||
l--
|
l--
|
||||||
if l > 0 {
|
if l > 0 {
|
||||||
b.WriteString(" ")
|
b.WriteString(" ")
|
||||||
|
Loading…
Reference in New Issue
Block a user