summaryrefslogtreecommitdiff
path: root/cmd/util
diff options
context:
space:
mode:
authorWolfy-J <[email protected]>2018-09-26 22:30:53 +0300
committerWolfy-J <[email protected]>2018-09-26 22:30:53 +0300
commita206fc3270ea6e469b1704f15f2f15a9f6a14bbd (patch)
tree68bc61c49f84db861976659abed3784bb7e304a9 /cmd/util
parentd24c43aaeff897394eca140478c73dd146f8710a (diff)
improved debug handlers
Diffstat (limited to 'cmd/util')
-rw-r--r--cmd/util/client.go18
-rw-r--r--cmd/util/cprint.go28
-rw-r--r--cmd/util/debug.go57
-rw-r--r--cmd/util/table.go58
4 files changed, 161 insertions, 0 deletions
diff --git a/cmd/util/client.go b/cmd/util/client.go
new file mode 100644
index 00000000..ee3414a6
--- /dev/null
+++ b/cmd/util/client.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/cprint.go b/cmd/util/cprint.go
new file mode 100644
index 00000000..0985de62
--- /dev/null
+++ b/cmd/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 `<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...)
+}
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/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", "")
+}