Autorun the Faye server when I start the Rails server
Asked Answered
C

4

17

I currently have Faye working with my Rails 3.0.9 application. However I have two separate tabs open in my terminal. One for the Faye server, and one for the Rails server. How can I integrate them and automatically run the Faye server when Rails starts up?

To start the Faye Server, I am running:

rackup faye.ru -s thin -E production

faye.ru

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

Please let me know if you need any more information.

Commandant answered 21/6, 2011 at 18:49 Comment(5)
just fyi: groups.google.com/group/faye-users/browse_thread/thread/…Congeal
Is that last message saying that it's not possible then?Commandant
Nope. I don't have Faye installed but I created an initializer and put system("ruby #{Rails.root}/test_script.rb"). This works like a charm. Try to create an initializer with system("rackup faye.ru -s thin -E production")Congeal
That seems to work. Please add this as an answer and I will mark it. Thank you for your help my friend.Commandant
Glad to know it helped. You're welcome man ;)Congeal
C
13

Simply create an initializer containing:

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

Better option:

Use https://github.com/FooBarWidget/daemon_controller

Congeal answered 21/6, 2011 at 19:37 Comment(2)
You sir, are amazing. Thank you very much. You are not obligated, but you may be interested in another question of mine related to this one. Found: #6441352Commandant
A posteriori, the need for another thread is obvious otherwise the main program can't continue once it launches the commandCongeal
F
7

Nowadays, I'd just use Foreman for this: https://github.com/ddollar/foreman

By creating a Procfile, you can specify which daemons need to run (with control for how many of them of each you want), and keeps everything in one terminal window (with great color coding of each process). It can even export to upstart or init.d scripts for production, if your environment is debian based.

Once your Procfile is all set up, then all you need to do is run: foreman start and you're off to the races. I use it for resque and faye.

Frankpledge answered 21/10, 2011 at 3:55 Comment(0)
F
3

On Ubuntu, you should use the operating systems's init system - 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 

I'm not happy with the method of calling Ruby since it will change. But the advantages are that it will start when the system starts and it will respawn if it dies or you KILL it.

Let Upstart take care of demonising a process and making sure it keeps running.

Fogged answered 11/2, 2012 at 7:59 Comment(2)
this seems appealing but unfortunately returns start: Unknown job: faye I can start the line in exec independently just fineJulietjulieta
@Julietjulieta try foreman, which I recommended above. It can export to upstart scripts which should make it easier to maintainFrankpledge
S
3

I wrote this shell script in config/thin_example.sh

#!/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

Loosely modified from the unicorn scripts that Ryan Bates used in his VPS deployment railscast (pro only).

Make it executable

chmod +x config/thin_example.sh

You'll need to symlink it to init.d (after chmod +x 'ing to make it executable)

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

Then if you want it to startup with the server

 sudo update-rc.d thin_example defaults

Otherwise you should just be able to /etc/init.d/thin_example [start|stop|restart]. An important point to note is that I'm telling rackup to start in daemon mode (-D) and explicitly setting the PID so I can kill it later.

Sacksen answered 25/9, 2012 at 3:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.