flock_synchronize 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,14 +14,30 @@ module FlockSynchronize
14
14
  # some.code_that_needs(synchronizing)
15
15
  # end
16
16
  #
17
- # The optional locking_constant parameter allows you to specify a different
18
- # constant than LOCK_EX. See the File.flock documentation for more info.
17
+ # Options available:
19
18
  #
20
- def self.flock_synchronize(key, locking_constant=File::LOCK_EX)
19
+ # locking_constant: use a different constant than LOCK_EX. See the
20
+ # File.flock documentation for more info.
21
+ # timeout: automatically unlock if the lock was created more
22
+ # this number of seconds ago
23
+ #
24
+ def self.flock_synchronize(key, options={})
25
+ # Previous versions took locking_constant as second param and now it's
26
+ # a hash
27
+ if !options.kind_of? Hash
28
+ options = {locking_constant: options}
29
+ end
30
+ options[:locking_constant] ||= File::LOCK_EX
31
+
21
32
  filename = File.join(Dir.tmpdir, "#{key}.flock")
22
33
  begin
34
+ if options[:timeout]
35
+ if File.exists? filename and (Time.now - File.mtime(filename)) > options[:timeout]
36
+ File.unlink filename
37
+ end
38
+ end
23
39
  File.open(filename, 'w') do |f|
24
- f.flock(locking_constant)
40
+ f.flock(options[:locking_constant])
25
41
  yield
26
42
  end
27
43
  ensure
@@ -1,3 +1,3 @@
1
1
  module FlockSynchronize
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flock_synchronize
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2015-10-30 00:00:00.000000000 Z
12
+ date: 2015-11-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler