July 29, 2010

“Capistrano doesn't play well with &”

Recently I had to deploy some code to a server, and part of it required me to run init scripts that backgrounded processes using &, which Capistrano doesn’t play well with. The processes would terminate when Capistrano was finished with the deployment.

As a fix, I ended up writing a task manager using the “daemons ruby gem”:http://daemons.rubygems.org.

In this case, we have some Ruby scripts that are “gearman”:http://www.gearman.org workers. This solution has a nice side-effect of giving us process control and pid-files as well for free!

require 'rubygems'
require 'daemons'

this_dir = File.expand_path(File.dirname(__FILE__))
files = Dir.glob("#{this_dir}/workers/*.rb")
workername = ARGV.shift

files.each do |script|
  worker = script.split("/")[-1]

  if worker == workername