master
go 114 lines 2.97 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package supervisord
4
5 import (
6 "context"
7 "errors"
8 "fmt"
9 "net"
10 "net/http"
11 "net/url"
12 "strings"
13
14 "github.com/mattn/go-xmlrpc"
15 )
16
17 type supervisorClient interface {
18 getAllProcessInfo() ([]processStatus, error)
19 closeIdleConnections()
20 }
21
22 type supervisorRPCClient struct {
23 client *xmlrpc.Client
24 }
25
26 func newSupervisorRPCClient(serverURL *url.URL, httpClient *http.Client) (supervisorClient, error) {
27 switch serverURL.Scheme {
28 case "http", "https":
29 c := xmlrpc.NewClient(serverURL.String())
30 c.HttpClient = httpClient
31 return &supervisorRPCClient{client: c}, nil
32 case "unix":
33 c := xmlrpc.NewClient("http://unix/RPC2")
34 t, ok := httpClient.Transport.(*http.Transport)
35 if !ok {
36 return nil, errors.New("unexpected HTTPConfig client transport")
37 }
38 t.DialContext = func(ctx context.Context, _, _ string) (net.Conn, error) {
39 d := net.Dialer{Timeout: httpClient.Timeout}
40 return d.DialContext(ctx, "unix", serverURL.Path)
41 }
42 c.HttpClient = httpClient
43 return &supervisorRPCClient{client: c}, nil
44 default:
45 return nil, fmt.Errorf("unexpected URL scheme: %s", serverURL)
46 }
47 }
48
49 // http://supervisord.org/api.html#process-control
50 type processStatus struct {
51 name string // name of the process.
52 group string // name of the process’ group.
53 start int // UNIX timestamp of when the process was started.
54 stop int // UNIX timestamp of when the process last ended, or 0 if the process has never been stopped.
55 now int // UNIX timestamp of the current time, which can be used to calculate process up-time.
56 state int // state code.
57 stateName string // string description of state.
58 exitStatus int // exit status (errorlevel) of process, or 0 if the process is still running.
59 }
60
61 func (c *supervisorRPCClient) getAllProcessInfo() ([]processStatus, error) {
62 const fn = "supervisor.getAllProcessInfo"
63 resp, err := c.client.Call(fn)
64 if err != nil {
65 return nil, fmt.Errorf("error on '%s' function call: %v", fn, err)
66 }
67 return parseGetAllProcessInfo(resp)
68 }
69
70 func (c *supervisorRPCClient) closeIdleConnections() {
71 c.client.HttpClient.CloseIdleConnections()
72 }
73
74 func parseGetAllProcessInfo(resp any) ([]processStatus, error) {
75 arr, ok := resp.(xmlrpc.Array)
76 if !ok {
77 return nil, fmt.Errorf("unexpected response type, want=xmlrpc.Array, got=%T", resp)
78 }
79
80 var info []processStatus
81
82 for _, item := range arr {
83 s, ok := item.(xmlrpc.Struct)
84 if !ok {
85 continue
86 }
87
88 var p processStatus
89 for k, v := range s {
90 switch strings.ToLower(k) {
91 case "name":
92 p.name, _ = v.(string)
93 case "group":
94 p.group, _ = v.(string)
95 case "start":
96 p.start, _ = v.(int)
97 case "stop":
98 p.stop, _ = v.(int)
99 case "now":
100 p.now, _ = v.(int)
101 case "state":
102 p.state, _ = v.(int)
103 case "statename":
104 p.stateName, _ = v.(string)
105 case "exitstatus":
106 p.exitStatus, _ = v.(int)
107 }
108 }
109 if p.name != "" && p.group != "" && p.stateName != "" {
110 info = append(info, p)
111 }
112 }
113 return info, nil
114 }