From d6a5c84dc6cfe6c09cf9a90b7258a8211d28d2f3 Mon Sep 17 00:00:00 2001 From: Ken Hibino Date: Thu, 4 Jun 2020 06:34:15 -0700 Subject: [PATCH] Add pause and unpause command to CLI --- tools/asynq/cmd/pause.go | 47 ++++++++++++++++++++++++++++++++++++++ tools/asynq/cmd/unpause.go | 46 +++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 tools/asynq/cmd/pause.go create mode 100644 tools/asynq/cmd/unpause.go diff --git a/tools/asynq/cmd/pause.go b/tools/asynq/cmd/pause.go new file mode 100644 index 0000000..091783b --- /dev/null +++ b/tools/asynq/cmd/pause.go @@ -0,0 +1,47 @@ +// 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. + +package cmd + +import ( + "fmt" + "os" + + "github.com/go-redis/redis/v7" + "github.com/hibiken/asynq/internal/rdb" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +// pauseCmd represents the pause command +var pauseCmd = &cobra.Command{ + Use: "pause [queue name]", + Short: "Pauses the specified queue", + Long: `Pause (asynq pause) will pause the specified queue. +Asynq servers will not process tasks from paused queues. +Use the "unpause" command to resume a paused queue. + +Example: asynq pause default -> Pause the "default" queue`, + Args: cobra.ExactValidArgs(1), + Run: pause, +} + +func init() { + rootCmd.AddCommand(pauseCmd) +} + +func pause(cmd *cobra.Command, args []string) { + c := redis.NewClient(&redis.Options{ + Addr: viper.GetString("uri"), + DB: viper.GetInt("db"), + Password: viper.GetString("password"), + }) + r := rdb.NewRDB(c) + err := r.Pause(args[0]) + if err != nil { + fmt.Printf("error: %v\n", err) + os.Exit(1) + } + fmt.Printf("Successfully paused queue %q\n", args[0]) +} diff --git a/tools/asynq/cmd/unpause.go b/tools/asynq/cmd/unpause.go new file mode 100644 index 0000000..79e0764 --- /dev/null +++ b/tools/asynq/cmd/unpause.go @@ -0,0 +1,46 @@ +// 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. + +package cmd + +import ( + "fmt" + "os" + + "github.com/go-redis/redis/v7" + "github.com/hibiken/asynq/internal/rdb" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +// unpauseCmd represents the unpause command +var unpauseCmd = &cobra.Command{ + Use: "unpause [queue name]", + Short: "Unpauses the specified queue", + Long: `Unpause (asynq unpause) will unpause the specified queue. +Asynq servers will process tasks from unpaused/resumed queues. + +Example: asynq unpause default -> Resume the "default" queue`, + Args: cobra.ExactValidArgs(1), + Run: unpause, +} + +func init() { + rootCmd.AddCommand(unpauseCmd) +} + +func unpause(cmd *cobra.Command, args []string) { + c := redis.NewClient(&redis.Options{ + Addr: viper.GetString("uri"), + DB: viper.GetInt("db"), + Password: viper.GetString("password"), + }) + r := rdb.NewRDB(c) + err := r.Unpause(args[0]) + if err != nil { + fmt.Printf("error: %v\n", err) + os.Exit(1) + } + fmt.Printf("Successfully resumed queue %q\n", args[0]) +}