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.
Simply create an initializer containing:
Better option:
Use https://github.com/FooBarWidget/daemon_controller
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.I wrote this shell script in config/thin_example.sh
Loosely modified from the unicorn scripts that Ryan Bates used in his VPS deployment railscast (pro only).
Make it executable
You'll need to symlink it to init.d (after chmod +x 'ing to make it executable)
Then if you want it to startup with the server
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.On Ubuntu, you should use the operating systems's init system - Upstart.
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.