newBike
newBike

Reputation: 15002

What's the better way to execute daemons when Rails server runing

I have some gems in my Rails App, such as resque, sunspot. I run the following command manually when the machines boots:

rake sunspot:solr:start 
/usr/local/bin/redis-server /usr/local/etc/redis.conf
rake resque:work QUEUE='*'

Is there a better practice to run these daemon in the background? And is there any side-effect when run these tasks run in the background?

Upvotes: 0

Views: 666

Answers (2)

kik
kik

Reputation: 7937

My solution to that is to use a mix of god, capistrano and whenever. A specific problem I have is that I want all app processes to be run as user, so initd scripts are not an option (this could be done, but it's quite a pain of user switching / environment loading).

God

The basic idea is to use god to start / restart / monitor processes. God may be difficult to get start with, but is very powerful :

  • running god alone will start all your processes (webserver, bg jobs, whatever)
  • it can detect a process crashed and restart it
  • you can group processes and batch restart them (staging, production, background, devops, etc)

Whenever

You still have to start god on server restart. A good mean to do so is to use user crontab. Most cron implementation have a special instruction called @reboot, which allows you to run a specific command on server restart :

@reboot /bin/bash -l -c 'cd /home/my_app && SERVER=true god -c production/current/config/app.god"

Whenever is a gem that allows easy management for crontab, including generating reboot command. While it's not absolutely necessary for achieving what I describe, it's really useful for its capistrano integration.

Capistrano

You not only want to start your processes on server restart, you also want to restart them on deploy. If your background jobs code is not up to date, problem will arise.

Capistrano allows to easily handle that, just ask god to restart the whole group (like : god restart production) in a post deploy capistrano task, and it will be handled seamlessly.

Whenever's capistrano integration also ensure your crontab is always up to date, updating it if you changed your config/schedule.rb file.

Upvotes: 1

Steve Robinson
Steve Robinson

Reputation: 3939

You can use something like foreman to manage these processes. You can define process types and other things in a Procfile and you can start and do whatever with them.

Upvotes: 0

Related Questions