2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-12-26 07:42:17 +08:00

Rename cli subcommand to not to use dash

This commit is contained in:
Ken Hibino 2021-05-24 16:14:36 -07:00
parent 709ca79a2b
commit 7776c7ae53
2 changed files with 28 additions and 25 deletions

View File

@ -140,31 +140,31 @@ func queueInspect(cmd *cobra.Command, args []string) {
} }
} }
func printQueueInfo(s *asynq.QueueInfo) { func printQueueInfo(info *asynq.QueueInfo) {
bold := color.New(color.Bold) bold := color.New(color.Bold)
bold.Println("Queue Info") bold.Println("Queue Info")
fmt.Printf("Name: %s\n", s.Queue) fmt.Printf("Name: %s\n", info.Queue)
fmt.Printf("Size: %d\n", s.Size) fmt.Printf("Size: %d\n", info.Size)
fmt.Printf("Paused: %t\n\n", s.Paused) fmt.Printf("Paused: %t\n\n", info.Paused)
bold.Println("Task Count by State") bold.Println("Task Count by State")
printTable( printTable(
[]string{"active", "pending", "scheduled", "retry", "archived"}, []string{"active", "pending", "scheduled", "retry", "archived"},
func(w io.Writer, tmpl string) { func(w io.Writer, tmpl string) {
fmt.Fprintf(w, tmpl, s.Active, s.Pending, s.Scheduled, s.Retry, s.Archived) fmt.Fprintf(w, tmpl, info.Active, info.Pending, info.Scheduled, info.Retry, info.Archived)
}, },
) )
fmt.Println() fmt.Println()
bold.Printf("Daily Stats %s UTC\n", s.Timestamp.UTC().Format("2006-01-02")) bold.Printf("Daily Stats %s UTC\n", info.Timestamp.UTC().Format("2006-01-02"))
printTable( printTable(
[]string{"processed", "failed", "error rate"}, []string{"processed", "failed", "error rate"},
func(w io.Writer, tmpl string) { func(w io.Writer, tmpl string) {
var errRate string var errRate string
if s.Processed == 0 { if info.Processed == 0 {
errRate = "N/A" errRate = "N/A"
} else { } else {
errRate = fmt.Sprintf("%.2f%%", float64(s.Failed)/float64(s.Processed)*100) errRate = fmt.Sprintf("%.2f%%", float64(info.Failed)/float64(info.Processed)*100)
} }
fmt.Fprintf(w, tmpl, s.Processed, s.Failed, errRate) fmt.Fprintf(w, tmpl, info.Processed, info.Failed, errRate)
}, },
) )
} }

View File

