diff options
author | Wolfy-J <[email protected]> | 2018-09-29 23:37:16 +0300 |
---|---|---|
committer | GitHub <[email protected]> | 2018-09-29 23:37:16 +0300 |
commit | 6122fca108c20984732c969fb1ba53cce5b3c44a (patch) | |
tree | 40835f46a5c208ea2546b76e3bd9fa05429b405a /cmd | |
parent | abe62c0675f839586312cff1c83d6a4cb31dd9d5 (diff) | |
parent | a04b5b33eb30944007973067ec07e9c4a2c464ab (diff) |
Merge pull request #39 from spiral/feature/1.3.0v1.2.3
Feature/1.3.0
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/rr/cmd/root.go | 8 | ||||
-rw-r--r-- | cmd/rr/debug/debugger.go | 120 | ||||
-rw-r--r-- | cmd/rr/http/debug.go | 84 | ||||
-rw-r--r-- | cmd/rr/http/reset.go | 18 | ||||
-rw-r--r-- | cmd/rr/http/workers.go | 71 | ||||
-rw-r--r-- | cmd/rr/main.go | 2 | ||||
-rw-r--r-- | cmd/util/cprint.go (renamed from cmd/rr/utils/cprint.go) | 2 | ||||
-rw-r--r-- | cmd/util/debug.go | 57 | ||||
-rw-r--r-- | cmd/util/rpc.go | 18 | ||||
-rw-r--r-- | cmd/util/table.go | 58 |
10 files changed, 224 insertions, 214 deletions
diff --git a/cmd/rr/cmd/root.go b/cmd/rr/cmd/root.go index 4ab37967..e71ce0c4 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/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( "<green>RoadRunner, PHP Application Server:</reset>\nVersion: <yellow+hb>%s</reset>, %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("<red+hb>Error:</reset> <red>%s</reset>\n", err) + util.Printf("<red+hb>Error:</reset> <red>%s</reset>\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("<red+hb>Error:</reset> <red>%s</reset>\n", err) + util.Printf("<red+hb>Error:</reset> <red>%s</reset>\n", err) os.Exit(1) } } diff --git a/cmd/rr/debug/debugger.go b/cmd/rr/debug/debugger.go deleted file mode 100644 index ed9a1a56..00000000 --- a/cmd/rr/debug/debugger.go +++ /dev/null @@ -1,120 +0,0 @@ -package debug - -import ( - "fmt" - "github.com/sirupsen/logrus" - "github.com/spiral/roadrunner" - "github.com/spiral/roadrunner/cmd/rr/utils" - rrhttp "github.com/spiral/roadrunner/service/http" - "net/http" - "strings" -) - -// Listener creates new debug listener. -func Listener(logger *logrus.Logger) func(event int, ctx interface{}) { - return (&debugger{logger}).listener -} - -// listener provide debug callback for system events. With colors! -type debugger struct{ logger *logrus.Logger } - -// listener listens to http events and generates nice looking output. -func (s *debugger) listener(event int, ctx interface{}) { - // http events - switch event { - case rrhttp.EventResponse: - e := ctx.(*rrhttp.ResponseEvent) - s.logger.Info(utils.Sprintf( - "<cyan+h>%s</reset> %s <white+hb>%s</reset> %s", - e.Request.RemoteAddr, - statusColor(e.Response.Status), - e.Request.Method, - e.Request.URI, - )) - case rrhttp.EventError: - e := ctx.(*rrhttp.ErrorEvent) - - if _, ok := e.Error.(roadrunner.JobError); ok { - s.logger.Info(utils.Sprintf( - "%s <white+hb>%s</reset> %s", - statusColor(500), - e.Request.Method, - uri(e.Request), - )) - } else { - s.logger.Info(utils.Sprintf( - "%s <white+hb>%s</reset> %s <red>%s</reset>", - statusColor(500), - e.Request.Method, - uri(e.Request), - e.Error, - )) - } - } - - switch event { - case roadrunner.EventWorkerKill: - w := ctx.(*roadrunner.Worker) - s.logger.Warning(utils.Sprintf( - "<white+hb>worker.%v</reset> <yellow>killed</red>", - *w.Pid, - )) - case roadrunner.EventWorkerError: - err := ctx.(roadrunner.WorkerError) - s.logger.Error(utils.Sprintf( - "<white+hb>worker.%v</reset> <red>%s</reset>", - *err.Worker.Pid, - err.Caused, - )) - } - - // outputs - switch event { - case roadrunner.EventStderrOutput: - s.logger.Warning(utils.Sprintf( - "<yellow>%s</reset>", - strings.Trim(string(ctx.([]byte)), "\r\n"), - )) - } - - // rr server events - switch event { - case roadrunner.EventServerFailure: - s.logger.Error(utils.Sprintf("<red>server is dead</reset>")) - } - - // pool events - switch event { - case roadrunner.EventPoolConstruct: - s.logger.Debug(utils.Sprintf("<cyan>new worker pool</reset>")) - case roadrunner.EventPoolError: - s.logger.Error(utils.Sprintf("<red>%s</reset>", ctx)) - } - - //s.logger.Warning(event, ctx) -} - -func statusColor(status int) string { - if status < 300 { - return utils.Sprintf("<green>%v</reset>", status) - } - - if status < 400 { - return utils.Sprintf("<cyan>%v</reset>", status) - } - - if status < 500 { - return utils.Sprintf("<yellow>%v</reset>", status) - } - - return utils.Sprintf("<red>%v</reset>", status) -} - -// uri fetches full uri from request in a form of string (including https scheme if TLS connection is enabled). -func uri(r *http.Request) string { - if r.TLS != nil { - return fmt.Sprintf("https://%s%s", r.Host, r.URL.String()) - } - - return fmt.Sprintf("http://%s%s", r.Host, r.URL.String()) -} diff --git a/cmd/rr/http/debug.go b/cmd/rr/http/debug.go index f69e10a8..53980303 100644 --- a/cmd/rr/http/debug.go +++ b/cmd/rr/http/debug.go @@ -1,20 +1,90 @@ package http import ( - rr "github.com/spiral/roadrunner/cmd/rr/cmd" - + "fmt" + "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "github.com/spiral/roadrunner/cmd/rr/debug" - "github.com/spiral/roadrunner/service/http" + "github.com/spiral/roadrunner" + rr "github.com/spiral/roadrunner/cmd/rr/cmd" + "github.com/spiral/roadrunner/cmd/util" + rrhttp "github.com/spiral/roadrunner/service/http" + "net/http" ) func init() { cobra.OnInitialize(func() { if rr.Debug { - svc, _ := rr.Container.Get(http.ID) - if svc, ok := svc.(*http.Service); ok { - svc.AddListener(debug.Listener(rr.Logger)) + svc, _ := rr.Container.Get(rrhttp.ID) + if svc, ok := svc.(*rrhttp.Service); ok { + svc.AddListener((&debugger{logger: rr.Logger}).listener) } } }) } + +// listener provide debug callback for system events. With colors! +type debugger struct{ logger *logrus.Logger } + +// listener listens to http events and generates nice looking output. +func (s *debugger) listener(event int, ctx interface{}) { + if util.LogEvent(s.logger, event, ctx) { + // handler by default debug package + return + } + + // http events + switch event { + case rrhttp.EventResponse: + e := ctx.(*rrhttp.ResponseEvent) + s.logger.Info(util.Sprintf( + "<cyan+h>%s</reset> %s <white+hb>%s</reset> %s", + e.Request.RemoteAddr, + statusColor(e.Response.Status), + e.Request.Method, + e.Request.URI, + )) + case rrhttp.EventError: + e := ctx.(*rrhttp.ErrorEvent) + + if _, ok := e.Error.(roadrunner.JobError); ok { + s.logger.Info(util.Sprintf( + "%s <white+hb>%s</reset> %s", + statusColor(500), + e.Request.Method, + uri(e.Request), + )) + } else { + s.logger.Info(util.Sprintf( + "%s <white+hb>%s</reset> %s <red>%s</reset>", + statusColor(500), + e.Request.Method, + uri(e.Request), + e.Error, + )) + } + } +} + +func statusColor(status int) string { + if status < 300 { + return util.Sprintf("<green>%v</reset>", status) + } + + if status < 400 { + return util.Sprintf("<cyan>%v</reset>", status) + } + + if status < 500 { + return util.Sprintf("<yellow>%v</reset>", status) + } + + return util.Sprintf("<red>%v</reset>", status) +} + +func uri(r *http.Request) string { + if r.TLS != nil { + return fmt.Sprintf("https://%s%s", r.Host, r.URL.String()) + } + + return fmt.Sprintf("http://%s%s", r.Host, r.URL.String()) +} diff --git a/cmd/rr/http/reset.go b/cmd/rr/http/reset.go index 3bc089ec..42fd966d 100644 --- a/cmd/rr/http/reset.go +++ b/cmd/rr/http/reset.go @@ -21,41 +21,33 @@ package http 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/service" - "github.com/spiral/roadrunner/service/rpc" + "github.com/spiral/roadrunner/cmd/util" ) func init() { rr.CLI.AddCommand(&cobra.Command{ Use: "http:reset", - Short: "Reload RoadRunner worker pools for the HTTP service", + Short: "Reload RoadRunner worker pool for the HTTP service", RunE: reloadHandler, }) } func reloadHandler(cmd *cobra.Command, args []string) error { - svc, st := rr.Container.Get(rpc.ID) - if st < service.StatusOK { - return errors.New("RPC service is not configured") - } - - client, err := svc.(*rpc.Service).Client() + client, err := util.RPCClient(rr.Container) if err != nil { return err } defer client.Close() - utils.Printf("<green>restarting http worker pool</reset>: ") + util.Printf("<green>restarting http worker pool</reset>: ") var r string if err := client.Call("http.Reset", true, &r); err != nil { return err } - utils.Printf("<green+hb>done</reset>\n") + util.Printf("<green+hb>done</reset>\n") return nil } diff --git a/cmd/rr/http/workers.go b/cmd/rr/http/workers.go index b03c273f..4444b87f 100644 --- a/cmd/rr/http/workers.go +++ b/cmd/rr/http/workers.go @@ -21,21 +21,14 @@ 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/service" + "github.com/spiral/roadrunner/cmd/util" "github.com/spiral/roadrunner/service/http" - rrpc "github.com/spiral/roadrunner/service/rpc" "net/rpc" "os" "os/signal" - "strconv" "syscall" "time" ) @@ -73,12 +66,7 @@ func workersHandler(cmd *cobra.Command, args []string) (err error) { } }() - svc, st := rr.Container.Get(rrpc.ID) - if st < service.StatusOK { - return errors.New("RPC service is not configured") - } - - client, err := svc.(*rrpc.Service).Client() + client, err := util.RPCClient(rr.Container) if err != nil { return err } @@ -108,58 +96,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("<yellow>inactive</reset>") - case "ready": - return utils.Sprintf("<cyan>ready</reset>") - case "working": - return utils.Sprintf("<green>working</reset>") - case "stopped": - return utils.Sprintf("<red>stopped</reset>") - case "errored": - return utils.Sprintf("<red>errored</reset>") - } - - 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/main.go b/cmd/rr/main.go index 18e22cdd..4d2a06b4 100644 --- a/cmd/rr/main.go +++ b/cmd/rr/main.go @@ -32,7 +32,7 @@ import ( "github.com/spiral/roadrunner/service/rpc" "github.com/spiral/roadrunner/service/static" - // additional command and debug handlers + // additional commands and debug handlers _ "github.com/spiral/roadrunner/cmd/rr/http" ) diff --git a/cmd/rr/utils/cprint.go b/cmd/util/cprint.go index 020975ec..0985de62 100644 --- a/cmd/rr/utils/cprint.go +++ b/cmd/util/cprint.go @@ -1,4 +1,4 @@ -package utils +package util import ( "fmt" diff --git a/cmd/util/debug.go b/cmd/util/debug.go new file mode 100644 index 00000000..7be258ec --- /dev/null +++ b/cmd/util/debug.go @@ -0,0 +1,57 @@ +package util + +import ( + "github.com/sirupsen/logrus" + "github.com/spiral/roadrunner" + "strings" +) + +// LogEvent outputs rr event into given logger and return false if event was not handled. +func LogEvent(logger *logrus.Logger, event int, ctx interface{}) bool { + switch event { + case roadrunner.EventWorkerKill: + w := ctx.(*roadrunner.Worker) + logger.Warning(Sprintf( + "<white+hb>worker.%v</reset> <yellow>killed</red>", + *w.Pid, + )) + return true + case roadrunner.EventWorkerError: + err := ctx.(roadrunner.WorkerError) + logger.Error(Sprintf( + "<white+hb>worker.%v</reset> <red>%s</reset>", + *err.Worker.Pid, + err.Caused, + )) + return true + } + + // outputs + switch event { + case roadrunner.EventStderrOutput: + logger.Warning(Sprintf( + "<yellow>%s</reset>", + strings.Trim(string(ctx.([]byte)), "\r\n"), + )) + return true + } + + // rr server events + switch event { + case roadrunner.EventServerFailure: + logger.Error(Sprintf("<red>server is dead</reset>")) + return true + } + + // pool events + switch event { + case roadrunner.EventPoolConstruct: + logger.Debug(Sprintf("<cyan>new worker pool</reset>")) + return true + case roadrunner.EventPoolError: + logger.Error(Sprintf("<red>%s</reset>", ctx)) + return true + } + + return false +} diff --git a/cmd/util/rpc.go b/cmd/util/rpc.go new file mode 100644 index 00000000..ee3414a6 --- /dev/null +++ b/cmd/util/rpc.go @@ -0,0 +1,18 @@ +package util + +import ( + "errors" + "github.com/spiral/roadrunner/service" + rrpc "github.com/spiral/roadrunner/service/rpc" + "net/rpc" +) + +// RPCClient returns RPC client associated with given roadrunner service container. +func RPCClient(container service.Container) (*rpc.Client, error) { + svc, st := container.Get(rrpc.ID) + if st < service.StatusOK { + return nil, errors.New("RPC service is not configured") + } + + return svc.(*rrpc.Service).Client() +} diff --git a/cmd/util/table.go b/cmd/util/table.go new file mode 100644 index 00000000..565c0679 --- /dev/null +++ b/cmd/util/table.go @@ -0,0 +1,58 @@ +package util + +import ( + "github.com/dustin/go-humanize" + "github.com/olekukonko/tablewriter" + rrutil "github.com/spiral/roadrunner/util" + "os" + "strconv" + "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("<yellow>inactive</reset>") + case "ready": + return Sprintf("<cyan>ready</reset>") + case "working": + return Sprintf("<green>working</reset>") + case "stopped": + return Sprintf("<red>stopped</reset>") + case "errored": + return Sprintf("<red>errored</reset>") + } + + 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", "") +} |