2013-07-03 109 views
19

我有[program:A][program:B]在我supervisord.conf蟒蛇supervisord程序依赖

B取决于A,意思是:

A应该B之前启动。

如何由主管保证呢?

+0

它可能值得看看[ordered-startup-supervisord](https://pypi.python.org/pypi/ordered-startup-supervisord/) –

回答

16

supervisord不直接支持依赖关系。您的选项改为:

  • 使用优先级。将priority设置为A的值较低,它将在B之前启动,并在B之后关闭。为priority的默认值是999

    如果您将两个程序放入一个组中,那么您可以让它们一起启动和停止它们,优先级调节它们的启动和停止顺序。

  • 写一个event listener侦听PROCESS_STATESTARTING至 - RUNNING过渡和ASTOPPING事件,然后指示supervisord开始,并根据这些事件停止B。有A自动启动,但B禁用。自动启动,那么该事件处理程序来控制它。

+5

我的理解是,上面的“优先级”方法很可能无法按预期工作。 Supervisord确实会按顺序启动进程,但例如[它不会等待进程A在开始进程B之前完全启动](https://github.com/Supervisor/supervisor/issues/122)(即,supervisord将忽略“startsecs”参数)。相反,它会在A之后立即启动B,在很多情况下,B不会满足你的要求。这种行为[已于2012年报告,但截至2014年仍未修复](https://github.com/Supervisor/supervisor/issues/122)。 –

+0

@miguno:那么你的选择是使用事件监听器。 –

5

如果你想走捷径,并跳过阅读文档有关event listeners并跳过修改你的计划,使他们了解事件,则:直接

启动程序B(取决于A),而不是,你可以启动休眠,直到A已经启动一个bash脚本,然后开始B。举例来说,如果你有一个PostgreSQL数据库和服务器不应该PostgreSQL的前开始:

[program:server] 
autorestart=true 
command=/.../start-server.sh 

[program:postgres] 
user=postgres 
autorestart=true 
command=/usr/lib/postgresql/9.3/bin/postgres ... 

然后里面start-server.sh

#!/bin/bash 

# Wait until PostgreSQL started and listens on port 5432. 
while [ -z "`netstat -tln | grep 5432`" ]; do 
    echo 'Waiting for PostgreSQL to start ...' 
    sleep 1 
done 
echo 'PostgreSQL started.' 

# Start server. 
echo 'Starting server...' 
/.../really-start-the-server 
2

this对我来说是一个很好的解决方案!

,我使用的解决方法是设置autostart=false上 进程,然后创建自举脚本autostart=trueautorestart=false(一步法)。引导程序可以是一个shell脚本 为每个进程调用supervisorctl startsupervisorctl start 将阻塞,直到一个进程已成功启动。

1

一个解决方案是使用supervisorctl:为程序B设置自动启动为false,并在由A启动的程序中写入supervisorctl start B

例子:

supervisor.cfg

[supervisord] 
nodaemon=false 
pidfile=/tmp/supervisor.pid 
logfile=/logs/supervisor.log 

[unix_http_server] 
file=/var/run/supervisor.sock 

[rpcinterface:supervisor] 
supervisor.rpcinterface_factory=supervisor.rpcinterface:make_main_rpcinterface 

[supervisorctl] 
serverurl=unix:///var/run/supervisor.sock 

[program:A] 
command=do_a 

[program:B] 
command=do_b 
autostart=false 

do_a程序包含:

#!/bin/bash 
#do things 
supervisorctl start B 

TBH它是由@drrzmr建议的解决方案,但我没有时间了解它。