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

时间:2011-06-21 18:49:21

标签: ruby-on-rails ruby-on-rails-3 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

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

4 个答案:

答案 0 :(得分:13)

只需创建一个包含以下内容的初始化程序:

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

更好的选择:

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

答案 1 :(得分:7)

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

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

一旦你的Procfile全部设置完毕,你需要做的就是运行:foreman start然后你就开始了比赛。我把它用于resque和faye。

答案 2 :(得分:3)

在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负责妖魔化一个进程并确保它继续运行。

答案 3 :(得分:3)

我在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 deployment railscast (pro only)中使用的独角兽脚本中轻松修改。

使其可执行

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,以便我可以在以后杀死它。