diff options
author | Valery Piashchynski <[email protected]> | 2024-07-11 17:45:06 +0200 |
---|---|---|
committer | GitHub <[email protected]> | 2024-07-11 17:45:06 +0200 |
commit | 60761af30694544741b5b009cb8ff886180aa7cf (patch) | |
tree | 663ffc47a79f38262a30aa52eb0d1c47461fda18 | |
parent | d80c68c8a41daef46548d44721ca102a1b1c36e6 (diff) | |
parent | abaa6ab13a1f27b099fd8615830257f779a5e17f (diff) |
[#1967]: feature: show RPC error message in terminal instead of failing
-rw-r--r-- | internal/cli/workers/command.go | 27 | ||||
-rw-r--r-- | internal/cli/workers/render.go | 49 |
2 files changed, 51 insertions, 25 deletions
diff --git a/internal/cli/workers/command.go b/internal/cli/workers/command.go index 8ea69600..3c34740e 100644 --- a/internal/cli/workers/command.go +++ b/internal/cli/workers/command.go @@ -8,7 +8,7 @@ import ( "syscall" "time" - "github.com/roadrunner-server/api/v4/plugins/v1/jobs" + "github.com/roadrunner-server/api/v4/plugins/v4/jobs" internalRpc "github.com/roadrunner-server/roadrunner/v2024/internal/rpc" tm "github.com/buger/goterm" @@ -46,12 +46,13 @@ func NewCommand(cfgFile *string, override *[]string) *cobra.Command { //nolint:f plugins := args // by default, we expect a plugin list from user if len(plugins) == 0 { // but if nothing was passed - request all informers list if err = client.Call(informerList, true, &plugins); err != nil { - return err + return fmt.Errorf("failed to get list of plugins: %w", err) } } if !interactive { - return showWorkers(plugins, client) + showWorkers(plugins, client) + return nil } oss := make(chan os.Signal, 1) @@ -71,9 +72,7 @@ func NewCommand(cfgFile *string, override *[]string) *cobra.Command { //nolint:f tm.MoveCursor(1, 1) tm.Flush() - if err = showWorkers(plugins, client); err != nil { - return errors.E(op, err) - } + showWorkers(plugins, client) } } }, @@ -90,9 +89,8 @@ func NewCommand(cfgFile *string, override *[]string) *cobra.Command { //nolint:f return cmd } -func showWorkers(plugins []string, client *rpc.Client) error { +func showWorkers(plugins []string, client *rpc.Client) { const ( - op = errors.Op("show_workers") informerWorkers = "informer.Workers" informerJobs = "informer.Jobs" // this is only one exception to Render the workers, service plugin has the same workers as other plugins, @@ -105,7 +103,9 @@ func showWorkers(plugins []string, client *rpc.Client) error { list := &informer.WorkerList{} if err := client.Call(informerWorkers, plugin, &list); err != nil { - return errors.E(op, err) + // this is a special case, when we can't get workers list, we need to render an error message + WorkerTable(os.Stdout, list.Workers, fmt.Errorf("failed to receive information about %s plugin: %w", plugin, err)).Render() + continue } if len(list.Workers) == 0 { @@ -121,14 +121,15 @@ func showWorkers(plugins []string, client *rpc.Client) error { fmt.Printf("Workers of [%s]:\n", color.HiYellowString(plugin)) - WorkerTable(os.Stdout, list.Workers).Render() + WorkerTable(os.Stdout, list.Workers, nil).Render() } for _, plugin := range plugins { var jst []*jobs.State if err := client.Call(informerJobs, plugin, &jst); err != nil { - return errors.E(op, err) + JobsTable(os.Stdout, jst, fmt.Errorf("failed to receive information about %s plugin: %w", plugin, err)).Render() + continue } // eq to nil @@ -137,8 +138,6 @@ func showWorkers(plugins []string, client *rpc.Client) error { } fmt.Printf("Jobs of [%s]:\n", color.HiYellowString(plugin)) - JobsTable(os.Stdout, jst).Render() + JobsTable(os.Stdout, jst, nil).Render() } - - return nil } diff --git a/internal/cli/workers/render.go b/internal/cli/workers/render.go index e18fb243..1b6a90de 100644 --- a/internal/cli/workers/render.go +++ b/internal/cli/workers/render.go @@ -9,7 +9,7 @@ import ( "github.com/dustin/go-humanize" "github.com/fatih/color" "github.com/olekukonko/tablewriter" - "github.com/roadrunner-server/api/v4/plugins/v1/jobs" + "github.com/roadrunner-server/api/v4/plugins/v4/jobs" "github.com/roadrunner-server/pool/state/process" ) @@ -19,11 +19,7 @@ const ( ) // WorkerTable renders table with information about rr server workers. -func WorkerTable(writer io.Writer, workers []*process.State) *tablewriter.Table { - sort.Slice(workers, func(i, j int) bool { - return workers[i].Pid < workers[j].Pid - }) - +func WorkerTable(writer io.Writer, workers []*process.State, err error) *tablewriter.Table { tw := tablewriter.NewWriter(writer) tw.SetHeader([]string{"PID", "Status", "Execs", "Memory", "CPU%", "Created"}) tw.SetColMinWidth(0, 7) @@ -33,6 +29,23 @@ func WorkerTable(writer io.Writer, workers []*process.State) *tablewriter.Table tw.SetColMinWidth(4, 7) tw.SetColMinWidth(5, 18) + if err != nil { + tw.Append([]string{ + "0", + err.Error(), + "ERROR", + "ERROR", + "ERROR", + "ERROR", + }) + + return tw + } + + sort.Slice(workers, func(i, j int) bool { + return workers[i].Pid < workers[j].Pid + }) + for i := 0; i < len(workers); i++ { tw.Append([]string{ strconv.Itoa(int(workers[i].Pid)), @@ -75,11 +88,7 @@ func ServiceWorkerTable(writer io.Writer, workers []*process.State) *tablewriter } // JobsTable renders table with information about rr server jobs. -func JobsTable(writer io.Writer, jobs []*jobs.State) *tablewriter.Table { - sort.Slice(jobs, func(i, j int) bool { - return jobs[i].Pipeline < jobs[j].Pipeline - }) - +func JobsTable(writer io.Writer, jobs []*jobs.State, err error) *tablewriter.Table { tw := tablewriter.NewWriter(writer) tw.SetAutoWrapText(false) tw.SetHeader([]string{"Status", "Pipeline", "Driver", "Queue", "Active", "Delayed", "Reserved"}) @@ -92,6 +101,24 @@ func JobsTable(writer io.Writer, jobs []*jobs.State) *tablewriter.Table { tw.SetColWidth(10) tw.SetAlignment(tablewriter.ALIGN_LEFT) + if err != nil { + tw.Append([]string{ + err.Error(), + "ERROR", + "ERROR", + "ERROR", + "ERROR", + "ERROR", + "ERROR", + }) + + return tw + } + + sort.Slice(jobs, func(i, j int) bool { + return jobs[i].Pipeline < jobs[j].Pipeline + }) + for i := 0; i < len(jobs); i++ { tw.Append([]string{ renderReady(jobs[i].Ready), |