当前位置:网站首页>The communication mechanism and extension of Supervisor
The communication mechanism and extension of Supervisor
2022-07-01 00:56:00 【hncscwc】
【 Background Overview 】
In our project supervisor As a process daemon . In the process of business transformation , Some configuration management businesses need to be migrated and run in a separate container , The container communicates with the main business container to complete the configuration and operation and maintenance management of business programs .
Without introducing new modules ,supervisor Whether the corresponding logic can be extended , So there is this article on supervisor Research and arrangement of communication mechanism .
【supervisor brief introduction 】
supervisor It's a client/server System , Allow users to be in the class unix Manage multiple processes on the operating system .
supervisor Yes, it is python Developed a set of general process management procedures , It can turn a common command-line program into a background program daemon, And monitor the process status , Automatic restart when the process exits abnormally .
Learn from official documents ,supervisor from 4 Component composition
supervisord: This is a supervisor Of Server program , Responsible for starting the subprocess according to the configuration , And manage ( Abnormal monitoring process triggers restart ), At the same time, respond to the client's request to perform the corresponding processing actions .
supervisorctl:supervisor Client program , Is a command line program .supervisorctl Will send to supervisord send out rpc request , Get the status of the child process , List of running programs , Initiate and stop subprocesses .
web server:supervisord Provided http service , Its function is equivalent to supervisorctl, Just providing the interface will be more intuitive , It is also more convenient to operate .
xml-rpc interface:supervisord Similar to http Service rpc Interface ,http Service and supervisorctl In essence, communication requests are completed through this interface .

【 be based on XML-RPC Interface extension for 】
supervisord Native provides only a limited set of features , from supervisorctl You can see the help information on the command line .

If you want to extend some functions , You can use it xml-rpc To achieve .
In the official website document To configure XML-RPC Interface factory Section Mentioned in the , It can be extended by adding the following configuration items to the configuration file
[rpcinterface:xxx]
supervisor.rpcinterface_factory = XXXModuleName.XXXInterfaceFactoryFunction
; rpcinterface by section The fixed prefix of
; xxx Indicates the name of the interface
; supervisor.rpcinterface_factory For fixed configuration items key
; XXXModuleName Is the module name of the interface class
; XXXInterfaceFactoryFunction The function returned for the interface class actually , In the configuration file, there is such Default , Configuration items that must be retained .
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterfaceThis is supervisord Provided by default rpc Interface class , Like the default command shown in the previous figure , The end result is to call these rpc Interface . The corresponding implementation is in supervisor Bag rpcinterface Module , The relevant code is as follows ( List only a few )


With the bedding above , It is very convenient to extend the function interface .
The first is the writing and implementation of the extension interface , Simple example code :
import subprocess
class MyRPCInterface:
def __init__(self, supervisord):
self.supervisord = supervisord
def runCommand(self, args):
ret,val = subprocess.getstatusoutput(args)
return val
def make_extend_rpcinterface(supervisord):
return MyRPCInterface(supervisord)here ,runCommand Interface implementation runs the specified system commands , Then return the result to
Then add the corresponding configuration item in the configuration , That is, add interface factory configuration items for the extension interface
[rpcinterface:hncscwc]
supervisor.rpcinterface_factory = supervisor.myrpc:make_extend_rpcinterfaceCome here , The extension interface is successfully added to supervisord It's in ( restart supervisord perhaps reload take effect )
【 And supervisorctl Integrate 】
The extension interface can be successfully added , So how to access ( call ) This interface ?
In official documents , It also gives a brief introduction , adopt python Of xmlrpc library To visit , for example :

You can see from the picture above , adopt xmlrpclib It can really be done rpc Call to .
But all through direct adjustment python Script , It looks a bit like " streaking ", Is there a more elegant way to complete the call ? After all, the original way of use (supervisorctl status/stop/start ..) It's obviously more elegant .
I wanted to find some clues from the official website documents , But no , There is no similar introduction in the online articles . So we have to go back to the most direct way to get the source code .
simply :supervisorctl On initialization , There is one The default control plug-in factory class , In this class , All with “do_" The way to prefix is supervisorctl The calling method corresponding to the command parameter .
in other words ,status/stop/start Wait and follow supervisorctl These commands of , The final call is the... In the default plug-in class do_status/do_stop/do_start Method .
In these methods , Finally through xmlrpclib Finished with supervisord Of rpc signal communication .

