Jeff Kreeftmeijer

Daemonizing Navvy with God

2010-05-31

In version 0.2.1, Navvy introduced a really simple Daemon script to allow users to run Navvy’s worker in the background.

While some people asked for this, you might argue that running rake navvy:work & would do the exact same thing. It runs the worker, like the rake task does. It only does it in the background, like rake’s & argument does.

To try and get some more control over my Worker process, I decided to let God daemonize it. This allowed me to monitor my process instead of just starting it and hoping it would do it’s thing. Also, God allows you to set memory limits and automatically restart — and notify me — when the worker dies.

God config

I’m assuming you put this block in a Rakefile somewhere to load Navvy’s tasks:

begin
  require 'navvy/tasks'
rescue LoadError
  namespace :navvy do
    abort "Couldn't find Navvy. " << 
      "Please run `gem install navvy` to use Navvy's tasks."
  end
end

Now, we’ll create a God configuration file called navvy.god in the same directory as our Rakefile:

God.watch do |w|
  w.name     = "navvy"
  w.interval = 30.seconds # default
  w.start    = "rake navvy:work"
  w.dir      = File.dirname(__FILE__)
  w.log      = "log/navvy.log"

  # determine the state on startup
  w.transition(:init, { true => :up, false => :start }) do |on|
    on.condition(:process_running) do |c|
      c.running = true
    end
  end

  # determine when process has finished starting
  w.transition([:start, :restart], :up) do |on|
    on.condition(:process_running) do |c|
      c.running = true
      c.interval = 5.seconds
    end
  
    # failsafe
    on.condition(:tries) do |c|
      c.times = 5
      c.transition = :start
      c.interval = 5.seconds
    end
  end

  # start if process is not running
  w.transition(:up, :start) do |on|
    on.condition(:process_running) do |c|
      c.running = false
    end
  end
end

This config will start a process called navvy. It’ll check if the process is still running every 30 seconds and restart it if it isn’t.

Now, you can start the worker by running:

$ god -c navvy.god

Check the worker’s status:

$ god status

Or stop it:

$ god stop navvy

Cool, huh?

What about Navvy’s daemon script?

What I’ve shown you does exactly what Navvy’s included daemon script does, but keeps monitoring it. If you want more — like email notifications and memory limits — check out God’s website.

I’m thinking about completely removing the daemon script from Navvy in the next version and encouraging users to start using some kind of process monitor, since I believe handling background processes is none of Navvy’s business. Navvy is a job processor. I think we should focus on that.

What do you think? Your opinion is always welcome.