启动Rails服务器时自动运行Faye服务器

我目前让Faye使用我的Rails 3.0.9应用程序。 但是我在终端上打开了两个单独的标签。 一个用于Faye服务器,一个用于Rails服务器。 如何在Rails启动时集成它们并自动运行Faye服务器?

为了启动Faye Server,我正在运行:

rackup faye.ru -s thin -E production 

faye.ru

 require 'faye' faye_server = Faye::RackAdapter.new(:mount => '/faye') run faye_server 

如果您需要更多信息,请与我们联系。

只需创建一个初始化程序包含:

 Thread.new do system("rackup faye.ru -s thin -E production") end 

更好的选择:

使用https://github.com/FooBarWidget/daemon_controller

如今,我只是使用Foreman: https : //github.com/ddollar/foreman

通过创建Procfile,您可以指定需要运行哪些守护进程(通过控制每个守护进程中的多少个),并将所有守护进程保存在一个终端窗口中(每个进程都有很好的颜色编码)。 如果您的环境基于debian,它甚至可以导出到upstart或init.d脚本进行生产。

一旦你的Procfile全部设置完毕,你需要做的就是运行: foreman start并且你将参加比赛。 我把它用于resque和faye。

在Ubuntu上,您应该使用操作系统的init系统 – Upstart。

 user@host:~$ cat /etc/init/faye.conf description "Faye Upstart script" start on startup stop on shutdown respawn script env RAILS_ENV=production exec sudo -u deployuser -i /home/deployuser/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/bin/rackup /var/www/booko.com.au/booko/faye.ru -s thin -E production end script 

我对调用Ruby的方法不满意,因为它会改变。 但优点是它将在系统启动时启动,如果它死亡或你杀了它将重生。

让Upstart负责妖魔化一个进程并确保它继续运行。

我在config / thin_example.sh中编写了这个shell脚本

 #!/bin/sh set -e # Feel free to change any of the following variables for your app: TIMEOUT=${TIMEOUT-60} APP_ROOT=/home/deployer/apps/example/current PID=$APP_ROOT/tmp/pids/thin.pid CMD="cd $APP_ROOT; bundle exec rackup -D -P $PID $APP_ROOT/config/faye.ru -s thin -E production" AS_USER=deployer set -u startme() { run "$CMD" } stopme() { run "pkill -f $PID" } run () { if [ "$(id -un)" = "$AS_USER" ]; then eval $1 else su -c "$1" - $AS_USER fi } case "$1" in start) startme ;; stop) stopme ;; restart) stopme; startme ;; *) echo "usage: $0 start|stop|restart" >&2 exit 1 ;; esac 

从Ryan Bates在他的VPS部署railscast中使用的独角兽脚本中进行了松散的修改(仅限专业版) 。

让它可执行

 chmod +x config/thin_example.sh 

你需要将它符号链接到init.d(在chmod + x之后使其可执行)

 sudo ln -nfs /home/deployer/apps/example/current/config/thin_example.sh /etc/init.d/thin_example 

然后,如果您希望它与服务器启动

  sudo update-rc.d thin_example defaults 

否则你应该能够/etc/init.d/thin_example [start|stop|restart] 。 需要注意的一点是,我告诉rackup以守护进程模式启动(-D)并显式设置PID,以便我可以在以后杀死它。