bdunagan

Brian Dunagan

October 1 2011
Rails Tip: Locking Cron Jobs

I really like using the Ruby whenever gem for managing cron jobs in Rails. But recently I needed to ensure there was only one instance of a cron job running. If it took a bit too long, I didn’t want another instance starting up in parallel. Enter flock.

task :do_stuff_for_a_while => :environment do
  # Ensure there is only one instance running.
  return if File.new(__FILE__).flock(File::LOCK_EX | File::LOCK_NB) == false

  # Do some stuff.
end
Converting MySQL from LATIN1 to UTF8 Rack Tip: ActiveRecord with Logging
LinkedIn GitHub Email