1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
package workers
import (
"io"
"sort"
"strconv"
"time"
"github.com/dustin/go-humanize"
"github.com/fatih/color"
"github.com/olekukonko/tablewriter"
"github.com/roadrunner-server/api/v4/plugins/v4/jobs"
"github.com/roadrunner-server/pool/state/process"
)
const (
Ready string = "READY"
Paused string = "PAUSED/STOPPED"
)
// WorkerTable renders table with information about rr server workers.
func WorkerTable(writer io.Writer, workers []*process.State, err error) *tablewriter.Table {
tw := tablewriter.NewWriter(writer)
tw.SetHeader([]string{"PID", "Status", "Execs", "Memory", "CPU%", "Created"})
tw.SetColMinWidth(0, 7)
tw.SetColMinWidth(1, 9)
tw.SetColMinWidth(2, 7)
tw.SetColMinWidth(3, 7)
tw.SetColMinWidth(4, 7)
tw.SetColMinWidth(5, 18)
if err != nil {
tw.Append([]string{
"0",
err.Error(),
"ERROR",
"ERROR",
"ERROR",
"ERROR",
})
return tw
}
sort.Slice(workers, func(i, j int) bool {
return workers[i].Pid < workers[j].Pid
})
for i := 0; i < len(workers); i++ {
tw.Append([]string{
strconv.Itoa(int(workers[i].Pid)),
renderStatus(workers[i].StatusStr),
renderJobs(workers[i].NumExecs),
humanize.Bytes(workers[i].MemoryUsage),
renderCPU(workers[i].CPUPercent),
renderAlive(time.Unix(0, workers[i].Created)),
})
}
return tw
}
// ServiceWorkerTable renders table with information about rr server workers.
func ServiceWorkerTable(writer io.Writer, workers []*process.State) *tablewriter.Table {
sort.Slice(workers, func(i, j int) bool {
return workers[i].Pid < workers[j].Pid
})
tw := tablewriter.NewWriter(writer)
tw.SetAutoWrapText(false)
tw.SetHeader([]string{"PID", "Memory", "CPU%", "Command"})
tw.SetColMinWidth(0, 7)
tw.SetColMinWidth(1, 7)
tw.SetColMinWidth(2, 7)
tw.SetColMinWidth(3, 18)
tw.SetAlignment(tablewriter.ALIGN_LEFT)
for i := 0; i < len(workers); i++ {
tw.Append([]string{
strconv.Itoa(int(workers[i].Pid)),
humanize.Bytes(workers[i].MemoryUsage),
renderCPU(workers[i].CPUPercent),
workers[i].Command,
})
}
return tw
}
// JobsTable renders table with information about rr server jobs.
func JobsTable(writer io.Writer, jobs []*jobs.State, err error) *tablewriter.Table {
tw := tablewriter.NewWriter(writer)
tw.SetAutoWrapText(false)
tw.SetHeader([]string{"Status", "Pipeline", "Driver", "Queue", "Active", "Delayed", "Reserved"})
tw.SetColWidth(10)
tw.SetColWidth(10)
tw.SetColWidth(7)
tw.SetColWidth(15)
tw.SetColWidth(10)
tw.SetColWidth(10)
tw.SetColWidth(10)
tw.SetAlignment(tablewriter.ALIGN_LEFT)
if err != nil {
tw.Append([]string{
err.Error(),
"ERROR",
"ERROR",
"ERROR",
"ERROR",
"ERROR",
"ERROR",
})
return tw
}
sort.Slice(jobs, func(i, j int) bool {
return jobs[i].Pipeline < jobs[j].Pipeline
})
for i := 0; i < len(jobs); i++ {
tw.Append([]string{
renderReady(jobs[i].Ready),
jobs[i].Pipeline,
jobs[i].Driver,
jobs[i].Queue,
strconv.Itoa(int(jobs[i].Active)),
strconv.Itoa(int(jobs[i].Delayed)),
strconv.Itoa(int(jobs[i].Reserved)),
})
}
return tw
}
func renderReady(ready bool) string {
if ready {
return Ready
}
return Paused
}
//go:inline
func renderCPU(cpu float64) string {
return strconv.FormatFloat(cpu, 'f', 2, 64)
}
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")
default:
return status
}
}
func renderJobs(number uint64) string {
return humanize.Comma(int64(number)) //nolint:gosec
}
func renderAlive(t time.Time) string {
return humanize.RelTime(t, time.Now(), "ago", "")
}
|