问题
I'm using Supervisor's events framework to subscribe to events from processes managed by Supervisor.
My event listener, processlistener.py
, looks like this:
import sys
from supervisor.childutils import listener
def write_stdout(s):
sys.stdout.write(s)
sys.stdout.flush()
def write_stderr(s):
sys.stderr.write(s)
sys.stderr.flush()
def main():
while True:
headers, body = listener.wait(sys.stdin, sys.stdout)
body = dict([pair.split(":") for pair in body.split(" ")])
write_stderr("Headers: %r\n" % repr(headers))
write_stderr("Body: %r\n" % repr(body))
if headers["eventname"] == "PROCESS_STATE_RUNNING":
write_stderr("Process state running...\n")
if __name__ == '__main__':
main()
In my supervisord.conf
, I have:
[program:theprogramname]
command=/bin/cat ; the program (relative uses PATH, can take args)
process_name=%(program_name)s_%(process_num)s ; process_name expr (default %(program_name)s)
numprocs=1 ; number of processes copies to start (def 1)
[eventlistener:theeventlistenername]
command=python processlistener.py ; the program (relative uses PATH, can take args)
process_name=%(program_name)s_%(process_num)s ; process_name expr (default %(program_name)s)
numprocs=1 ; number of processes copies to start (def 1)
events=PROCESS_STATE_RUNNING ; event notif. types to subscribe to (req'd)
With this configuration, I am expecting my event listener to be notified whenever a process managed by Supervisor enters the RUNNING
state. However, this is not the case. When I kill theprogramname
with a SIGINT
signal, the process is restarted by Supervisor, but my listener doesn't get notified of this.
Am I missing an extra piece of configuration in order to achieve what I want?
回答1:
This is because your listener needs to send a 'RESULT 2\nOK' response back to supervisord on stdout.
If supervisord doesn't see this response, it thinks that your listener is not ready and won't send any more events.
Try adding this inside your while loop:
listener.ok(sys.stdout)
This tells the listener to send the 'OK' response.
Here is the source for the ok
method in supervisor.childutils.listener
:
https://github.com/Supervisor/supervisor/blob/3.0/supervisor/childutils.py#L61
来源:https://stackoverflow.com/questions/17413914/how-to-subscribe-to-process-state-running-events-for-all-processes