When deploying Rails via Passenger or Mongrel you have multiple instances of the application running. What is the best practice or pattern to establish a mutex on shared resources such as a writing to a local file or to a remote file. I want to ensure two processes are not writing to the same resource at the same time.
When deploying Rails via Passenger or Mongrel you have multiple instances of the application
Share
If you just need to prevent multiple writers from working with a file simultaneously, you can use the
File#flockmethod to request an exclusive write lock from each process:Note: putting the unlock call in an
ensureblock is important to prevent deadlock if an uncaught exception is thrown after you’ve locked the file.