From 4ae73abdaaa3a50ffeaaf589df8fa58a8b53354c Mon Sep 17 00:00:00 2001 From: Ken Hibino Date: Sun, 11 Oct 2020 14:35:18 -0700 Subject: [PATCH] Minor update to asynq cron command --- tools/asynq/cmd/cron.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/tools/asynq/cmd/cron.go b/tools/asynq/cmd/cron.go index 4367ffb..9c86bc9 100644 --- a/tools/asynq/cmd/cron.go +++ b/tools/asynq/cmd/cron.go @@ -9,6 +9,7 @@ import ( "io" "os" "sort" + "time" "github.com/spf13/cobra" ) @@ -59,12 +60,30 @@ func cronList(cmd *cobra.Command, args []string) { cols := []string{"EntryID", "Spec", "Type", "Payload", "Options", "Next", "Prev"} printRows := func(w io.Writer, tmpl string) { for _, e := range entries { - fmt.Fprintf(w, tmpl, e.ID, e.Spec, e.Type, e.Payload, e.Opts, e.Next, e.Prev) + fmt.Fprintf(w, tmpl, e.ID, e.Spec, e.Type, e.Payload, e.Opts, + nextEnqueue(e.Next), prevEnqueue(e.Prev)) } } printTable(cols, printRows) } +// Returns a string describing when the next enqueue will happen. +func nextEnqueue(nextEnqueueAt time.Time) string { + d := nextEnqueueAt.Sub(time.Now()).Round(time.Second) + if d < 0 { + return "Now" + } + return fmt.Sprintf("In %v", d) +} + +// Returns a string describing when the previous enqueue was. +func prevEnqueue(prevEnqueuedAt time.Time) string { + if prevEnqueuedAt.IsZero() { + return "N/A" + } + return fmt.Sprintf("%v ago", time.Since(prevEnqueuedAt).Round(time.Second)) +} + func cronHistory(cmd *cobra.Command, args []string) { r := createRDB() for i, entryID := range args {