Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I ensure only one instance of a Ruby script is running at a time?

Tags:

linux

ruby

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.

like image 802
Jason Butler Avatar asked Mar 19 '09 10:03

Jason Butler


5 Answers

Here's a one-liner that should work at the top of any Ruby script:

exit unless File.new(__FILE__)).tap {|f| f.autoclose = false}.flock(File::LOCK_NB | File::LOCK_EX)

There are two issues with the original code.

First, the reason it's blocking is that the call to #flock is missing File::LOCK_NB:

Don't block when locking. May be combined with other lock options using logical or.

Second, if a File object is closed (whether at the end of an #open block as in the code above, via explicit #close, or implicitly auto-closed when the File is garbage-collected), the underlying file descriptor is closed and the lock is released. To prevent this you can set #autoclose =false.

like image 161
wjordan Avatar answered Sep 27 '22 22:09

wjordan


Depending on your needs, this should work just fine and doesn't require creating another file anywhere.

exit unless DATA.flock(File::LOCK_NB | File::LOCK_EX)

# your script here

__END__
DO NOT REMOVE: required for the DATA object above.
like image 21
Matt Todd Avatar answered Nov 14 '22 04:11

Matt Todd


I know this is old, but for anyone interested, there's a non-blocking constant that you can pass to flock so that it returns instead of blocking.

File.new("/tmp/foo.lock").flock( File::LOCK_NB | File::LOCK_EX )

Update for slhck

flock will return true if this process received the lock, false otherwise. So to ensure just one process is running at a time you just want to try to get the lock, and exit if you weren't able to. It's as simple as putting an exit unless in front of the line of code I have above:

exit unless File.new("/tmp/foo.lock").flock( File::LOCK_NB | File::LOCK_EX )
like image 24
smathy Avatar answered Nov 14 '22 02:11

smathy


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:

require 'rubygems'
require 'daemons'

Daemons.run('indexer.rb')

And your indexer can do almost the same thing, except you specify a sleep interval

loop do
   # code executing your indexing 

   sleep INDEXING_INTERVAL
end

This is how job processors in tandem with a queue server usually function.

like image 3
ucron Avatar answered Nov 14 '22 04:11

ucron


You could create and delete a temporary file and check for existence of this file. Please check the answer to this question : one instance shell script

like image 3
shodanex Avatar answered Nov 14 '22 04:11

shodanex