How to subscribe to PROCESS_STATE_RUNNING events for all processes

我们两清 提交于 2020-01-15 12:31:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!