2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-12-27 00:02:19 +08:00
asynq/tools/asynqmon/cmd/enq.go

75 lines
1.9 KiB
Go
Raw Normal View History

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-09 08:36:08 +08:00
package cmd
import (
"fmt"
2019-12-09 22:22:08 +08:00
"os"
2019-12-09 08:36:08 +08:00
"github.com/go-redis/redis/v7"
"github.com/hibiken/asynq/internal/rdb"
"github.com/spf13/cobra"
)
// enqCmd represents the enq command
var enqCmd = &cobra.Command{
2019-12-11 13:38:25 +08:00
Use: "enq [task id]",
2019-12-09 08:36:08 +08:00
Short: "Enqueues a task given an identifier",
2019-12-11 13:38:25 +08:00
Long: `Enq (asynqmon enq) will enqueue a task given an identifier.
2019-12-09 08:36:08 +08:00
The command takes one argument which specifies the task to enqueue.
The task should be in either scheduled, retry or dead queue.
2019-12-09 08:36:08 +08:00
Identifier for a task should be obtained by running "asynqmon ls" command.
2019-12-09 08:36:08 +08:00
The task enqueued by this command will be processed as soon as the task
gets dequeued by a processor.
Example: asynqmon enq d:1575732274:bnogo8gt6toe23vhef0g`,
2019-12-09 08:36:08 +08:00
Args: cobra.ExactArgs(1),
Run: enq,
}
func init() {
rootCmd.AddCommand(enqCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// enqCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// enqCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
func enq(cmd *cobra.Command, args []string) {
id, score, qtype, err := parseQueryID(args[0])
if err != nil {
2019-12-09 22:22:08 +08:00
fmt.Println(err)
os.Exit(1)
2019-12-09 08:36:08 +08:00
}
r := rdb.NewRDB(redis.NewClient(&redis.Options{
Addr: uri,
DB: db,
}))
switch qtype {
case "s":
2019-12-10 12:37:30 +08:00
err = r.EnqueueScheduledTask(id, score)
2019-12-09 08:36:08 +08:00
case "r":
2019-12-10 12:37:30 +08:00
err = r.EnqueueRetryTask(id, score)
2019-12-09 08:36:08 +08:00
case "d":
2019-12-10 12:37:30 +08:00
err = r.EnqueueDeadTask(id, score)
2019-12-09 08:36:08 +08:00
default:
2019-12-09 22:22:08 +08:00
fmt.Println("invalid argument")
os.Exit(1)
2019-12-09 08:36:08 +08:00
}
if err != nil {
2019-12-09 22:22:08 +08:00
fmt.Println(err)
os.Exit(1)
2019-12-09 08:36:08 +08:00
}
fmt.Printf("Successfully enqueued %v\n", args[0])
}