如何为所有进程订阅 PROCESS_STATE_RUNNING 事件 [英] How to subscribe to PROCESS_STATE_RUNNING events for all processes

查看:51
本文介绍了如何为所有进程订阅 PROCESS_STATE_RUNNING 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Supervisor 的事件框架来订阅来自 Supervisor 管理的进程的事件.

I'm using Supervisor's events framework to subscribe to events from processes managed by Supervisor.

我的事件侦听器 processlistener.py 如下所示:

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()

在我的 supervisord.conf 中,我有:

[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)

使用此配置,我希望每当 Supervisor 管理的进程进入 RUNNING 状态时,我的事件侦听器都会收到通知.然而,这种情况并非如此.当我用 SIGINT 信号杀死 theprogramname 时,进程由主管重新启动,但我的听众没有收到通知.

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?

推荐答案

这是因为您的侦听器需要在 stdout 上将RESULT 2\nOK"响应发送回 supervisord.

This is because your listener needs to send a 'RESULT 2\nOK' response back to supervisord on stdout.

如果 supervisord 没有看到这个响应,它认为你的监听器没有准备好,不会再发送任何事件.

If supervisord doesn't see this response, it thinks that your listener is not ready and won't send any more events.

尝试将其添加到您的 while 循环中:

Try adding this inside your while loop:

listener.ok(sys.stdout)

这告诉侦听器发送OK"响应.

This tells the listener to send the 'OK' response.

这里是 supervisor.childutils.listenerok 方法的源代码:https://github.com/Supervisor/supervisor/blob/3.0/supervisor/childutils.py#L61

Here is the source for the ok method in supervisor.childutils.listener: https://github.com/Supervisor/supervisor/blob/3.0/supervisor/childutils.py#L61

这篇关于如何为所有进程订阅 PROCESS_STATE_RUNNING 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