In addition to the default control plug-in , There is an additional step : Load customized plug-in modules from the configuration , So as to realize the expansion of the command .

With this step , We can encapsulate the previous code into simple commands and inherit them to supervisorctl Used in .
Simple example code :
from supervisor.supervisorctl import ControllerPluginBase
class MyControllerPlugin(ControllerPluginBase):
name="mycontroller"
def __init__(self, controller, **config):
self.ctl = controller
def do_exec(self, arg):
supervisor=self.ctl.get_server_proxy('myrpc')
print(supervisor.runCommand(arg))
def make_extend_controllerplugin(controller, **config):
return MyControllerPlugin(controller, **config)meanwhile , Add the following configuration items to the configuration file
[ctlplugin:mycontroller]
supervisor.ctl_factory=supervisor.mycontroller:make_extend_controllerpluginSo you can get through supervisorctl Direct access to our extended rpc The interface

【 summary 】
To sum up , This article mainly describes supervisorctl And supervisorctl Communication mechanism , And if in supervisord In the extended rpc Interface , And if in supervisorctl Expand the command , It is convenient for the integration to call our extended interface , At the same time, it can also provide the demo.
Of course , In this paper, the demo It's simpler , In fact, it can be more complicated , At the same time, it can also be combined with configuration , Carry some different parameter settings . You can refer to the official documents for this 、 Combined with the corresponding source code to study .
Okay , That's all for this article , I think it's not bad , Let's have a three company company ( give the thumbs-up , Looking at , Share )

This article is from WeChat official account. - hncscwc(gh_383bc7486c1a).
If there is any infringement , Please contact the [email protected] Delete .
Participation of this paper “OSC Source creation plan ”, You are welcome to join us , share .
边栏推荐
- C#生成putty格式的ppk文件(支持passphrase)
- Mindjet mindmanager2022 mind map decompression installer tutorial
- Pytorch auto derivation
- 20220215-ctf-misc-buuctf-einstein-binwalk analyze picture-dd command separate zip file -- look for password in picture attribute
- 20220215 CTF misc buuctf Xiaoming's safe binwalk analysis DD command separate rar file archpr brute force password cracking
- 20220216 misc buuctf another world WinHex, ASCII conversion flag zip file extraction and repair if you give me three days of brightness zip to rar, Morse code waveform conversion mysterious tornado br
- SAP ui5 beginner tutorial 19 - SAP ui5 data types and complex data binding
- Ditto set global paste only text shortcuts
- Get screen height
- 20220215-ctf-misc-buuctf-ningen--binwalk analysis --dd command separation --archpr brute force cracking
猜你喜欢

Two-stage RO: part 1

CTF tool (1) -- archpr -- including installation / use process

2022-2028 global herbal diet tea industry research and trend analysis report

2022-2028 global ethylene oxide scrubber industry research and trend analysis report

The programmer's girlfriend gave me a fatigue driving test

C#生成putty格式的ppk文件(支持passphrase)

2022-2028 global rampant travel industry research and trend analysis report
![[untitled]](/img/96/7f26614bbdcce71006e38ee34ab216.jpg)
[untitled]

2022-2028 global retro glass industry research and trend analysis report

Sword finger offer 19 Regular Expression Matching
随机推荐
Rhai - rust's embedded scripting engine
20220215 CTF misc buuctf Xiaoming's safe binwalk analysis DD command separate rar file archpr brute force password cracking
leetcode 474. Ones and Zeroes 一和零(中等)
[2023 MediaTek approved the test questions in advance] ~ questions and reference answers
【日常记录】——对BigDecimal除法运算时遇到的Bug
Confirm() method of window
C WinForm program interface optimization example
Rust book materials - yazhijia Library
Docsify building a personal minimalist knowledge warehouse
Bugku CTF daily one question dark cloud invitation code
2022-2028 global mobile scanning radiology room industry survey and trend analysis report
Sword finger offer 19 Regular Expression Matching
2022-2028 global rampant travel industry research and trend analysis report
Some views on libco
How to specify the number of cycles in JSTL- How to loop over something a specified number of times in JSTL?
魔王冷饭||#101 魔王解惑数量多与质量;员工管理;高考志愿填报;游戏架构设计
Ybtoj exchange game [tree chain splitting, line segment tree merging]
剑指 Offer 19. 正则表达式匹配
Vulnerability discovery - App application vulnerability probe type utilization and repair
20220215 misc buctf easycap Wireshark tracks TCP flow hidden key (use of WinHex tool)