当前位置:网站首页>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)
- leetcode 474. Ones and Zeroes 一和零(中等)
- Set different background colors for the border and text of the button
- P4 learning - Basic tunneling
- 2022-2028 global weight loss ginger tea industry research and trend analysis report
- Oracle-表的创建与管理
- Two-stage RO: part 1
- CentOS install MySQL
- 获取屏幕高度
- Basic knowledge of Embedded Network - introduction of mqtt
猜你喜欢

The quantity and quality of the devil's cold rice 101; Employee management; College entrance examination voluntary filling; Game architecture design

Redis - how to understand publishing and subscribing

20220216 misc buuctf backdoor killing (d shield scanning) - clues in the packet (Base64 to image)

2022-2028 global plant peptone industry research and trend analysis report

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

P4 learning - p4runtime

CMU15445 (Fall 2019) 之 Project#1 - Buffer Pool 详解

2022-2028 global encrypted external hard disk industry research and trend analysis report

2022-2028 global ultra high purity electrolytic iron powder industry research and trend analysis report

CentOS installation starts redis
随机推荐
1009 product of polynomials (25 points) [PTA class A]
Redis - cache penetration, cache breakdown, cache avalanche
PyTorch安装并使用gpu加速
对libco的一点看法
Get to know the drawing component of flutter - custompaint
Exercise and health
剑指 Offer 18. 删除链表的节点
Solving the weird problem that the query conditions affect the value of query fields in MySQL query
2022就要过去一半了,挣钱好难
20220215 misc buctf easycap Wireshark tracks TCP flow hidden key (use of WinHex tool)
Techo youth 2022 academic year college open class: behind the live broadcast of Lianmai, explore how to apply audio and video technology
[daily record] - bug encountered in BigDecimal division operation
Introduction to ES6 promise, new features of ES7 and es8 async and await
第53章 从业务逻辑实现角度整体性理解程序
20220215 CTF misc buuctf Xiaoming's safe binwalk analysis DD command separate rar file archpr brute force password cracking
ArrayList分析1-循环、扩容、版本
The question of IBL precomputation is finally solved
剑指 Offer 19. 正则表达式匹配
ArrayList analysis 1-cycle, capacity expansion, version
C WinForm program interface optimization example