使用 upstart 运行多个实例

使用 upstart 运行多个实例

我编写了一个需要在多个端口上运行的服务器。

因此我按照 Upstart 文档的建议编写了两个 Upstart 脚本。http://upstart.ubuntu.com/cookbook/#instance

生成实例的任务:

# gatling-broadcast.conf
description "Start multiple gatling-broadcast servers"
console output

start on runlevel [2345]

task

script
    for i in `seq 21001 21004`; do
        start gatling-broadcast-worker PORT=$i
    done
end script

实例脚本:

# gatling-broadcast-worker.conf
description "Gatling-broadcast server."
instance $PORT  
env ROOT="/home/ctargett/Projects/msgqueue"
exec $ROOT/env/bin/pypy $ROOT/main.py --mode=broadcast --port=$PORT --log_file_prefix=/tmp/gatling-broadcast-$PORT.log

我可以gatling-broadcast-worker很好地运行该作业的多个版本:

$ initctl --session start gatling-broadcast-worker PORT=21001
gatling-broadcast-worker (21001) start/running, process 20956
$ initctl --session start gatling-broadcast-worker PORT=21002
gatling-broadcast-worker (21002) start/running, process 20963
$ initctl --session list
gatling-broadcast stop/waiting
gatling-monitor stop/waiting
gatling-broadcast-worker (21002) start/running, process 20963
gatling-broadcast-worker (21001) start/running, process 20956

然而,当我尝试开始gatling-broadcast工作时,出现错误:

$ initctl --session start gatling-broadcast
initctl: Job failed to start

除了以下输出之外,没有任何init帮助init: gatling-broadcast main process (21007) terminated with status 1

Loading configuration from /home/ctargett/Projects/msgqueue/upstart
job_class_unregister: Unregistered job /com/ubuntu/Upstart/jobs/gatling_2dbroadcast
conf_file_destroy: Destroyed unused job gatling-broadcast
conf_reload_path: Loading gatling-broadcast from /home/ctargett/Projects/msgqueue/upstart/gatling-broadcast.conf
parse_job: Creating new JobClass gatling-broadcast
job_class_register: Registered job /com/ubuntu/Upstart/jobs/gatling_2dbroadcast
job_class_unregister: Unregistered job /com/ubuntu/Upstart/jobs/gatling_2dbroadcast_2dworker
conf_file_destroy: Destroyed unused job gatling-broadcast-worker
conf_reload_path: Loading gatling-broadcast-worker from /home/ctargett/Projects/msgqueue/upstart/gatling-broadcast-worker.conf
parse_job: Creating new JobClass gatling-broadcast-worker
job_class_register: Registered job /com/ubuntu/Upstart/jobs/gatling_2dbroadcast_2dworker
job_class_unregister: Unregistered job /com/ubuntu/Upstart/jobs/gatling_2dmonitor
conf_file_destroy: Destroyed unused job gatling-monitor
conf_reload_path: Loading gatling-monitor from /home/ctargett/Projects/msgqueue/upstart/gatling-monitor.conf
parse_job: Creating new JobClass gatling-monitor
job_class_register: Registered job /com/ubuntu/Upstart/jobs/gatling_2dmonitor

job_register: Registered instance /com/ubuntu/Upstart/jobs/gatling_2dbroadcast/_
gatling-broadcast goal changed from stop to start
gatling-broadcast state changed from waiting to starting
event_new: Pending starting event
Handling starting event
event_finished: Finished starting event
gatling-broadcast state changed from starting to pre-start
gatling-broadcast state changed from pre-start to spawned
gatling-broadcast main process (21007)
gatling-broadcast state changed from spawned to post-start
gatling-broadcast state changed from post-start to running
event_new: Pending started event
Handling started event
event_finished: Finished started event
init: gatling-broadcast main process (21007) terminated with status 1
gatling-broadcast goal changed from start to stop
gatling-broadcast state changed from running to stopping
event_new: Pending stopping event
Handling stopping event
event_finished: Finished stopping event
gatling-broadcast state changed from stopping to killed
gatling-broadcast state changed from killed to post-stop
gatling-broadcast state changed from post-stop to waiting
event_new: Pending stopped event
job_change_state: Destroyed inactive instance gatling-broadcast
Handling stopped event
event_finished: Finished stopped event

答案1

我猜问题是gatling-broadcast无法启动一个或多个实例,可能是因为它们已经正在运行。请记住,Upstyart 使用“sh -e”运行您的作业,因此如果任何简单命令失败,脚本将立即退出。参见:

http://upstart.ubuntu.com/cookbook/#debugging-a-script-which-appears-to-be-behaving-oddly

如果您正在运行 Ubuntu Precise,请查看/var/log/upstart/gatling-broadcast.log。您可能需要set -x在脚本节顶部添加“ ”,以准确查看失败的位置。

修复方法很简单:

script
    for i in `seq 21001 21004`; do
        start gatling-broadcast-worker PORT=$i || true
    done
end script

不过,更好的解决方案是检查作业特定实例的状态gatling-broadcast-worker,如果未运行,则尝试启动它。如果失败,则采取一些适当的措施。

相关内容