2020-01-03 10:13:16 +08:00
|
|
|
// Copyright 2020 Kentaro Hibino. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
2019-12-07 23:48:11 +08:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2019-12-09 08:36:08 +08:00
|
|
|
"strconv"
|
2019-12-07 23:48:11 +08:00
|
|
|
"strings"
|
|
|
|
"text/tabwriter"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-redis/redis/v7"
|
|
|
|
"github.com/hibiken/asynq/internal/rdb"
|
2019-12-11 23:38:24 +08:00
|
|
|
"github.com/rs/xid"
|
2019-12-07 23:48:11 +08:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2019-12-11 13:38:25 +08:00
|
|
|
var lsValidArgs = []string{"enqueued", "inprogress", "scheduled", "retry", "dead"}
|
2019-12-07 23:48:11 +08:00
|
|
|
|
|
|
|
// lsCmd represents the ls command
|
|
|
|
var lsCmd = &cobra.Command{
|
2020-01-11 13:32:15 +08:00
|
|
|
Use: "ls [task state]",
|
|
|
|
Short: "Lists tasks in the specified state",
|
|
|
|
Long: `Ls (asynqmon ls) will list all tasks in the specified state in a table format.
|
2019-12-07 23:48:11 +08:00
|
|
|
|
2020-01-11 13:32:15 +08:00
|
|
|
The command takes one argument which specifies the state of tasks.
|
|
|
|
The argument value should be one of "enqueued", "inprogress", "scheduled",
|
2019-12-07 23:48:11 +08:00
|
|
|
"retry", or "dead".
|
|
|
|
|
2020-01-11 13:32:15 +08:00
|
|
|
Example:
|
|
|
|
asynqmon ls dead -> Lists all tasks in dead state
|
|
|
|
|
|
|
|
Enqueued tasks can optionally be filtered by providing queue names after ":"
|
|
|
|
Example:
|
|
|
|
asynqmon ls enqueued:critical -> List tasks from critical queue only
|
|
|
|
`,
|
|
|
|
Args: cobra.ExactValidArgs(1),
|
|
|
|
Run: ls,
|
2019-12-07 23:48:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(lsCmd)
|
|
|
|
|
|
|
|
// Here you will define your flags and configuration settings.
|
|
|
|
|
|
|
|
// Cobra supports Persistent Flags which will work for this command
|
|
|
|
// and all subcommands, e.g.:
|
|
|
|
// lsCmd.PersistentFlags().String("foo", "", "A help for foo")
|
|
|
|
|
|
|
|
// Cobra supports local flags which will only run when this command
|
|
|
|
// is called directly, e.g.:
|
|
|
|
// lsCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
|
|
|
}
|
|
|
|
|
|
|
|
func ls(cmd *cobra.Command, args []string) {
|
|
|
|
c := redis.NewClient(&redis.Options{
|
|
|
|
Addr: uri,
|
|
|
|
DB: db,
|
|
|
|
})
|
|
|
|
r := rdb.NewRDB(c)
|
2020-01-11 13:32:15 +08:00
|
|
|
parts := strings.Split(args[0], ":")
|
|
|
|
switch parts[0] {
|
2019-12-07 23:48:11 +08:00
|
|
|
case "enqueued":
|
2020-01-11 13:32:15 +08:00
|
|
|
listEnqueued(r, parts[1:]...)
|
2019-12-07 23:48:11 +08:00
|
|
|
case "inprogress":
|
|
|
|
listInProgress(r)
|
|
|
|
case "scheduled":
|
|
|
|
listScheduled(r)
|
|
|
|
case "retry":
|
|
|
|
listRetry(r)
|
|
|
|
case "dead":
|
|
|
|
listDead(r)
|
|
|
|
default:
|
2020-01-11 13:32:15 +08:00
|
|
|
fmt.Printf("error: `asynqmon ls [task state]` only accepts %v as the argument.\n", lsValidArgs)
|
2019-12-11 13:38:25 +08:00
|
|
|
os.Exit(1)
|
2019-12-07 23:48:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-09 08:36:08 +08:00
|
|
|
// queryID returns an identifier used for "enq" command.
|
|
|
|
// score is the zset score and queryType should be one
|
|
|
|
// of "s", "r" or "d" (scheduled, retry, dead respectively).
|
2019-12-11 23:38:24 +08:00
|
|
|
func queryID(id xid.ID, score int64, qtype string) string {
|
2019-12-09 08:36:08 +08:00
|
|
|
const format = "%v:%v:%v"
|
|
|
|
return fmt.Sprintf(format, qtype, score, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseQueryID is a reverse operation of queryID function.
|
|
|
|
// It takes a queryID and return each part of id with proper
|
|
|
|
// type if valid, otherwise it reports an error.
|
2019-12-11 23:38:24 +08:00
|
|
|
func parseQueryID(queryID string) (id xid.ID, score int64, qtype string, err error) {
|
2019-12-09 08:36:08 +08:00
|
|
|
parts := strings.Split(queryID, ":")
|
|
|
|
if len(parts) != 3 {
|
2019-12-11 23:38:24 +08:00
|
|
|
return xid.NilID(), 0, "", fmt.Errorf("invalid id")
|
2019-12-09 08:36:08 +08:00
|
|
|
}
|
2019-12-11 23:38:24 +08:00
|
|
|
id, err = xid.FromString(parts[2])
|
2019-12-09 08:36:08 +08:00
|
|
|
if err != nil {
|
2019-12-11 23:38:24 +08:00
|
|
|
return xid.NilID(), 0, "", fmt.Errorf("invalid id")
|
2019-12-09 08:36:08 +08:00
|
|
|
}
|
2019-12-10 12:37:30 +08:00
|
|
|
score, err = strconv.ParseInt(parts[1], 10, 64)
|
2019-12-09 08:36:08 +08:00
|
|
|
if err != nil {
|
2019-12-11 23:38:24 +08:00
|
|
|
return xid.NilID(), 0, "", fmt.Errorf("invalid id")
|
2019-12-09 08:36:08 +08:00
|
|
|
}
|
|
|
|
qtype = parts[0]
|
|
|
|
if len(qtype) != 1 || !strings.Contains("srd", qtype) {
|
2019-12-11 23:38:24 +08:00
|
|
|
return xid.NilID(), 0, "", fmt.Errorf("invalid id")
|
2019-12-09 08:36:08 +08:00
|
|
|
}
|
|
|
|
return id, score, qtype, nil
|
|
|
|
}
|
|
|
|
|
2020-01-11 13:32:15 +08:00
|
|
|
func listEnqueued(r *rdb.RDB, qnames ...string) {
|
|
|
|
tasks, err := r.ListEnqueued(qnames...)
|
2019-12-07 23:48:11 +08:00
|
|
|
if err != nil {
|
2019-12-09 22:22:08 +08:00
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
2019-12-07 23:48:11 +08:00
|
|
|
}
|
|
|
|
if len(tasks) == 0 {
|
2020-01-11 13:32:15 +08:00
|
|
|
msg := "No enqueued tasks"
|
|
|
|
if len(qnames) > 0 {
|
|
|
|
msg += " in"
|
|
|
|
for i, q := range qnames {
|
|
|
|
msg += fmt.Sprintf(" %q queue", q)
|
|
|
|
if i != len(qnames)-1 {
|
|
|
|
msg += ","
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Println(msg)
|
2019-12-07 23:48:11 +08:00
|
|
|
return
|
|
|
|
}
|
2020-01-10 23:06:26 +08:00
|
|
|
cols := []string{"ID", "Type", "Payload", "Queue"}
|
2019-12-07 23:48:11 +08:00
|
|
|
printRows := func(w io.Writer, tmpl string) {
|
|
|
|
for _, t := range tasks {
|
2020-01-10 23:06:26 +08:00
|
|
|
fmt.Fprintf(w, tmpl, t.ID, t.Type, t.Payload, t.Queue)
|
2019-12-07 23:48:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
printTable(cols, printRows)
|
|
|
|
}
|
|
|
|
|
|
|
|
func listInProgress(r *rdb.RDB) {
|
|
|
|
tasks, err := r.ListInProgress()
|
|
|
|
if err != nil {
|
2019-12-09 22:22:08 +08:00
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
2019-12-07 23:48:11 +08:00
|
|
|
}
|
|
|
|
if len(tasks) == 0 {
|
|
|
|
fmt.Println("No in-progress tasks")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cols := []string{"ID", "Type", "Payload"}
|
|
|
|
printRows := func(w io.Writer, tmpl string) {
|
|
|
|
for _, t := range tasks {
|
|
|
|
fmt.Fprintf(w, tmpl, t.ID, t.Type, t.Payload)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printTable(cols, printRows)
|
|
|
|
}
|
|
|
|
|
|
|
|
func listScheduled(r *rdb.RDB) {
|
|
|
|
tasks, err := r.ListScheduled()
|
|
|
|
if err != nil {
|
2019-12-09 22:22:08 +08:00
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
2019-12-07 23:48:11 +08:00
|
|
|
}
|
|
|
|
if len(tasks) == 0 {
|
|
|
|
fmt.Println("No scheduled tasks")
|
|
|
|
return
|
|
|
|
}
|
2020-01-10 23:06:26 +08:00
|
|
|
cols := []string{"ID", "Type", "Payload", "Process In", "Queue"}
|
2019-12-07 23:48:11 +08:00
|
|
|
printRows := func(w io.Writer, tmpl string) {
|
|
|
|
for _, t := range tasks {
|
|
|
|
processIn := fmt.Sprintf("%.0f seconds", t.ProcessAt.Sub(time.Now()).Seconds())
|
2020-01-10 23:06:26 +08:00
|
|
|
fmt.Fprintf(w, tmpl, queryID(t.ID, t.Score, "s"), t.Type, t.Payload, processIn, t.Queue)
|
2019-12-07 23:48:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
printTable(cols, printRows)
|
|
|
|
}
|
|
|
|
|
|
|
|
func listRetry(r *rdb.RDB) {
|
|
|
|
tasks, err := r.ListRetry()
|
|
|
|
if err != nil {
|
2019-12-09 22:22:08 +08:00
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
2019-12-07 23:48:11 +08:00
|
|
|
}
|
|
|
|
if len(tasks) == 0 {
|
|
|
|
fmt.Println("No retry tasks")
|
|
|
|
return
|
|
|
|
}
|
2020-01-10 23:06:26 +08:00
|
|
|
cols := []string{"ID", "Type", "Payload", "Retry In", "Last Error", "Retried", "Max Retry", "Queue"}
|
2019-12-07 23:48:11 +08:00
|
|
|
printRows := func(w io.Writer, tmpl string) {
|
|
|
|
for _, t := range tasks {
|
|
|
|
retryIn := fmt.Sprintf("%.0f seconds", t.ProcessAt.Sub(time.Now()).Seconds())
|
2020-01-10 23:06:26 +08:00
|
|
|
fmt.Fprintf(w, tmpl, queryID(t.ID, t.Score, "r"), t.Type, t.Payload, retryIn, t.ErrorMsg, t.Retried, t.Retry, t.Queue)
|
2019-12-07 23:48:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
printTable(cols, printRows)
|
|
|
|
}
|
|
|
|
|
|
|
|
func listDead(r *rdb.RDB) {
|
|
|
|
tasks, err := r.ListDead()
|
|
|
|
if err != nil {
|
2019-12-09 22:22:08 +08:00
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
2019-12-07 23:48:11 +08:00
|
|
|
}
|
|
|
|
if len(tasks) == 0 {
|
|
|
|
fmt.Println("No dead tasks")
|
|
|
|
return
|
|
|
|
}
|
2020-01-10 23:06:26 +08:00
|
|
|
cols := []string{"ID", "Type", "Payload", "Last Failed", "Last Error", "Queue"}
|
2019-12-07 23:48:11 +08:00
|
|
|
printRows := func(w io.Writer, tmpl string) {
|
|
|
|
for _, t := range tasks {
|
2020-01-10 23:06:26 +08:00
|
|
|
fmt.Fprintf(w, tmpl, queryID(t.ID, t.Score, "d"), t.Type, t.Payload, t.LastFailedAt, t.ErrorMsg, t.Queue)
|
2019-12-07 23:48:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
printTable(cols, printRows)
|
|
|
|
}
|
|
|
|
|
|
|
|
func printTable(cols []string, printRows func(w io.Writer, tmpl string)) {
|
|
|
|
format := strings.Repeat("%v\t", len(cols)) + "\n"
|
|
|
|
tw := new(tabwriter.Writer).Init(os.Stdout, 0, 8, 2, ' ', 0)
|
|
|
|
var headers []interface{}
|
|
|
|
var seps []interface{}
|
|
|
|
for _, name := range cols {
|
|
|
|
headers = append(headers, name)
|
|
|
|
seps = append(seps, strings.Repeat("-", len(name)))
|
|
|
|
}
|
|
|
|
fmt.Fprintf(tw, format, headers...)
|
|
|
|
fmt.Fprintf(tw, format, seps...)
|
|
|
|
printRows(tw, format)
|
|
|
|
tw.Flush()
|
|
|
|
}
|