/ God

Simple delayed_job configuration with God on Ubuntu

After Ryan Bates’ really awesome Railscast I figured it was time to get delayed_job happening in my application to process asynchronous stuff – in this case, delivering email “notifications”. If you want to know more about DJ, the Railscast serves as a great starting point.

DelayedJob is awesome, and comes with a rake task that spools up and runs the jobs. What I needed was a way to have Ubuntu control DJ – in particular, to automatically start up if the system has been rebooted… and to give me a quick easy way of probing for the status… and this is where God comes in – the pure ruby monitoring system that it’s author, Tom Preston-Werner describes as “like Monit, only awesome”.

For this I’m standing on the shoulders of giants – check out the links below to the places from which I’ve learned and obtained the DJ-fu.

1. Get delayed_job happy

There’s no scope here to go through how DJ works. A good starting point is the DJ config that the github guys use. Suffice to say… if it’s all apples, you should be able to run the rake task and get it to successfully execute:

rake jobs:work

2. Get God

sudo gem install god

3. Get Ubuntu geared up to go for it

For this I’m massively indebted to Tim Riley’s very cool post & script.

Start with a config file – we’ll put it in Ubuntu’s preferred place: /etc/default/god. This script is insanely simple – just sets a var that God uses to find our delayed_job config.

#config for GOD - I point it to a file in my RAILS_ROOT
GOD_CONFIG=/path/to/rails/config/delayed_job.god

And then get an init script – at /etc/init.d/god. This, adapted from Tim Riley’s one (see the link above), should do the trick – it points back to our file at /etc/default/god and sticks its logs and pids in Ubuntu-friendly places:

!/bin/sh

BEGIN INIT INFO

Provides: god

Required-Start: $all

Required-Stop: $all

Default-Start: 2 3 4 5

Default-Stop: 0 1 6

Short-Description: God

END INIT INFO

NAME=god DESC=god

set -e

Make sure the binary and the config file are present before proceeding

test -x /usr/bin/god || exit 0

Create this file and put in a variable called GOD_CONFIG, pointing to

your God configuration file

test -f /etc/default/god && . /etc/default/god [ $GOD_CONFIG ] || exit 0

. /lib/lsb/init-functions

RETVAL=0

case “$1” in start) echo -n “Starting $DESC: ” /usr/bin/god -c $GOD_CONFIG -P /var/run/god.pid -l /var/log/god.log RETVAL=$? echo “$NAME.” ;; stop) echo -n “Stopping $DESC: ” kill cat /var/run/god.pid RETVAL=$? echo “$NAME.” ;; restart) echo -n “Restarting $DESC: ” kill cat /var/run/god.pid /usr/bin/god -c $GOD_CONFIG -P /var/run/god.pid -l /var/log/god.log RETVAL=$? echo “$NAME.” ;; status) /usr/bin/god status RETVAL=$? ;; *) echo “Usage: god {start|stop|restart|status}” exit 1 ;; esac

exit $RETVAL

4. Tell Ubuntu to start using it at the default run levels.

This ensures that when the system starts up it’ll kick off our DJ task.

sudo update-rc.d god defaults

5. Try it out – fire her up

sudo /etc/init.d/god start
Starting god: god.

Checking the status should be equally simple.

sudo /etc/init.d/god status

dj:
  dj-0: up

That should have you up and running…