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-12 23:02:14 +08:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/go-redis/redis/v7"
|
|
|
|
"github.com/hibiken/asynq/internal/rdb"
|
|
|
|
"github.com/spf13/cobra"
|
2020-01-20 00:40:51 +08:00
|
|
|
"github.com/spf13/viper"
|
2019-12-12 23:02:14 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var delallValidArgs = []string{"scheduled", "retry", "dead"}
|
|
|
|
|
|
|
|
// delallCmd represents the delall command
|
|
|
|
var delallCmd = &cobra.Command{
|
2020-01-20 07:21:51 +08:00
|
|
|
Use: "delall [state]",
|
2020-02-04 22:16:29 +08:00
|
|
|
Short: "Deletes all tasks in the specified state",
|
2020-01-20 07:21:51 +08:00
|
|
|
Long: `Delall (asynqmon delall) will delete all tasks in the specified state.
|
2019-12-12 23:02:14 +08:00
|
|
|
|
|
|
|
The argument should be one of "scheduled", "retry", or "dead".
|
|
|
|
|
2020-01-20 07:21:51 +08:00
|
|
|
Example: asynqmon delall dead -> Deletes all dead tasks`,
|
2019-12-12 23:02:14 +08:00
|
|
|
ValidArgs: delallValidArgs,
|
|
|
|
Args: cobra.ExactValidArgs(1),
|
|
|
|
Run: delall,
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(delallCmd)
|
|
|
|
|
|
|
|
// Here you will define your flags and configuration settings.
|
|
|
|
|
|
|
|
// Cobra supports Persistent Flags which will work for this command
|
|
|
|
// and all subcommands, e.g.:
|
|
|
|
// delallCmd.PersistentFlags().String("foo", "", "A help for foo")
|
|
|
|
|
|
|
|
// Cobra supports local flags which will only run when this command
|
|
|
|
// is called directly, e.g.:
|
|
|
|
// delallCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
|
|
|
}
|
|
|
|
|
|
|
|
func delall(cmd *cobra.Command, args []string) {
|
|
|
|
c := redis.NewClient(&redis.Options{
|
2020-01-20 00:40:51 +08:00
|
|
|
Addr: viper.GetString("uri"),
|
|
|
|
DB: viper.GetInt("db"),
|
|
|
|
Password: viper.GetString("password"),
|
2019-12-12 23:02:14 +08:00
|
|
|
})
|
|
|
|
r := rdb.NewRDB(c)
|
|
|
|
var err error
|
|
|
|
switch args[0] {
|
|
|
|
case "scheduled":
|
|
|
|
err = r.DeleteAllScheduledTasks()
|
|
|
|
case "retry":
|
|
|
|
err = r.DeleteAllRetryTasks()
|
|
|
|
case "dead":
|
|
|
|
err = r.DeleteAllDeadTasks()
|
|
|
|
default:
|
2020-01-20 07:21:51 +08:00
|
|
|
fmt.Printf("error: `asynqmon delall [state]` only accepts %v as the argument.\n", delallValidArgs)
|
2019-12-12 23:02:14 +08:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-01-20 07:21:51 +08:00
|
|
|
fmt.Printf("Deleted all tasks in %q state\n", args[0])
|
2019-12-12 23:02:14 +08:00
|
|
|
}
|