Skip to content

Commit

Permalink
[#1967]: feature: show RPC error message in terminal instead of failing
Browse files Browse the repository at this point in the history
  • Loading branch information
rustatian authored Jul 11, 2024
2 parents d80c68c + abaa6ab commit 60761af
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 25 deletions.
27 changes: 13 additions & 14 deletions internal/cli/workers/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
}
},
Expand All @@ -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,
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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
}
49 changes: 38 additions & 11 deletions internal/cli/workers/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
Expand All @@ -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)),
Expand Down Expand Up @@ -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"})
Expand All @@ -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),
Expand Down

0 comments on commit 60761af

Please sign in to comment.