I was writing a Ruby program today that just needed a very simple way to control multiple web requests to a Sinatra server from querying a remote server at the same time. I would think that Sinatra does have some nice way to do this(and if you know it, please send me the link ), but I didn’t have time to dig into it, so I pulled out my old trusty Lockfile class.
The concept is very simple: Open a file, put a random string into it, close it. I use the random string to make sure that my process really did create the file, rather than another process running at the same time. The verify method can be called at any time to check that we still have the lock….well, that we are suppose to have the lock If the file already exists, then you don’t get the lock. If you can’t open the file, then someone else must have just opened it, and again no lock.
Now the source… Continue reading »