rfilelock 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2de6157119cba66e07d0619ea8d155411e483cf23835b2601c6dd30caeb302cb
4
+ data.tar.gz: 7da33d82e5c3f5c4a051df2836ab7c344be52d8f4d3dbc732207bd41027d0089
5
+ SHA512:
6
+ metadata.gz: ec568d1c7e6f3c21e0b1897c1c56fc2deeb8b1d500561398cf84710e70a20a56f532f6e4544efd0e0991cf6650337ac99f8c9c64ee16895268871636b441a280
7
+ data.tar.gz: 1a437779dc0ea78872fe446a42b0e3423bc546ec61aeb79c6be6152956522feb74db59b12239cc1ffc40ad676f2993d6128594cb960a2ec13f6ebcf9b1a51bb5
@@ -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
@@ -0,0 +1,3 @@
1
+ module Filelock
2
+ VERSION = "1.3.0"
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
@@ -0,0 +1,87 @@
1
+ require 'timeout'
2
+ require 'filelock/version'
3
+ require 'filelock/exec_timeout'
4
+ require 'filelock/wait_timeout'
5
+ require 'tempfile'
6
+
7
+
8
+
9
+ def update_lock_status(file, lock)
10
+ tid = Thread.current.object_id.to_s
11
+ #puts "updating lock to #{lock} for #{tid}"
12
+
13
+ keyfile = File.open( "keyfile.tmp", "w")
14
+ keyfile.truncate(0)
15
+
16
+ if(lock == true)
17
+ keyfile.write tid
18
+ keyfile.flush
19
+ keyfile.close
20
+ end
21
+ end
22
+
23
+ def is_owner(lockname)
24
+ tid = Thread.current.object_id.to_s
25
+ #puts "checking if tid=#{tid} is owner"
26
+ begin
27
+ ftid = nil
28
+ keyfile = File.open( "keyfile.tmp", "r")
29
+ ftid = keyfile.read
30
+ keyfile.close
31
+ #puts "read ftid=#{ftid} returning #{ftid == tid}"
32
+ return ftid == tid
33
+ rescue
34
+ return false
35
+ end
36
+ end
37
+
38
+
39
+
40
+
41
+ if RUBY_PLATFORM == "java"
42
+ def Filelock(lockname, options = {}, &block)
43
+ if(is_owner(lockname))#reentrant
44
+ yield
45
+ else
46
+ lockname = lockname.path if lockname.is_a?(Tempfile)
47
+ File.open(lockname, File::RDWR|File::CREAT, 0644) do |file|
48
+ Thread.pass until Timeout::timeout(options.fetch(:wait, 60*60*24), Filelock::WaitTimeout) {file.flock(File::LOCK_EX)}
49
+ Timeout::timeout(options.fetch(:timeout, 60), Filelock::ExecTimeout) {
50
+ begin
51
+ update_lock_status(file, true)
52
+ yield file
53
+ rescue Timeout::Error
54
+ update_lock_status(file, false)
55
+ throw(Timeout::Error)
56
+ end
57
+ update_lock_status(file, false)
58
+ }
59
+ end
60
+ end
61
+ end
62
+ else
63
+ def Filelock(lockname, options = {}, &block)
64
+ if(is_owner(lockname))#reentrant
65
+ yield
66
+ else
67
+ lockname = lockname.path if lockname.is_a?(Tempfile)
68
+ File.open(lockname, File::RDWR|File::CREAT, 0644) do |file|
69
+ #puts "trying to lock"
70
+ Timeout::timeout(options.fetch(:wait, 60*60*24), Filelock::WaitTimeout) {file.flock(File::LOCK_EX)}
71
+ Timeout::timeout(options.fetch(:timeout, 60), Filelock::ExecTimeout) {
72
+ begin
73
+ update_lock_status(file, true)
74
+ #puts "locked"
75
+ yield file
76
+ #puts "done"
77
+ rescue Timeout::Error
78
+ update_lock_status(file, false)
79
+ throw(Timeout::Error)
80
+ end
81
+ update_lock_status(file, false)
82
+
83
+ }
84
+ end
85
+ end
86
+ end
87
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rfilelock
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.3.0
5
+ platform: ruby
6
+ authors:
7
+ - ''
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-11-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Reentrant Filelock using sheerun/filelock and flock
56
+ email:
57
+ - tsahil@weavingthings.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - lib/filelock/exec_timeout.rb
63
+ - lib/filelock/version.rb
64
+ - lib/filelock/wait_timeout.rb
65
+ - lib/rfilelock.rb
66
+ homepage: https://github.com/Produvia-Weaver/reentrant-filelock
67
+ licenses:
68
+ - MIT
69
+ metadata: {}
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 2.7.3
87
+ signing_key:
88
+ specification_version: 4
89
+ summary: Reentrant Filelock using sheerun/filelock and flock
90
+ test_files: []