I have a process that runs on cron every five minutes. Usually, it takes only a few seconds to run, but sometimes it takes several minutes. I want to ensure that only one version of this is running at a time.
I tried an obvious way…
File.open('/tmp/indexer_lock.tmp','w') do |f| exit unless f.flock(File::LOCK_EX) end
…but it’s not testing to see if it can get the lock, it’s blocking until the lock is released.
Any idea what I’m missing? I’d rather not hack something using ps, but that’s an alternative.
Although this isn’t directly answering your question, if I were you I’d probably write a daemon script (you could use http://daemons.rubyforge.org/)
You could have your indexer (assuming its indexer.rb) be run through a wrapper script named script/index for example:
And your indexer can do almost the same thing, except you specify a sleep interval
This is how job processors in tandem with a queue server usually function.