[PYTHON] Try connecting to Supervisord via XMLRPC to start / stop the program

I wanted to operate the process on Supervisor from a Python program, so I tried the XMLRPC interface.

Preparation

supervisord.conf

Enable ʻinet_http_server`.

[inet_http_server]         ; inet (TCP) server disabled by default
port=127.0.0.1:9001        ; (ip_address:port specifier, *:port for all iface)
;username=user              ; (default is no username (open server))
;password=123               ; (default is no password (open server))

[include]
files = conf.d/*.ini

conf.d/simplehttp.ini

This is not important, but just in case. Settings on the program side. Since it is operated by virtualenv, add the setting of ʻenvironment`. The program itself just calls the standard module simple HTTP server.

[program:simplehttp]
command=python -m SimpleHTTPServer ; the program (relative uses PATH, can take args)
process_name=%(program_name)s ; process_name expr (default %(program_name)s)
numprocs=1                    ; number of processes copies to start (def 1)
directory=/tmp                ; directory to cwd to before exec (def no cwd)
autostart=true                ; start at supervisord start (default: true)
autorestart=unexpected        ; whether/when to restart (default: unexpected)
stdout_logfile=/tmp/simplehttp.stdout ; stdout log path, NONE for none; default AUTO
stderr_logfile=/tmp/simplehttp.stderr ; stderr log path, NONE for none; default AUTO
environment=PATH=/Users/shrkw/.virtualenv/cpython-275/bin ; process environment additions (def no adds)

Manipulate

The introduction is written at http://supervisord.org/api.html, so you should look there.

The following is operated from IPython.

Connect to XMLRPC interface

In [1]: import xmlrpclib In [2]: server = xmlrpclib.ServerProxy('http://localhost:9001')

Check method

In [6]: server.system.listMethods() Out[6]: ['supervisor.addProcessGroup', 'supervisor.clearAllProcessLogs', 'supervisor.clearLog', 'supervisor.clearProcessLog', 'supervisor.clearProcessLogs', 'supervisor.getAPIVersion', 'supervisor.getAllConfigInfo', 'supervisor.getAllProcessInfo', 'supervisor.getIdentification', 'supervisor.getPID', 'supervisor.getProcessInfo', 'supervisor.getState', 'supervisor.getSupervisorVersion', 'supervisor.getVersion', 'supervisor.readLog', 'supervisor.readMainLog', 'supervisor.readProcessLog', 'supervisor.readProcessStderrLog', 'supervisor.readProcessStdoutLog', 'supervisor.reloadConfig', 'supervisor.removeProcessGroup', 'supervisor.restart', 'supervisor.sendProcessStdin', 'supervisor.sendRemoteCommEvent', 'supervisor.shutdown', 'supervisor.startAllProcesses', 'supervisor.startProcess', 'supervisor.startProcessGroup', 'supervisor.stopAllProcesses', 'supervisor.stopProcess', 'supervisor.stopProcessGroup', 'supervisor.tailProcessLog', 'supervisor.tailProcessStderrLog', 'supervisor.tailProcessStdoutLog', 'system.listMethods', 'system.methodHelp', 'system.methodSignature', 'system.multicall']

Check the status of all running processes

In [7]: server.supervisor.getAllProcessInfo() Out[7]: [{'description': 'pid 6624, uptime 0:09:45', 'exitstatus': 0, 'group': 'simplehttp', 'logfile': '/tmp/simplehttp.stdout', 'name': 'simplehttp', 'now': 1412565184, 'pid': 6624, 'spawnerr': '', 'start': 1412564599, 'state': 20, 'statename': 'RUNNING', 'stderr_logfile': '/tmp/simplehttp.stderr', 'stdout_logfile': '/tmp/simplehttp.stdout', 'stop': 0}]

Identify the process name and check the status of the process

In [10]: server.supervisor.getProcessInfo('simplehttp') Out[10]: {'description': 'pid 6624, uptime 0:10:51', 'exitstatus': 0, 'group': 'simplehttp', 'logfile': '/tmp/simplehttp.stdout', 'name': 'simplehttp', 'now': 1412565250, 'pid': 6624, 'spawnerr': '', 'start': 1412564599, 'state': 20, 'statename': 'RUNNING', 'stderr_logfile': '/tmp/simplehttp.stderr', 'stdout_logfile': '/tmp/simplehttp.stdout', 'stop': 0}

Stop the process

In [11]: server.supervisor.stopProcess('simplehttp') Out[11]: True

Check the status of the process to see if it has stopped properly.

In [12]: server.supervisor.getProcessInfo('simplehttp') Out[12]: {'description': 'Oct 05 11:15 PM', 'exitstatus': -1, 'group': 'simplehttp', 'logfile': '/tmp/simplehttp.stdout', 'name': 'simplehttp', 'now': 1412565327, 'pid': 0, 'spawnerr': '', 'start': 1412564599, 'state': 0, 'statename': 'STOPPED', 'stderr_logfile': '/tmp/simplehttp.stderr', 'stdout_logfile': '/tmp/simplehttp.stdout', 'stop': 1412565323}

It stopped because it was STOPPED.

Run the process

In [13]: server.supervisor.startProcess('simplehttp') Out[13]: True

I was able to resume.

Recommended Posts

Try connecting to Supervisord via XMLRPC to start / stop the program
How to start the program
Try to write a program that abuses the program and sends 100 emails
Try to introduce the theme to Pelican
Cython to try in the shortest
The fastest way to try EfficientNet
The easiest way to try PyQtGraph
Try to scrape as much as you can scrape the sample program of Google Spreadsheet API (v4) quick start
Try to face the integration by parts
Python amateurs try to summarize the list ①
Various comments to write in the program
Try porting the "FORTRAN77 Numerical Computing Programming" program to C and Python (Part 1)
Try porting the "FORTRAN77 Numerical Computing Programming" program to C and Python (Part 3)
Try porting the "FORTRAN77 Numerical Computing Programming" program to C and Python (Part 2)