From d24c43aaeff897394eca140478c73dd146f8710a Mon Sep 17 00:00:00 2001 From: Wolfy-J Date: Wed, 26 Sep 2018 22:18:48 +0300 Subject: worker information extraction is now moved to external package --- cmd/rr/cmd/root.go | 8 +++---- cmd/rr/debug/debugger.go | 28 +++++++++++----------- cmd/rr/http/reset.go | 6 ++--- cmd/rr/http/workers.go | 61 ++---------------------------------------------- cmd/rr/util/cprint.go | 28 ++++++++++++++++++++++ cmd/rr/util/list.go | 58 +++++++++++++++++++++++++++++++++++++++++++++ cmd/rr/utils/cprint.go | 28 ---------------------- 7 files changed, 109 insertions(+), 108 deletions(-) create mode 100644 cmd/rr/util/cprint.go create mode 100644 cmd/rr/util/list.go delete mode 100644 cmd/rr/utils/cprint.go (limited to 'cmd/rr') diff --git a/cmd/rr/cmd/root.go b/cmd/rr/cmd/root.go index 4ab37967..aabfcdea 100644 --- a/cmd/rr/cmd/root.go +++ b/cmd/rr/cmd/root.go @@ -24,7 +24,7 @@ import ( "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/viper" - "github.com/spiral/roadrunner/cmd/rr/utils" + "github.com/spiral/roadrunner/cmd/rr/util" "github.com/spiral/roadrunner/service" "os" "path/filepath" @@ -51,7 +51,7 @@ var ( Use: "rr", SilenceErrors: true, SilenceUsage: true, - Short: utils.Sprintf( + Short: util.Sprintf( "RoadRunner, PHP Application Server:\nVersion: %s, %s", Version, BuildTime, @@ -83,7 +83,7 @@ func (w *ViperWrapper) Unmarshal(out interface{}) error { // This is called by main.main(). It only needs to happen once to the CLI. func Execute() { if err := CLI.Execute(); err != nil { - utils.Printf("Error: %s\n", err) + util.Printf("Error: %s\n", err) os.Exit(1) } } @@ -100,7 +100,7 @@ func init() { if cfg := initConfig(cfgFile, []string{"."}, ".rr"); cfg != nil { if err := Container.Init(cfg); err != nil { - utils.Printf("Error: %s\n", err) + util.Printf("Error: %s\n", err) os.Exit(1) } } diff --git a/cmd/rr/debug/debugger.go b/cmd/rr/debug/debugger.go index ed9a1a56..5b1d586b 100644 --- a/cmd/rr/debug/debugger.go +++ b/cmd/rr/debug/debugger.go @@ -4,7 +4,7 @@ import ( "fmt" "github.com/sirupsen/logrus" "github.com/spiral/roadrunner" - "github.com/spiral/roadrunner/cmd/rr/utils" + "github.com/spiral/roadrunner/cmd/rr/util" rrhttp "github.com/spiral/roadrunner/service/http" "net/http" "strings" @@ -24,7 +24,7 @@ func (s *debugger) listener(event int, ctx interface{}) { switch event { case rrhttp.EventResponse: e := ctx.(*rrhttp.ResponseEvent) - s.logger.Info(utils.Sprintf( + s.logger.Info(util.Sprintf( "%s %s %s %s", e.Request.RemoteAddr, statusColor(e.Response.Status), @@ -35,14 +35,14 @@ func (s *debugger) listener(event int, ctx interface{}) { e := ctx.(*rrhttp.ErrorEvent) if _, ok := e.Error.(roadrunner.JobError); ok { - s.logger.Info(utils.Sprintf( + s.logger.Info(util.Sprintf( "%s %s %s", statusColor(500), e.Request.Method, uri(e.Request), )) } else { - s.logger.Info(utils.Sprintf( + s.logger.Info(util.Sprintf( "%s %s %s %s", statusColor(500), e.Request.Method, @@ -55,13 +55,13 @@ func (s *debugger) listener(event int, ctx interface{}) { switch event { case roadrunner.EventWorkerKill: w := ctx.(*roadrunner.Worker) - s.logger.Warning(utils.Sprintf( + s.logger.Warning(util.Sprintf( "worker.%v killed", *w.Pid, )) case roadrunner.EventWorkerError: err := ctx.(roadrunner.WorkerError) - s.logger.Error(utils.Sprintf( + s.logger.Error(util.Sprintf( "worker.%v %s", *err.Worker.Pid, err.Caused, @@ -71,7 +71,7 @@ func (s *debugger) listener(event int, ctx interface{}) { // outputs switch event { case roadrunner.EventStderrOutput: - s.logger.Warning(utils.Sprintf( + s.logger.Warning(util.Sprintf( "%s", strings.Trim(string(ctx.([]byte)), "\r\n"), )) @@ -80,15 +80,15 @@ func (s *debugger) listener(event int, ctx interface{}) { // rr server events switch event { case roadrunner.EventServerFailure: - s.logger.Error(utils.Sprintf("server is dead")) + s.logger.Error(util.Sprintf("server is dead")) } // pool events switch event { case roadrunner.EventPoolConstruct: - s.logger.Debug(utils.Sprintf("new worker pool")) + s.logger.Debug(util.Sprintf("new worker pool")) case roadrunner.EventPoolError: - s.logger.Error(utils.Sprintf("%s", ctx)) + s.logger.Error(util.Sprintf("%s", ctx)) } //s.logger.Warning(event, ctx) @@ -96,18 +96,18 @@ func (s *debugger) listener(event int, ctx interface{}) { func statusColor(status int) string { if status < 300 { - return utils.Sprintf("%v", status) + return util.Sprintf("%v", status) } if status < 400 { - return utils.Sprintf("%v", status) + return util.Sprintf("%v", status) } if status < 500 { - return utils.Sprintf("%v", status) + return util.Sprintf("%v", status) } - return utils.Sprintf("%v", status) + return util.Sprintf("%v", status) } // uri fetches full uri from request in a form of string (including https scheme if TLS connection is enabled). diff --git a/cmd/rr/http/reset.go b/cmd/rr/http/reset.go index 3bc089ec..f1129eef 100644 --- a/cmd/rr/http/reset.go +++ b/cmd/rr/http/reset.go @@ -24,7 +24,7 @@ import ( "errors" "github.com/spf13/cobra" rr "github.com/spiral/roadrunner/cmd/rr/cmd" - "github.com/spiral/roadrunner/cmd/rr/utils" + "github.com/spiral/roadrunner/cmd/rr/util" "github.com/spiral/roadrunner/service" "github.com/spiral/roadrunner/service/rpc" ) @@ -49,13 +49,13 @@ func reloadHandler(cmd *cobra.Command, args []string) error { } defer client.Close() - utils.Printf("restarting http worker pool: ") + util.Printf("restarting http worker pool: ") var r string if err := client.Call("http.Reset", true, &r); err != nil { return err } - utils.Printf("done\n") + util.Printf("done\n") return nil } diff --git a/cmd/rr/http/workers.go b/cmd/rr/http/workers.go index b03c273f..3c3e9987 100644 --- a/cmd/rr/http/workers.go +++ b/cmd/rr/http/workers.go @@ -23,19 +23,15 @@ package http import ( "errors" tm "github.com/buger/goterm" - "github.com/dustin/go-humanize" - "github.com/olekukonko/tablewriter" - "github.com/shirou/gopsutil/process" "github.com/spf13/cobra" rr "github.com/spiral/roadrunner/cmd/rr/cmd" - "github.com/spiral/roadrunner/cmd/rr/utils" + "github.com/spiral/roadrunner/cmd/rr/util" "github.com/spiral/roadrunner/service" "github.com/spiral/roadrunner/service/http" rrpc "github.com/spiral/roadrunner/service/rpc" "net/rpc" "os" "os/signal" - "strconv" "syscall" "time" ) @@ -108,58 +104,5 @@ func showWorkers(client *rpc.Client) { panic(err) } - tw := tablewriter.NewWriter(os.Stdout) - tw.SetHeader([]string{"PID", "Status", "Execs", "Memory", "Created"}) - tw.SetColMinWidth(0, 7) - tw.SetColMinWidth(1, 9) - tw.SetColMinWidth(2, 7) - tw.SetColMinWidth(3, 7) - tw.SetColMinWidth(4, 18) - - for _, w := range r.Workers { - tw.Append([]string{ - strconv.Itoa(w.Pid), - renderStatus(w.Status), - renderJobs(w.NumJobs), - renderMemory(w.Pid), - renderAlive(time.Unix(0, w.Created)), - }) - } - - tw.Render() -} - -func renderStatus(status string) string { - switch status { - case "inactive": - return utils.Sprintf("inactive") - case "ready": - return utils.Sprintf("ready") - case "working": - return utils.Sprintf("working") - case "stopped": - return utils.Sprintf("stopped") - case "errored": - return utils.Sprintf("errored") - } - - return status -} - -func renderJobs(number int64) string { - return humanize.Comma(int64(number)) -} - -func renderAlive(t time.Time) string { - return humanize.RelTime(t, time.Now(), "ago", "") -} - -func renderMemory(pid int) string { - p, _ := process.NewProcess(int32(pid)) - i, err := p.MemoryInfo() - if err != nil { - return err.Error() - } - - return humanize.Bytes(i.RSS) + util.WorkerTable(r.Workers).Render() } diff --git a/cmd/rr/util/cprint.go b/cmd/rr/util/cprint.go new file mode 100644 index 00000000..0985de62 --- /dev/null +++ b/cmd/rr/util/cprint.go @@ -0,0 +1,28 @@ +package util + +import ( + "fmt" + "github.com/mgutz/ansi" + "regexp" + "strings" +) + +var reg *regexp.Regexp + +func init() { + reg, _ = regexp.Compile(`<([^>]+)>`) +} + +// Printf works identically to fmt.Print but adds `color formatting support for CLI`. +func Printf(format string, args ...interface{}) { + fmt.Print(Sprintf(format, args...)) +} + +// Sprintf works identically to fmt.Sprintf but adds `color formatting support for CLI`. +func Sprintf(format string, args ...interface{}) string { + format = reg.ReplaceAllStringFunc(format, func(s string) string { + return ansi.ColorCode(strings.Trim(s, "<>/")) + }) + + return fmt.Sprintf(format, args...) +} diff --git a/cmd/rr/util/list.go b/cmd/rr/util/list.go new file mode 100644 index 00000000..4094ce44 --- /dev/null +++ b/cmd/rr/util/list.go @@ -0,0 +1,58 @@ +package util + +import ( + rrutil "github.com/spiral/roadrunner/util" + "github.com/olekukonko/tablewriter" + "os" + "strconv" + "github.com/dustin/go-humanize" + "time" +) + +// WorkerTable renders table with information about rr server workers. +func WorkerTable(workers []*rrutil.State) *tablewriter.Table { + tw := tablewriter.NewWriter(os.Stdout) + tw.SetHeader([]string{"PID", "Status", "Execs", "Memory", "Created"}) + tw.SetColMinWidth(0, 7) + tw.SetColMinWidth(1, 9) + tw.SetColMinWidth(2, 7) + tw.SetColMinWidth(3, 7) + tw.SetColMinWidth(4, 18) + + for _, w := range workers { + tw.Append([]string{ + strconv.Itoa(w.Pid), + renderStatus(w.Status), + renderJobs(w.NumJobs), + humanize.Bytes(w.MemoryUsage), + renderAlive(time.Unix(0, w.Created)), + }) + } + + return tw +} + +func renderStatus(status string) string { + switch status { + case "inactive": + return Sprintf("inactive") + case "ready": + return Sprintf("ready") + case "working": + return Sprintf("working") + case "stopped": + return Sprintf("stopped") + case "errored": + return Sprintf("errored") + } + + return status +} + +func renderJobs(number int64) string { + return humanize.Comma(int64(number)) +} + +func renderAlive(t time.Time) string { + return humanize.RelTime(t, time.Now(), "ago", "") +} diff --git a/cmd/rr/utils/cprint.go b/cmd/rr/utils/cprint.go deleted file mode 100644 index 020975ec..00000000 --- a/cmd/rr/utils/cprint.go +++ /dev/null @@ -1,28 +0,0 @@ -package utils - -import ( - "fmt" - "github.com/mgutz/ansi" - "regexp" - "strings" -) - -var reg *regexp.Regexp - -func init() { - reg, _ = regexp.Compile(`<([^>]+)>`) -} - -// Printf works identically to fmt.Print but adds `color formatting support for CLI`. -func Printf(format string, args ...interface{}) { - fmt.Print(Sprintf(format, args...)) -} - -// Sprintf works identically to fmt.Sprintf but adds `color formatting support for CLI`. -func Sprintf(format string, args ...interface{}) string { - format = reg.ReplaceAllStringFunc(format, func(s string) string { - return ansi.ColorCode(strings.Trim(s, "<>/")) - }) - - return fmt.Sprintf(format, args...) -} -- cgit v1.2.3