Mutex for Rails Processes

前端 未结 3 1083
遇见更好的自我
遇见更好的自我 2021-01-03 02:22

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 res

相关标签:
3条回答
  • 2021-01-03 02:38

    As far as I know, the only way to do this in an environment like this is to use a file-based semaphore - touch a lockfile, do your work, remove the lockfile. Make the process fail if there's a lock on the file.

    You could also have a service that writes to the file that is threaded, and make the apps talk to the service to modify the file rather than letting them modify the file directly.

    0 讨论(0)
  • 2021-01-03 02:45

    If you just need to prevent multiple writers from working with a file simultaneously, you can use the File#flock method to request an exclusive write lock from each process:

    fh = File.new("/some/file/path")
    begin
      fh.flock(File::LOCK_EX)
      # ... write to the file here, or perform some other critical operation
    ensure
      fh.flock(File::LOCK_UN)
    end
    

    Note: putting the unlock call in an ensure block is important to prevent deadlock if an uncaught exception is thrown after you've locked the file.

    0 讨论(0)
  • 2021-01-03 02:47

    You could use a background job scheduler to do the actual work, for example delayed_job (http://github.com/tobi/delayed_job).

    0 讨论(0)
提交回复
热议问题