@ -35,19 +35,19 @@ func init() {
taskCmd.AddCommand(taskArchiveCmd) taskCmd.AddCommand(taskArchiveCmd)
taskArchiveCmd.Flags().StringP("queue", "q", "", "queue to which the task belongs") taskArchiveCmd.Flags().StringP("queue", "q", "", "queue to which the task belongs")
taskArchiveCmd.Flags().StringP("id", "t", "", "id of the task") taskArchiveCmd.Flags().StringP("id", "i", "", "id of the task")
taskArchiveCmd.MarkFlagRequired("queue") taskArchiveCmd.MarkFlagRequired("queue")
taskArchiveCmd.MarkFlagRequired("id") taskArchiveCmd.MarkFlagRequired("id")
taskCmd.AddCommand(taskDeleteCmd) taskCmd.AddCommand(taskDeleteCmd)
taskDeleteCmd.Flags().StringP("queue", "q", "", "queue to which the task belongs") taskDeleteCmd.Flags().StringP("queue", "q", "", "queue to which the task belongs")
taskDeleteCmd.Flags().StringP("id", "t", "", "id of the task") taskDeleteCmd.Flags().StringP("id", "i", "", "id of the task")
taskDeleteCmd.MarkFlagRequired("queue") taskDeleteCmd.MarkFlagRequired("queue")
taskDeleteCmd.MarkFlagRequired("id") taskDeleteCmd.MarkFlagRequired("id")
taskCmd.AddCommand(taskRunCmd) taskCmd.AddCommand(taskRunCmd)
taskRunCmd.Flags().StringP("queue", "q", "", "queue to which the task belongs") taskRunCmd.Flags().StringP("queue", "q", "", "queue to which the task belongs")
taskRunCmd.Flags().StringP("id", "t", "", "id of the task") taskRunCmd.Flags().StringP("id", "i", "", "id of the task")
taskRunCmd.MarkFlagRequired("queue") taskRunCmd.MarkFlagRequired("queue")
taskRunCmd.MarkFlagRequired("id") taskRunCmd.MarkFlagRequired("id")
@ -136,21 +136,21 @@ var taskRunCmd = &cobra.Command{
} }
var taskArchiveAllCmd = &cobra.Command{ var taskArchiveAllCmd = &cobra.Command{
Use: "archive-all --queue=QUEUE --state=STATE", Use: "archiveall --queue=QUEUE --state=STATE",
Short: "Archive all tasks in the given state", Short: "Archive all tasks in the given state",
Args: cobra.NoArgs, Args: cobra.NoArgs,
Run: taskArchiveAll, Run: taskArchiveAll,
} }
var taskDeleteAllCmd = &cobra.Command{ var taskDeleteAllCmd = &cobra.Command{
Use: "delete-all --queue=QUEUE --state=STATE", Use: "deleteall --queue=QUEUE --state=STATE",
Short: "Delete all tasks in the given state", Short: "Delete all tasks in the given state",
Args: cobra.NoArgs, Args: cobra.NoArgs,
Run: taskDeleteAll, Run: taskDeleteAll,
} }
var taskRunAllCmd = &cobra.Command{ var taskRunAllCmd = &cobra.Command{
Use: "run-all --queue=QUEUE --state=STATE", Use: "runall --queue=QUEUE --state=STATE",
Short: "Run all tasks in the given state", Short: "Run all tasks in the given state",
Args: cobra.NoArgs, Args: cobra.NoArgs,
Run: taskRunAll, Run: taskRunAll,
@ -252,14 +252,23 @@ func listScheduledTasks(qname string, pageNum, pageSize int) {
[]string{"ID", "Type", "Payload", "Process In"}, []string{"ID", "Type", "Payload", "Process In"},
func(w io.Writer, tmpl string) { func(w io.Writer, tmpl string) {
for _, t := range tasks { for _, t := range tasks {
processIn := fmt.Sprintf("%.0f seconds", fmt.Fprintf(w, tmpl, t.ID(), t.Type(), t.Payload(), formatProcessAt(t.NextProcessAt()))
t.NextProcessAt().Sub(time.Now()).Seconds())
fmt.Fprintf(w, tmpl, t.ID(), t.Type(), t.Payload(), processIn)
} }
}, },
) )
} }
// formatProcessAt formats next process at time to human friendly string.
// If processAt time is in the past, returns "right now".
// If processAt time is in the future, returns "in xxx" where xxx is the duration from now.
func formatProcessAt(processAt time.Time) string {
d := processAt.Sub(time.Now())
if d < 0 {
return "right now"
}
return fmt.Sprintf("in %v", d.Round(time.Second))
}
func listRetryTasks(qname string, pageNum, pageSize int) { func listRetryTasks(qname string, pageNum, pageSize int) {
i := createInspector() i := createInspector()
tasks, err := i.ListRetryTasks(qname, asynq.PageSize(pageSize), asynq.Page(pageNum)) tasks, err := i.ListRetryTasks(qname, asynq.PageSize(pageSize), asynq.Page(pageNum))
@ -275,13 +284,7 @@ func listRetryTasks(qname string, pageNum, pageSize int) {
[]string{"ID", "Type", "Payload", "Next Retry", "Last Error", "Retried", "Max Retry"}, []string{"ID", "Type", "Payload", "Next Retry", "Last Error", "Retried", "Max Retry"},
func(w io.Writer, tmpl string) { func(w io.Writer, tmpl string) {
for _, t := range tasks { for _, t := range tasks {
var nextRetry string fmt.Fprintf(w, tmpl, t.ID(), t.Type(), t.Payload(), formatProcessAt(t.NextProcessAt()), t.LastErr(), t.Retried(), t.MaxRetry())
if d := t.NextProcessAt().Sub(time.Now()); d > 0 {
nextRetry = fmt.Sprintf("in %v", d.Round(time.Second))
} else {
nextRetry = "right now"
}
fmt.Fprintf(w, tmpl, t.ID(), t.Type(), t.Payload(), nextRetry, t.LastErr(), t.Retried(), t.MaxRetry())
} }
}, },
) )