filelock 1.0.3 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ca5c3766ead1d38142a6a07fff24bb2b84eb1bf8
4
- data.tar.gz: 7fa07b19de6de7a852412b07a3425d6992a0fbb2
3
+ metadata.gz: a4d5bba388f19de54cba97e0af369c8c18764bb5
4
+ data.tar.gz: 5a42b3b0e31fd1f9b2f02d1f1eef513a9112244e
5
5
  SHA512:
6
- metadata.gz: cea750b7cf162bb5c7924a442a29acb02cb0ea7a2cd26baa29e3c3227e55a238bcf8ce6cc01bfca1d611f6b43aaa66481a6efeb4b00afea9e5389d62787997d7
7
- data.tar.gz: 78f0673ea1fbadc2658c3e455b7d5eadb1eb5b6522313e6944bafc4314ec4d45fbcd5f2633cdefe1a36552d402c7fb06d1dc2a936ed7b4db257ccf22283e5aca
6
+ metadata.gz: cd10b0613b05bff7c4d9eb70985e9ffe1e3236d9e6a8f84d9d1cc77af3486bc1767c91a0345abfe551924a8d0bce088401c34ce9c0adb681dd663f8f087795d4
7
+ data.tar.gz: 9d4f60be252ceb0be614e8f6a701493aeff105db1b9e77c3c68c97dfe6d3756a44265985e62f3a3f32d1b593a7ea26afe2a2e73d5901a3eed2ecdfa331b99aec
@@ -0,0 +1,2 @@
1
+ Gemfile.lock
2
+ pkg
@@ -1,10 +1,14 @@
1
+ before_install:
2
+ - gem update bundler
1
3
  language: ruby
2
4
  script: bundle exec rspec
3
5
  rvm:
4
- - "1.8.7"
5
- - "1.9.3"
6
- - "1.9.2"
7
- - "2.0.0"
6
+ - "1.8"
7
+ - "1.9"
8
+ - "2.0"
9
+ - "2.1"
10
+ - "2.2"
11
+ - "2.3.0"
8
12
  - jruby-19mode
9
13
  - jruby-18mode
10
- - rbx-19mode
14
+ - rbx-2
@@ -0,0 +1,16 @@
1
+ # 1.1.0
2
+
3
+ - Add `:wait` flag for lock-acquiting timeout
4
+ - Add two new timeout exteptions: `FileLock::ExecTimeout` and `FileLock::WaitTimeout`
5
+
6
+ # 1.0.3
7
+
8
+ Fix for rubinius
9
+
10
+ # 1.0.2
11
+
12
+ Make filelock working for jruby
13
+
14
+ # 1.0.1
15
+
16
+ Test and fix more ruby versions
data/README.md CHANGED
@@ -1,13 +1,15 @@
1
1
  # Filelock [![Build Status][travis-img-url]][travis-url]
2
2
 
3
3
  [travis-img-url]: https://travis-ci.org/sheerun/filelock.png
4
- [travis-url]: https://travis-ci-org/sheerun/filelock
4
+ [travis-url]: https://travis-ci.org/sheerun/filelock
5
5
 
