summaryrefslogtreecommitdiff
path: root/cmd/rr
diff options
context:
space:
mode:
authorWolfy-J <[email protected]>2018-09-29 23:37:16 +0300
committerGitHub <[email protected]>2018-09-29 23:37:16 +0300
commit6122fca108c20984732c969fb1ba53cce5b3c44a (patch)
tree40835f46a5c208ea2546b76e3bd9fa05429b405a /cmd/rr
parentabe62c0675f839586312cff1c83d6a4cb31dd9d5 (diff)
parenta04b5b33eb30944007973067ec07e9c4a2c464ab (diff)
Merge pull request #39 from spiral/feature/1.3.0v1.2.3
Feature/1.3.0
Diffstat (limited to 'cmd/rr')
-rw-r--r--cmd/rr/cmd/root.go8
-rw-r--r--cmd/rr/debug/debugger.go120
-rw-r--r--cmd/rr/http/debug.go84
-rw-r--r--cmd/rr/http/reset.go18
-rw-r--r--cmd/rr/http/workers.go71
-rw-r--r--cmd/rr/main.go2
-rw-r--r--cmd/rr/utils/cprint.go28
7 files changed, 90 insertions, 241 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/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 `<white+hb>color formatting support for CLI</reset>`.
-func Printf(format string, args ...interface{}) {
- fmt.Print(Sprintf(format, args...))
-}
-
-// Sprintf works identically to fmt.Sprintf but adds `<white+hb>color formatting support for CLI</reset>`.
-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...)
-}