Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions agent/app/service/host_tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,8 +613,7 @@ func getProcessStatus(config *response.SupervisorProcessConfig, containerName st
Status: fields[1],
}
if fields[1] == "RUNNING" {
status.PID = strings.TrimSuffix(fields[3], ",")
status.Uptime = fields[5]
status.PID, status.Uptime = parseSupervisorRunningDetails(fields)
} else {
status.Msg = strings.Join(fields[2:], " ")
}
Expand All @@ -623,3 +622,22 @@ func getProcessStatus(config *response.SupervisorProcessConfig, containerName st
}
return nil
}

func parseSupervisorRunningDetails(fields []string) (string, string) {
var pid, uptime string
for i, field := range fields {
field = strings.TrimSuffix(field, ",")
switch field {
case "pid":
if i+1 < len(fields) {
pid = strings.TrimSuffix(fields[i+1], ",")
}
case "uptime":
if i+1 < len(fields) {
uptime = strings.Join(fields[i+1:], " ")
}
return pid, uptime
}
}
return pid, uptime
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是不是可以限制一下从 fields[3:] 之后开始处理呢
避免 supvervisor name 取名 uptime 的情况(虽然概率很低)

46 changes: 46 additions & 0 deletions agent/app/service/host_tool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package service

import (
"strings"
"testing"
)

func TestParseSupervisorRunningDetails(t *testing.T) {
tests := []struct {
name string
line string
wantPID string
wantUptime string
}{
{
name: "short uptime",
line: "test:test_00 RUNNING pid 123, uptime 0:12:34",
wantPID: "123",
wantUptime: "0:12:34",
},
{
name: "single day uptime",
line: "test:test_00 RUNNING pid 123, uptime 1 day, 0:12:34",
wantPID: "123",
wantUptime: "1 day, 0:12:34",
},
{
name: "multiple days uptime",
line: "test:test_00 RUNNING pid 123, uptime 103 days, 0:12:34",
wantPID: "123",
wantUptime: "103 days, 0:12:34",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pid, uptime := parseSupervisorRunningDetails(strings.Fields(tt.line))
if pid != tt.wantPID {
t.Fatalf("pid = %q, want %q", pid, tt.wantPID)
}
if uptime != tt.wantUptime {
t.Fatalf("uptime = %q, want %q", uptime, tt.wantUptime)
}
})
}
}