6
6
  Heavily tested, but simple filelocking solution using [flock](http://linux.die.net/man/2/flock) command. It guarantees unlocking of files.
7
7
 
8
8
  It works for sure on MRI 1.8, 1.9, 2.0, JRuby in both 1.8 and 1.9 mode, and Rubinius.
9
9
 
10
- ## Usage
10
+ This gem doesn't support NFS. You can use it with [GlusterFS](http://www.gluster.org/), though.
11
+
12
+ ## Basic Usage
11
13
 
12
14
  ```ruby
13
15
  Filelock '/tmp/path/to/lock' do
@@ -15,6 +17,8 @@ Filelock '/tmp/path/to/lock' do
15
17
  end
16
18
  ```
17
19
 
20
+ ### Operation Timeout
21
+
18
22
  You can also pass the timeout for blocking operation (default is 60 seconds):
19
23
 
20
24
  ```ruby
@@ -23,6 +27,20 @@ Filelock '/tmp/path/to/lock', :timeout => 10 do
23
27
  end
24
28
  ```
25
29
 
30
+ You can detect timeout by catching `Filelock::ExecTimeout`.
31
+
32
+ ### Lock Acquiring Timeout
33
+
34
+ You can also pass a wait timeout for grabbing the lock (default is 1 day):
35
+
36
+ ```ruby
37
+ Filelock '/tmp/path/to/lock', :wait => 3600 do
38
+ # do blocking operation
39
+ end
40
+ ```
41
+
42
+ You can detect this kind of timeout by catching `Filelock::WaitTimeout`.
43
+
26
44
  Note that lock file directory must already exist, and lock file is not removed after unlock.
27
45
 
28
46
  ## FAQ
@@ -45,11 +63,9 @@ Lockfile is filelocking solution handling NFS filesystems, based on homemade loc
45
63
 
46
64
  Cleverua removes lockfile after unlocking it. Thas has been proven fatal both in my tests and in [filelocking advices from the Internet](http://world.std.com/~swmcd/steven/tech/flock.html). You could try find a way to remove lock file without breaking Filelock tests. I will be glad to accept such pull-request.
47
65
 
48
- ## Challenge
49
-
50
- Please try to break Filelock in some way (note it doesn't support NFS).
66
+ ## Contribute
51
67
 
52
- If you show at least one failing test, I'll put your name below:
68
+ Try to break Filelock in some way (note it doesn't support NFS).
53
69
 
54
70
  ## License
55
71
 
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_development_dependency "bundler", "~> 1.3"
21
+ spec.add_development_dependency "bundler", "> 1.3"
22
22
  spec.add_development_dependency "rake"
23
23
  spec.add_development_dependency "rspec"
24
24
  end
@@ -1,4 +1,6 @@
1
1
  require 'filelock/version'
2
+ require 'filelock/exec_timeout'
3
+ require 'filelock/wait_timeout'
2
4
  require 'timeout'
3
5
  require 'tempfile'
4
6
 
@@ -6,16 +8,16 @@ if RUBY_PLATFORM == "java"
6
8
  def Filelock(lockname, options = {}, &block)
7
9
  lockname = lockname.path if lockname.is_a?(Tempfile)
8
10
  File.open(lockname, File::RDWR|File::CREAT, 0644) do |file|
9
- Thread.pass until file.flock(File::LOCK_EX)
10
- Timeout::timeout(options.fetch(:timeout, 60)) { yield }
11
+ Thread.pass until Timeout::timeout(options.fetch(:wait, 60*60*24), Filelock::WaitTimeout) { file.flock(File::LOCK_EX) }
12
+ Timeout::timeout(options.fetch(:timeout, 60), Filelock::ExecTimeout) { yield }
11
13
  end
12
14
  end
13
15
  else
14
16
  def Filelock(lockname, options = {}, &block)
15
17
  lockname = lockname.path if lockname.is_a?(Tempfile)
16
18
  File.open(lockname, File::RDWR|File::CREAT, 0644) do |file|
17
- file.flock(File::LOCK_EX)
18
- Timeout::timeout(options.fetch(:timeout, 60)) { yield }
19
+ Timeout::timeout(options.fetch(:wait, 60*60*24), Filelock::WaitTimeout) { file.flock(File::LOCK_EX) }
20
+ Timeout::timeout(options.fetch(:timeout, 60), Filelock::ExecTimeout) { yield }
19
21
  end
20
22
  end
21
23
  end
@@ -0,0 +1,7 @@
1
+ module Filelock
2
+ class ExecTimeout < Timeout::Error
3
+ def message
4
+ "Didn't finish executing Filelock block within the timeout specified."
5
+ end
6
+ end
7
+ end
@@ -1,3 +1,3 @@
1
1
  module Filelock
2
- VERSION = "1.0.3"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -0,0 +1,7 @@
1
+ module Filelock
2
+ class WaitTimeout < Timeout::Error
3
+ def message
4
+ "Unable to acquire a file lock within the wait timeout specified."
5
+ end
6
+ end
7
+ end
@@ -131,9 +131,53 @@ describe Filelock do
131
131
  end
132
132
  end
133
133
 
134
+ # Seems like a duplicate but the above test ensures support of the older versions
135
+ # before Filelock::ExecTimeout existed, while this test is testing the new exception
136
+ # class.
137
+ it 'raises Filelock::ExecTimeout exception after specified number of seconds' do
138
+ Dir.mktmpdir do |dir|
139
+ lockpath = File.join(dir, 'sample.lock')
140
+
141
+ answer = 42
142
+
143
+ expect {
144
+ Filelock lockpath, :timeout => 1 do
145
+ sleep 2
146
+ answer = 0
147
+ end
148
+ }.to raise_error(Filelock::ExecTimeout)
149
+
150
+ expect(answer).to eq(42)
151
+ end
152
+ end
153
+
154
+
134
155
  # Java doesn't support forking
135
156
  if RUBY_PLATFORM != 'java'
136
-
157
+
158
+ it 'times out after lock cannot be acquired within specified number of seconds' do
159
+ Dir.mktmpdir do |dir|
160
+ lockpath = File.join(dir, 'sample.lock')
161
+
162
+ pid1 = Process.fork do
163
+ Filelock lockpath do
164
+ sleep 3
165
+ end
166
+ end
167
+
168
+ # Give the forked process some time to spin up
169
+ sleep 1
170
+
171
+ expect {
172
+ Filelock lockpath, :wait => 1 do
173
+ answer = 0
174
+ end
175
+ }.to raise_error(Filelock::WaitTimeout)
176
+
177
+ Process.wait
178
+ end
179
+ end
180
+
137
181
  it 'should work for multiple processes' do
138
182
  write('/tmp/number.txt', '0')
139
183
 
metadata CHANGED
@@ -1,27 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: filelock
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Stankiewicz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-16 00:00:00.000000000 Z
11
+ date: 2016-02-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - '>'
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.3'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - '>'
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.3'
27
27
  - !ruby/object:Gem::Dependency
@@ -59,15 +59,19 @@ executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
+ - .gitignore
62
63
  - .rspec
63
64
  - .travis.yml
65
+ - CHANGELOG.md
64
66
  - Gemfile
65
67
  - LICENSE.txt
66
68
  - README.md
67
69
  - Rakefile
68
70
  - filelock.gemspec
69
71
  - lib/filelock.rb
72
+ - lib/filelock/exec_timeout.rb
70
73
  - lib/filelock/version.rb
74
+ - lib/filelock/wait_timeout.rb
71
75
  - spec/filelock_spec.rb
72
76
  homepage: http://github.com/sheerun/filelock
73
77
  licenses:
@@ -95,4 +99,3 @@ specification_version: 4
95
99
  summary: Heavily tested yet simple filelocking solution using flock
96
100
  test_files:
97
101
  - spec/filelock_spec.rb
98
- has_rdoc: