Get the App
SLTechnology News&Howtos  ›  Development  › 

Go operates the supervisor xml rpc interface and what matters need to be paid attention to

Shulou Source: shulou.com Published: 2022-06-03 10:25:07 10月03日 Update

Go operation supervisor xml rpc interface and what matters need to pay attention to, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

1. Preface

As mentioned before, our processes are managed through a process management software such as supervisor (http://supervisord.org/), and we have also done special translations of some contents of supervisor: https://blog.csdn.net/weixin_39510813/category_11128455.html.

We will find that more than 3.0 versions will have xml-rpc interface (http://supervisord.org/xmlrpc.html) that can control the process of supervisor management through the corresponding interface, including access to the corresponding log, running status and other functions, which in the actual development process to obtain these information on the web control, query is also very helpful, so here is a simple summary of go how to deal with the information of the supervisor management process.

two。 Manage web

It is generally added to the configuration file:

[inet_http_server]

Port=9001

You can access a web page through port 9001:

Through xml-rpc, you can obtain the status, control and manage these processes, view the corresponding logs, and so on.

Note: the user name and password of the web may need to be configured for security, but in order to facilitate program management, it is best not to configure authentication, otherwise the program may not be controlled due to authentication failure.

3. Go processing library

Here is a go-supervisor processing library: https://github.com/abrander/go-supervisord

Https://pkg.go.dev/github.com/abrander/go-supervisord#section-readme

Import "github.com/abrander/go-supervisord" func main () {c Err: = supervisord.NewClient ("http://127.0.0.1:9001/RPC2") if err! = nil {panic (err.Error ())} err = c.ClearLog () if err! = nil {panic (err.Error ())} err = c.Restart () If err! = nil {panic (err.Error ())}}

The real-time log API for obtaining stdout in the corresponding library is not implemented. We can expand the interface by websocket, mainly by obtaining the corresponding log file name through process information.

4. Real-time log processing code snippet

Here is a code snippet that obtains the log file name through supervisor, and then reads the log for real-time log reporting through websocket:

Func (s * businessLogService) TailLog (name string, ws * ghttp.WebSocket) error {c, err: = supervisord.NewClient ("http://127.0.0.1:9001/RPC2") if err! = nil {return err} defer c.Close () processInfo Err: = c.GetProcessInfo (name) if err! = nil {logger.Error (err) return err} filename = processInfo.StdoutLogfile logger.Debug (filename) s.serveWs (ws) return nil} / * * the following dependencies need to be installed when compiling: go get github.com/gorilla/websocketgo get github.com/hpcloud/tail*/const ( / / Time allowed to write the file to the client. / / writeWait = 1 * time.Second writeWait = 100 * time.Millisecond / / Time allowed to read the next pong message from the client. / / pongWait = 24 * time.Hour pongWait = 60 * time.Second / / Send pings to client with this period. Must be less than pongWait. PingPeriod = (pongWait * 9) / 10 / / Poll file for changes with this period. FilePeriod = 1 * time.Second) var (filename string) func (s * businessLogService) readFileIfModified (lastMod time.Time) ([] byte, time.Time, error) {fi, err: = os.Stat (filename) if err! = nil {return nil, lastMod, err} if! fi.ModTime (). After (lastMod) {return nil, lastMod, nil} p Err: = ioutil.ReadFile (filename) if err! = nil {return nil, fi.ModTime (), err} return p, fi.ModTime () Nil} func (s * businessLogService) reader (ws * ghttp.WebSocket) {defer ws.Close () ws.SetReadLimit (512) ws.SetReadDeadline (time.Now () .Add (pongWait)) ws.SetPongHandler (func (string) error {ws.SetReadDeadline (time.Now (). Add (pongWait)) Return nil}) for {_, err: = ws.ReadMessage () if err! = nil {logger.Warn (err) break} func (s * businessLogService) tailFile () * tail.Tail {tailFd, err: = tail.TailFile (filename Tail.Config {ReOpen: true, / / files are removed or packaged You need to reopen Follow: true, / / Real-time tracking Location: & tail.SeekInfo {Offset: 0, Whence: 2}, / / if there is an exception in the program, save the last read location to avoid re-reading. MustExist: false, / / if the file does not exist, whether to launch the program False does not quit Poll: true,}) if err! = nil {logger.Error ("tail file failed, err:" Err) return nil} return tailFd} func (s * businessLogService) writer (ws * ghttp.WebSocket) {tailFd: = s.tailFile () pingTicker: = time.NewTicker (pingPeriod) fileTicker: = time.NewTicker (filePeriod) maxTimeout: = time.NewTicker (time.Duration (1) * time.Minute) defer func () {pingTicker .stop () fileTicker.Stop () ws.Close ()} () for {select {case msg Ok: =

Tags: Log management process interface file real-time program processing control information help configuration code content can be passed file name fragment status item clear Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Docker vpn MariaDB Shulou Technology