diff --git a/asynq.go b/asynq.go index 59409d6..c3cc28e 100644 --- a/asynq.go +++ b/asynq.go @@ -4,7 +4,6 @@ import "github.com/go-redis/redis/v7" /* TODOs: -- [P0] asynqmon kill , asynqmon killall - [P0] Pagination for `asynqmon ls` command - [P0] Show elapsed time for InProgress tasks (asynqmon ls inprogress) - [P0] Go docs + CONTRIBUTION.md + Github issue template + License comment diff --git a/tools/asynqmon/cmd/kill.go b/tools/asynqmon/cmd/kill.go new file mode 100644 index 0000000..eb3f1d0 --- /dev/null +++ b/tools/asynqmon/cmd/kill.go @@ -0,0 +1,66 @@ +package cmd + +import ( + "fmt" + "os" + + "github.com/go-redis/redis/v7" + "github.com/hibiken/asynq/internal/rdb" + "github.com/spf13/cobra" +) + +// killCmd represents the kill command +var killCmd = &cobra.Command{ + Use: "kill [task id]", + Short: "Sends a task to dead queue given an identifier", + Long: `Kill (asynqmon kill) will send a task to dead queue given an identifier. + +The command takes one argument which specifies the task to kill. +The task should be in either scheduled or retry queue. +Identifier for a task should be obtained by running "asynqmon ls" command. + +Example: asynqmon kill r:1575732274:bnogo8gt6toe23vhef0g`, + Args: cobra.ExactArgs(1), + Run: kill, +} + +func init() { + rootCmd.AddCommand(killCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // killCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // killCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} + +func kill(cmd *cobra.Command, args []string) { + id, score, qtype, err := parseQueryID(args[0]) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + r := rdb.NewRDB(redis.NewClient(&redis.Options{ + Addr: uri, + DB: db, + })) + switch qtype { + case "s": + err = r.KillScheduledTask(id, score) + case "r": + err = r.KillRetryTask(id, score) + default: + fmt.Println("invalid argument") + os.Exit(1) + } + if err != nil { + fmt.Println(err) + os.Exit(1) + } + fmt.Printf("Successfully killed %v\n", args[0]) + +} diff --git a/tools/asynqmon/cmd/killall.go b/tools/asynqmon/cmd/killall.go new file mode 100644 index 0000000..e981893 --- /dev/null +++ b/tools/asynqmon/cmd/killall.go @@ -0,0 +1,64 @@ +package cmd + +import ( + "fmt" + "os" + + "github.com/go-redis/redis/v7" + "github.com/hibiken/asynq/internal/rdb" + "github.com/spf13/cobra" +) + +var killallValidArgs = []string{"scheduled", "retry"} + +// killallCmd represents the killall command +var killallCmd = &cobra.Command{ + Use: "killall [queue name]", + Short: "Sends all tasks to dead queue from the specified queue", + Long: `Killall (asynqmon killall) will moves all tasks from the specified queue to dead queue. + +The argument should be either "scheduled" or "retry". + +Example: asynqmon killall retry -> Moves all tasks from retry queue to dead queue`, + ValidArgs: killallValidArgs, + Args: cobra.ExactValidArgs(1), + Run: killall, +} + +func init() { + rootCmd.AddCommand(killallCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // killallCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // killallCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} + +func killall(cmd *cobra.Command, args []string) { + c := redis.NewClient(&redis.Options{ + Addr: uri, + DB: db, + }) + r := rdb.NewRDB(c) + var n int64 + var err error + switch args[0] { + case "scheduled": + n, err = r.KillAllScheduledTasks() + case "retry": + n, err = r.KillAllRetryTasks() + default: + fmt.Printf("error: `asynqmon killall [queue name]` only accepts %v as the argument.\n", killallValidArgs) + os.Exit(1) + } + if err != nil { + fmt.Println(err) + os.Exit(1) + } + fmt.Printf("Sent %d tasks to \"dead\" queue from %q queue\n", n, args[0]) +}