Supervisor & Docker: How to exit Supervisor if a service doesn't start?

為{幸葍}努か 提交于 2019-12-06 05:21:00

问题


I'm currently using Supervisor inside my Docker images to start and manage my services and I would like to configure Supervisor to exit if at least one of these services entered FATAL state.

Doing that, I want to avoid to have Docker containers in running state when nothing except Supervisor has succeeded to start.


回答1:


You can do this with a Supervisor event listener. Subscribe it to the event PROCESS_STATE_FATAL, and respond to the event by sending a SIGTERM to supervisord, which you are presumably running as PID 1 within the container.




回答2:


As mhsmith mentioned, an event listener is the best way to achieve this. You could use the following listener (ADD this as e.g. /usr/local/bin/exit-event-listener):

#!/usr/bin/env python
import os
import signal

from supervisor import childutils

def main():
    while True:
        headers, payload = childutils.listener.wait()
        childutils.listener.ok()
        if headers['eventname'] != 'PROCESS_STATE_FATAL':
            continue
        os.kill(os.getppid(), signal.SIGTERM)

if __name__ == "__main__":
    main()

And then, register it with supervisor:

[eventlistener:exit_on_any_fatal]
command=exit-event-listener
events=PROCESS_STATE_FATAL


来源:https://stackoverflow.com/questions/28175011/supervisor-docker-how-to-exit-supervisor-if-a-service-doesnt-start

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