package queues import ( "encoding/json" "fmt" "strings" "xcecli/common/request" "xcecli/config" "xcecli/models" "github.com/spf13/cobra" ) // QueueController queue controller type QueueController struct { configManager *config.ConfigManager action string queueNames []string comment string allQueues bool } // NewQueueController create queue controller func NewQueueController(configManager *config.ConfigManager, action string) *QueueController { return &QueueController{ configManager: configManager, action: action, queueNames: []string{}, } } // Run execute queue control func (c *QueueController) RunQueuesControl() error { // get server info and verify authentication serverInfo, _ := c.configManager.GetServerInfo() // create API client apiClient := request.NewAPIClient(serverInfo.URL) // handle all queues case if c.allQueues { c.queueNames = []string{"all"} } // if no queue names are specified, return an error if len(c.queueNames) == 0 { return fmt.Errorf("must specify queue names or use --all option") } // create request req := models.QueueControlRequest{ ControlAction: c.action, QueueName: c.queueNames, Comment: c.comment, } // convert request to JSON bytes reqData, err := json.Marshal(req) if err != nil { errMsg := request.ExtractErrorMessage(err) return fmt.Errorf("failed to encode request data: [%s]", errMsg) } // send request var resp models.QueueControlResponse err = apiClient.PostRequestToAPIServer( serverInfo.Token, "/xce/v1/queues/control", reqData, &resp, ) if err != nil { return err } if len(c.queueNames) == 1 && c.queueNames[0] == "all" { for _, data := range resp.Data.Accepted { fmt.Printf("queue <%s> is being <%s>\n", data.QueueName, c.action) } for _, queue := range resp.Data.Failed { fmt.Printf("queue <%s> cannot be <%s> for reason <%s>\n", queue.QueueName, c.action, queue.Msg) } } else { for _, data := range resp.Data.Accepted { fmt.Printf("queue <%s> is being <%s>\n", data.QueueName, c.action) } for _, queue := range resp.Data.Failed { fmt.Printf("queue <%s> cannot be <%s> for reason <%s>\n", queue.QueueName, c.action, queue.Msg) } } return nil } // Parse parse command line arguments func (c *QueueController) ParseCmdArgs(cmd *cobra.Command, args []string) error { // handle queue name parameters if len(args) > 0 { // Split comma-separated queue names for _, arg := range args { queues := strings.Split(arg, ",") c.queueNames = append(c.queueNames, queues...) } } return nil } // NewOpenQueuesCmd create open queues command func NewOpenQueuesCmd(configManager *config.ConfigManager) *cobra.Command { controller := NewQueueController(configManager, "open") cmd := &cobra.Command{ Use: "open", Short: "Open queues", Long: `Open queues by queue name. Opens queues to accept new job submissions. Examples: xce queues open normal priority # open specific queues xce queues open --all # open all queues`, RunE: func(cmd *cobra.Command, args []string) error { if err := controller.ParseCmdArgs(cmd, args); err != nil { fmt.Println(strings.ToLower(err.Error())) return nil } if err := controller.RunQueuesControl(); err != nil { fmt.Println(strings.ToLower(err.Error())) return nil } return nil }, } // add control options flags := cmd.Flags() flags.BoolVarP(&controller.allQueues, "all", "a", false, "apply to all queues") flags.StringVarP(&controller.comment, "comment", "c", "", "add comment for this operation") return cmd } // NewCloseQueuesCmd create close queues command func NewCloseQueuesCmd(configManager *config.ConfigManager) *cobra.Command { controller := NewQueueController(configManager, "close") cmd := &cobra.Command{ Use: "close", Short: "Close queues", Long: `Close queues by queue name. Closes queues to prevent new job submissions. Examples: xce queues close normal priority # close specific queues xce queues close --all # close all queues`, RunE: func(cmd *cobra.Command, args []string) error { if err := controller.ParseCmdArgs(cmd, args); err != nil { fmt.Println(strings.ToLower(err.Error())) return nil } if err := controller.RunQueuesControl(); err != nil { fmt.Println(strings.ToLower(err.Error())) return nil } return nil }, } // add control options flags := cmd.Flags() flags.BoolVarP(&controller.allQueues, "all", "a", false, "apply to all queues") flags.StringVarP(&controller.comment, "comment", "c", "", "add comment for this operation") return cmd } // NewActiveQueuesCmd create active queues command func NewActiveQueuesCmd(configManager *config.ConfigManager) *cobra.Command { controller := NewQueueController(configManager, "active") cmd := &cobra.Command{ Use: "active", Short: "Active queues", Long: `Active queues by queue name. Examples: xce queues active # queues_name status close/d`, RunE: func(cmd *cobra.Command, args []string) error { if err := controller.ParseCmdArgs(cmd, args); err != nil { fmt.Println(strings.ToLower(err.Error())) return nil } if err := controller.RunQueuesControl(); err != nil { fmt.Println(strings.ToLower(err.Error())) return nil } return nil }, } // add control options flags := cmd.Flags() flags.BoolVarP(&controller.allQueues, "all", "a", false, "apply to all queues") flags.StringVarP(&controller.comment, "comment", "c", "", "add comment for this operation") return cmd } func NewDeactiveQueuesCmd(configManager *config.ConfigManager) *cobra.Command { controller := NewQueueController(configManager, "deactive") cmd := &cobra.Command{ Use: "deactive", Short: "Deactive queues", Long: `Deactive queues by queue name. Examples: xce queues deactive # queues_name status close/d`, RunE: func(cmd *cobra.Command, args []string) error { if err := controller.ParseCmdArgs(cmd, args); err != nil { fmt.Println(strings.ToLower(err.Error())) return nil } if err := controller.RunQueuesControl(); err != nil { fmt.Println(strings.ToLower(err.Error())) return nil } return nil }, } // add control options flags := cmd.Flags() flags.BoolVarP(&controller.allQueues, "all", "a", false, "apply to all queues") flags.StringVarP(&controller.comment, "comment", "c", "", "add comment for this operation") return cmd }