Giter VIP home page Giter VIP logo

filelock's Introduction

Filelock Build Status

Heavily tested, but simple filelocking solution using flock command. It guarantees unlocking of files.

It works for sure on MRI 1.8, 1.9, 2.0, JRuby in both 1.8 and 1.9 mode, and Rubinius.

This gem doesn't support NFS. You can use it with GlusterFS, though.

Basic Usage

Filelock '/tmp/path/to/lock' do
  # do blocking operation
end

Operation Timeout

You can also pass the timeout for blocking operation (default is 60 seconds):

Filelock '/tmp/path/to/lock', :timeout => 10 do
  # do blocking operation
end

You can detect timeout by catching Filelock::ExecTimeout.

Lock Acquiring Timeout

You can also pass a non-zero wait timeout for grabbing the lock (default is 1 day):

Filelock '/tmp/path/to/lock', :wait => 3600 do
  # do blocking operation
end

You can detect this kind of timeout by catching Filelock::WaitTimeout. Note that a wait timeout of 0 will block until the lock can be acquired instead of raising Filelock::WaitTimeout.

Note that lock file directory must already exist, and lock file is not removed after unlock.

Getting handle to locked file

Filelock '/tmp/path/to/lock' do |file|
  file.truncate
  file.write Process.pid
end

FAQ

Does it support NFS?

No. You can use more complex lockfile gem if you want to support NFS.

The code is so short. Why shouln't I just copy-paste it?

Because even such short code can have issues in future. File locking is very fragile operation. You may expect new releases of this gem fixing discovered bogus behavior (or introducing awesome features).

You are encouraged to use it if you develop gem that uses flock command, and care about running it on different ruby versions and platforms. Each has its own quirks with regard to flock command.

How it's different from lockfile gem?

Lockfile is filelocking solution handling NFS filesystems, based on homemade locking solution. Filelock uses flock UNIX command to handle filelocking on very low level. Also lockfile allows you to specify retry timeout. In case of Ruby's flock command this is hard-cored to 0.1 seconds.

How it's different from cleverua-lockfile gem?

Cleverua removes lockfile after unlocking it. Thas has been proven fatal both in my tests and in filelocking advices from the Internet. You could try find a way to remove lock file without breaking Filelock tests. I will be glad to accept such pull-request.

What happens if my ruby process gets killed while holding a lock? Can new processes acquire this lock?

Yes

Contribute

Try to break Filelock in some way (note it doesn't support NFS).

License

Filelock is MIT-licensed. You are awesome.

filelock's People

Contributors

airblade avatar fermuch avatar jasonwbarnett avatar kylekyle avatar sheerun avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

filelock's Issues

uninitialized constant Filelock::Timeout

Running the following example from the README:

require 'filelock'

Filelock '/tmp/path/to/lock', :wait => 3600 do
  # do blocking operation
end

results in the following stack trace:

/Users/kyle/.rvm/gems/ruby-2.2.1/gems/filelock-1.1.0/lib/filelock/exec_timeout.rb:2:in `<module:Filelock>': uninitialized constant Filelock::Timeout (NameError)
    from /Users/kyle/.rvm/gems/ruby-2.2.1/gems/filelock-1.1.0/lib/filelock/exec_timeout.rb:1:in `<top (required)>'
    from /Users/kyle/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Users/kyle/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Users/kyle/.rvm/gems/ruby-2.2.1/gems/filelock-1.1.0/lib/filelock.rb:2:in `<top (required)>'
    from /Users/kyle/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:128:in `require'
    from /Users/kyle/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:128:in `rescue in require'
    from /Users/kyle/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:39:in `require'
    from lock.rb:1:in `<main>'

Moving the require "timeout" to the top of firelock.rb fixes the issue.

Pass file to block

Since the block has a exclusive lock on the file, you might as well let the user have it. They can ignore it if they choose. For example:

require 'filelock'

Filelock '/tmp/path/to/lock', :wait => 3600 do |file|
  file.truncate
  file.write Process.pid
end

NoMethodError: undefined method `[]' for #<Filelock::WaitTimeout: execution expired>

Hi!

Seeing this in production sporadically:

NoMethodError: undefined method `[]' for #<Filelock::WaitTimeout: execution expired>

File "/opt/rubies/ruby-2.3.0/lib/ruby/gems/2.3.0/gems/filelock-1.1.1/lib/filelock.rb" line 19 in flock
File "/opt/rubies/ruby-2.3.0/lib/ruby/gems/2.3.0/gems/filelock-1.1.1/lib/filelock.rb" line 19 in block (2 levels) in Filelock
File "/opt/rubies/ruby-2.3.0/lib/ruby/2.3.0/timeout.rb" line 101 in timeout
File "/opt/rubies/ruby-2.3.0/lib/ruby/gems/2.3.0/gems/filelock-1.1.1/lib/filelock.rb" line 19 in block in Filelock
File "/opt/rubies/ruby-2.3.0/lib/ruby/gems/2.3.0/gems/filelock-1.1.1/lib/filelock.rb" line 18 in open
File "/opt/rubies/ruby-2.3.0/lib/ruby/gems/2.3.0/gems/filelock-1.1.1/lib/filelock.rb" line 18 in Filelock

Reading https://github.com/sheerun/filelock/blob/master/lib/filelock.rb , I can't make sense out of the error.

Cheers - Victor

FAQ: what happens when killing a process holding a lock?

Hey there again,

I could see in the test suite (and checked myself in my server) that when a ruby process holding a lock gets killed, then a new process can grab the lock with no issue. Great!

This concern is probably a FAQ. Would you add this question...

What happens if my ruby process gets killed while holding a lock? Can new processes acquire this lock?

...and answer accurately? It's awesome that it works as expected, but it would be great to know why (the OS is smart? your ruby implementation is smart?).

Cheers - Victor

Append to lock file

@sheerun Would it be possible to "override" File::RDWR|File::CREAT so that we can actually append for example to the file ?

Clarify need to place locks in /tmp

Hi!

The examples in the readme go like this:

Filelock '/tmp/path/to/lock' do
  # do blocking operation
end

It calls my attention that the lock file is placed at /tmp. Is this the adviced directory to place our file locks?

I'd be more inclined to store them in Rails.root or in Rails.root.join('tmp'), because:

  • that way, these files are more prominently visible
  • /tmp sounds like it can be swiped by the OS at any time?

Note that I'm not experiencied with filelocks, so I'm not biased towards any specific approach. Just asking!

Cheers - Victor

Throws an exception when "wait" timeout is exceeded

Is it possible to have it do nothing and pass by the block when the wait timeout is exceeded instead of raising an exception?

My use case is in a cron job. If a previous instance of the process is already running I don't want to wait around. I'll just come back and run next time.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.