summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
Diffstat (limited to 'tools')
-rw-r--r--tools/process.go44
-rw-r--r--tools/worker_table.go61
2 files changed, 105 insertions, 0 deletions
diff --git a/tools/process.go b/tools/process.go
new file mode 100644
index 00000000..d92755d1
--- /dev/null
+++ b/tools/process.go
@@ -0,0 +1,44 @@
+package tools
+
+import (
+ "github.com/shirou/gopsutil/process"
+ "github.com/spiral/errors"
+ "github.com/spiral/roadrunner/v2/interfaces/worker"
+)
+
+// ProcessState provides information about specific worker.
+type ProcessState struct {
+ // Pid contains process id.
+ Pid int `json:"pid"`
+
+ // Status of the worker.
+ Status string `json:"status"`
+
+ // Number of worker executions.
+ NumJobs int64 `json:"numExecs"`
+
+ // Created is unix nano timestamp of worker creation time.
+ Created int64 `json:"created"`
+
+ // MemoryUsage holds the information about worker memory usage in bytes.
+ // Values might vary for different operating systems and based on RSS.
+ MemoryUsage uint64 `json:"memoryUsage"`
+}
+
+// WorkerProcessState creates new worker state definition.
+func WorkerProcessState(w worker.BaseProcess) (ProcessState, error) {
+ const op = errors.Op("worker_process state")
+ p, _ := process.NewProcess(int32(w.Pid()))
+ i, err := p.MemoryInfo()
+ if err != nil {
+ return ProcessState{}, errors.E(op, err)
+ }
+
+ return ProcessState{
+ Pid: int(w.Pid()),
+ Status: w.State().String(),
+ NumJobs: w.State().NumExecs(),
+ Created: w.Created().UnixNano(),
+ MemoryUsage: i.RSS,
+ }, nil
+}
diff --git a/tools/worker_table.go b/tools/worker_table.go
new file mode 100644
index 00000000..4aeb6ae7
--- /dev/null
+++ b/tools/worker_table.go
@@ -0,0 +1,61 @@
+package tools
+
+import (
+ "io"
+ "strconv"
+ "time"
+
+ "github.com/dustin/go-humanize"
+ "github.com/fatih/color"
+ "github.com/olekukonko/tablewriter"
+)
+
+// WorkerTable renders table with information about rr server workers.
+func WorkerTable(writer io.Writer, workers []ProcessState) *tablewriter.Table {
+ tw := tablewriter.NewWriter(writer)
+ 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 key := range workers {
+ tw.Append([]string{
+ strconv.Itoa(workers[key].Pid),
+ renderStatus(workers[key].Status),
+ renderJobs(workers[key].NumJobs),
+ humanize.Bytes(workers[key].MemoryUsage),
+ renderAlive(time.Unix(0, workers[key].Created)),
+ })
+ }
+
+ return tw
+}
+
+func renderStatus(status string) string {
+ switch status {
+ case "inactive":
+ return color.YellowString("inactive")
+ case "ready":
+ return color.CyanString("ready")
+ case "working":
+ return color.GreenString("working")
+ case "invalid":
+ return color.YellowString("invalid")
+ case "stopped":
+ return color.RedString("stopped")
+ case "errored":
+ return color.RedString("errored")
+ }
+
+ return status
+}
+
+func renderJobs(number int64) string {
+ return humanize.Comma(number)
+}
+
+func renderAlive(t time.Time) string {
+ return humanize.RelTime(t, time.Now(), "ago", "")
+}