process_lock 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -1
- data/README.md +1 -0
- data/lib/process_lock/version.rb +1 -1
- data/lib/process_lock.rb +6 -0
- data/spec/process_lock_spec.rb +39 -19
- metadata +74 -64
- data/Gemfile.lock +0 -27
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -33,6 +33,7 @@ Methods:
|
|
33
33
|
|
34
34
|
Note:
|
35
35
|
* locks don't stack - if we have already acquired the lock subsequent calls will reacquire the lock. releasing an already released lock will fail.
|
36
|
+
* If Rails.root is defined then lock files will be put in tmp/pids, and have .pid appended if no extension is specified
|
36
37
|
|
37
38
|
To acquire a lock, do some work and then release it:
|
38
39
|
|
data/lib/process_lock/version.rb
CHANGED
data/lib/process_lock.rb
CHANGED
@@ -13,6 +13,12 @@ class ProcessLock
|
|
13
13
|
|
14
14
|
def initialize(filename)
|
15
15
|
@filename = filename
|
16
|
+
if defined?(Rails) and Rails.root and (File.basename(filename) == @filename)
|
17
|
+
@filename = File.join(Rails.root, 'tmp', 'pids', filename)
|
18
|
+
unless filename =~ /\./
|
19
|
+
@filename << '.pid'
|
20
|
+
end
|
21
|
+
end
|
16
22
|
FileUtils.touch(@filename)
|
17
23
|
end
|
18
24
|
|
data/spec/process_lock_spec.rb
CHANGED
@@ -5,14 +5,14 @@ require 'fileutils'
|
|
5
5
|
describe 'ProcessLock' do
|
6
6
|
|
7
7
|
before(:all) do
|
8
|
-
FileUtils.mkdir_p('tmp')
|
9
|
-
FileUtils.rm_f Dir.glob('tmp/example*.tmp')
|
8
|
+
FileUtils.mkdir_p('tmp/pids')
|
9
|
+
FileUtils.rm_f Dir.glob('tmp/pids/example*.tmp')
|
10
10
|
end
|
11
11
|
|
12
12
|
describe '#acquire' do
|
13
13
|
|
14
14
|
it 'should acquire a lock when called without a block' do
|
15
|
-
p = ProcessLock.new('tmp/example1.txt')
|
15
|
+
p = ProcessLock.new('tmp/pids/example1.txt')
|
16
16
|
p.should_not be_owner
|
17
17
|
p.acquire.should be_true
|
18
18
|
p.should be_owner
|
@@ -23,7 +23,7 @@ describe 'ProcessLock' do
|
|
23
23
|
end
|
24
24
|
|
25
25
|
it 'should acquire a lock when called with a block and then release it' do
|
26
|
-
p = ProcessLock.new('tmp/example2.txt')
|
26
|
+
p = ProcessLock.new('tmp/pids/example2.txt')
|
27
27
|
p.should_not be_owner
|
28
28
|
did_something = false
|
29
29
|
p.acquire do
|
@@ -47,7 +47,7 @@ describe 'ProcessLock' do
|
|
47
47
|
end
|
48
48
|
|
49
49
|
it 'should acquire a lock when called with a block containing a return and then release it' do
|
50
|
-
p = ProcessLock.new('tmp/example3.txt')
|
50
|
+
p = ProcessLock.new('tmp/pids/example3.txt')
|
51
51
|
p.should_not be_owner
|
52
52
|
acquire_and_then_return_block_value(p) do
|
53
53
|
'value returned by block'
|
@@ -57,7 +57,7 @@ describe 'ProcessLock' do
|
|
57
57
|
end
|
58
58
|
|
59
59
|
it 'should not acquire a lock if some other process has the lock' do
|
60
|
-
fn = 'tmp/example4.txt'
|
60
|
+
fn = 'tmp/pids/example4.txt'
|
61
61
|
system('bash spec/other_process.sh "%s" 100' % fn)
|
62
62
|
p = ProcessLock.new(fn)
|
63
63
|
200.times do |i|
|
@@ -87,7 +87,7 @@ describe 'ProcessLock' do
|
|
87
87
|
end
|
88
88
|
|
89
89
|
it 'should acquire a lock if an completed process has the lock' do
|
90
|
-
fn = 'tmp/example5.txt'
|
90
|
+
fn = 'tmp/pids/example5.txt'
|
91
91
|
system('bash spec/other_process.sh "%s" 0' % fn)
|
92
92
|
p = ProcessLock.new(fn)
|
93
93
|
200.times do |i|
|
@@ -107,7 +107,7 @@ describe 'ProcessLock' do
|
|
107
107
|
|
108
108
|
it 'should allow multiple sequential locked sections' do
|
109
109
|
3.times do
|
110
|
-
p = ProcessLock.new('tmp/example6.txt')
|
110
|
+
p = ProcessLock.new('tmp/pids/example6.txt')
|
111
111
|
p.should_not be_owner
|
112
112
|
did_something = false
|
113
113
|
p.acquire do
|
@@ -122,7 +122,7 @@ describe 'ProcessLock' do
|
|
122
122
|
end
|
123
123
|
|
124
124
|
it 'should allow multiple parallel but differently named locked sections' do
|
125
|
-
ps = 3.times.collect { |i| ProcessLock.new('tmp/example7-%d.txt' % i) }
|
125
|
+
ps = 3.times.collect { |i| ProcessLock.new('tmp/pids/example7-%d.txt' % i) }
|
126
126
|
did_something = 0
|
127
127
|
ps.each do |p|
|
128
128
|
p.should_not be_owner
|
@@ -144,19 +144,19 @@ describe 'ProcessLock' do
|
|
144
144
|
describe '#acquire!' do
|
145
145
|
|
146
146
|
it 'should call acquire and expect true' do
|
147
|
-
p = ProcessLock.new('tmp/example8.txt')
|
147
|
+
p = ProcessLock.new('tmp/pids/example8.txt')
|
148
148
|
p.stub(:acquire_without_block).and_return(true)
|
149
149
|
p.acquire!.should be_true
|
150
150
|
end
|
151
151
|
|
152
152
|
it 'throw an error if acquire returns false' do
|
153
|
-
p = ProcessLock.new('tmp/example9.txt')
|
153
|
+
p = ProcessLock.new('tmp/pids/example9.txt')
|
154
154
|
p.stub(:acquire_without_block).and_return(false)
|
155
155
|
expect { p.acquire! }.to raise_error(ProcessLock::AlreadyLocked)
|
156
156
|
end
|
157
157
|
|
158
158
|
it 'should acquire a lock when called with a block and then release it' do
|
159
|
-
p = ProcessLock.new('tmp/example2.txt')
|
159
|
+
p = ProcessLock.new('tmp/pids/example2.txt')
|
160
160
|
p.should_not be_owner
|
161
161
|
did_something = false
|
162
162
|
p.acquire! do
|
@@ -175,13 +175,13 @@ describe 'ProcessLock' do
|
|
175
175
|
describe '#release!' do
|
176
176
|
|
177
177
|
it 'should call release and expect true' do
|
178
|
-
p = ProcessLock.new('tmp/example10.txt')
|
178
|
+
p = ProcessLock.new('tmp/pids/example10.txt')
|
179
179
|
p.stub(:release).and_return(true)
|
180
180
|
p.release!.should be_true
|
181
181
|
end
|
182
182
|
|
183
183
|
it 'throw an error if release returns false' do
|
184
|
-
p = ProcessLock.new('tmp/example11.txt')
|
184
|
+
p = ProcessLock.new('tmp/pids/example11.txt')
|
185
185
|
p.stub(:release).and_return(false)
|
186
186
|
expect { p.release! }.to raise_error(ProcessLock::NotLocked)
|
187
187
|
end
|
@@ -191,7 +191,7 @@ describe 'ProcessLock' do
|
|
191
191
|
describe '#read' do
|
192
192
|
|
193
193
|
it 'should return the current PID if the lock was acquired' do
|
194
|
-
p = ProcessLock.new('tmp/example12.txt')
|
194
|
+
p = ProcessLock.new('tmp/pids/example12.txt')
|
195
195
|
p.acquire do
|
196
196
|
p.read.should == Process.pid
|
197
197
|
p.should be_alive
|
@@ -200,7 +200,7 @@ describe 'ProcessLock' do
|
|
200
200
|
end
|
201
201
|
|
202
202
|
it 'should return whatever number is in the file' do
|
203
|
-
p = ProcessLock.new('tmp/example13.txt')
|
203
|
+
p = ProcessLock.new('tmp/pids/example13.txt')
|
204
204
|
File.open(p.filename, 'w') do |f|
|
205
205
|
f.puts('314152653')
|
206
206
|
end
|
@@ -211,15 +211,35 @@ describe 'ProcessLock' do
|
|
211
211
|
|
212
212
|
describe '#filename' do
|
213
213
|
|
214
|
-
|
215
|
-
|
214
|
+
class Rails
|
215
|
+
def self.root
|
216
|
+
'.'
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
it 'should return the path' do
|
221
|
+
fn = 'tmp/pids/example14'
|
216
222
|
p = ProcessLock.new(fn)
|
217
223
|
p.filename.should == fn
|
218
224
|
end
|
225
|
+
|
226
|
+
it 'should return tmp/pids/exampleN.pid for a simple name if Rails.root is set' do
|
227
|
+
fn = 'example14b'
|
228
|
+
Rails.root.should == '.'
|
229
|
+
p = ProcessLock.new(fn)
|
230
|
+
p.filename.should == "./tmp/pids/%s.pid" % fn
|
231
|
+
end
|
232
|
+
|
233
|
+
it 'should return tmp/pids/NAME.ext for NAME.ext if Rails.root is set' do
|
234
|
+
fn = 'example14c.ext'
|
235
|
+
p = ProcessLock.new(fn)
|
236
|
+
p.filename.should == "./tmp/pids/%s" % fn
|
237
|
+
end
|
238
|
+
|
219
239
|
end
|
220
240
|
|
221
241
|
it 'should use a string for the current PID in filename' do
|
222
|
-
p = ProcessLock.new('tmp/example15.txt')
|
242
|
+
p = ProcessLock.new('tmp/pids/example15.txt')
|
223
243
|
p.acquire do
|
224
244
|
File.open(p.filename, 'r') do |f|
|
225
245
|
contents = f.read
|
metadata
CHANGED
@@ -1,78 +1,81 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: process_lock
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
6
11
|
platform: ruby
|
7
|
-
authors:
|
12
|
+
authors:
|
8
13
|
- Simon Engledew
|
9
14
|
- Ian Heggie
|
10
15
|
autorequire:
|
11
16
|
bindir: bin
|
12
17
|
cert_chain: []
|
13
|
-
|
14
|
-
|
15
|
-
|
18
|
+
|
19
|
+
date: 2014-02-14 00:00:00 Z
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
16
22
|
name: bundler
|
17
|
-
requirement: !ruby/object:Gem::Requirement
|
18
|
-
none: false
|
19
|
-
requirements:
|
20
|
-
- - ~>
|
21
|
-
- !ruby/object:Gem::Version
|
22
|
-
version: '1.3'
|
23
|
-
type: :development
|
24
23
|
prerelease: false
|
25
|
-
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
26
25
|
none: false
|
27
|
-
requirements:
|
26
|
+
requirements:
|
28
27
|
- - ~>
|
29
|
-
- !ruby/object:Gem::Version
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
requirements:
|
36
|
-
- - ! '>='
|
37
|
-
- !ruby/object:Gem::Version
|
38
|
-
version: '0'
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 9
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 3
|
33
|
+
version: "1.3"
|
39
34
|
type: :development
|
35
|
+
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rake
|
40
38
|
prerelease: false
|
41
|
-
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
42
40
|
none: false
|
43
|
-
requirements:
|
44
|
-
- -
|
45
|
-
- !ruby/object:Gem::Version
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
none: false
|
51
|
-
requirements:
|
52
|
-
- - ~>
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '2.0'
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 3
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
version: "0"
|
55
48
|
type: :development
|
49
|
+
version_requirements: *id002
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: rspec
|
56
52
|
prerelease: false
|
57
|
-
|
53
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
58
54
|
none: false
|
59
|
-
requirements:
|
55
|
+
requirements:
|
60
56
|
- - ~>
|
61
|
-
- !ruby/object:Gem::Version
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
hash: 3
|
59
|
+
segments:
|
60
|
+
- 2
|
61
|
+
- 0
|
62
|
+
version: "2.0"
|
63
|
+
type: :development
|
64
|
+
version_requirements: *id003
|
65
|
+
description: A simple class to aquire and check process-id file based locks on a unix filesystem.
|
66
|
+
email:
|
66
67
|
- ian@heggie.biz
|
67
68
|
executables: []
|
69
|
+
|
68
70
|
extensions: []
|
71
|
+
|
69
72
|
extra_rdoc_files: []
|
70
|
-
|
73
|
+
|
74
|
+
files:
|
71
75
|
- .gitignore
|
72
76
|
- .rspec
|
73
77
|
- .travis.yml
|
74
78
|
- Gemfile
|
75
|
-
- Gemfile.lock
|
76
79
|
- README.md
|
77
80
|
- Rakefile
|
78
81
|
- lib/process_lock.rb
|
@@ -83,32 +86,39 @@ files:
|
|
83
86
|
- spec/run_example.sh
|
84
87
|
- spec/spec_helper.rb
|
85
88
|
homepage: https://github.com/ianheggie/ruby-process-lock
|
86
|
-
licenses:
|
89
|
+
licenses:
|
87
90
|
- MIT
|
88
91
|
post_install_message:
|
89
92
|
rdoc_options: []
|
90
|
-
|
93
|
+
|
94
|
+
require_paths:
|
91
95
|
- lib
|
92
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
93
97
|
none: false
|
94
|
-
requirements:
|
95
|
-
- -
|
96
|
-
- !ruby/object:Gem::Version
|
97
|
-
|
98
|
-
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
hash: 3
|
102
|
+
segments:
|
103
|
+
- 0
|
104
|
+
version: "0"
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
106
|
none: false
|
100
|
-
requirements:
|
101
|
-
- -
|
102
|
-
- !ruby/object:Gem::Version
|
103
|
-
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
hash: 3
|
111
|
+
segments:
|
112
|
+
- 0
|
113
|
+
version: "0"
|
104
114
|
requirements: []
|
115
|
+
|
105
116
|
rubyforge_project:
|
106
|
-
rubygems_version: 1.8.
|
117
|
+
rubygems_version: 1.8.6
|
107
118
|
signing_key:
|
108
119
|
specification_version: 3
|
109
|
-
summary: Use process lock to see if a process is already running or designate a master
|
110
|
-
|
111
|
-
test_files:
|
120
|
+
summary: Use process lock to see if a process is already running or designate a master process when running concurrent applications.
|
121
|
+
test_files:
|
112
122
|
- spec/other_process.sh
|
113
123
|
- spec/process_lock_spec.rb
|
114
124
|
- spec/run_example.sh
|
data/Gemfile.lock
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
process_lock (0.0.1)
|
5
|
-
|
6
|
-
GEM
|
7
|
-
remote: https://rubygems.org/
|
8
|
-
specs:
|
9
|
-
diff-lcs (1.2.5)
|
10
|
-
rake (10.1.1)
|
11
|
-
rspec (2.14.1)
|
12
|
-
rspec-core (~> 2.14.0)
|
13
|
-
rspec-expectations (~> 2.14.0)
|
14
|
-
rspec-mocks (~> 2.14.0)
|
15
|
-
rspec-core (2.14.7)
|
16
|
-
rspec-expectations (2.14.4)
|
17
|
-
diff-lcs (>= 1.1.3, < 2.0)
|
18
|
-
rspec-mocks (2.14.4)
|
19
|
-
|
20
|
-
PLATFORMS
|
21
|
-
ruby
|
22
|
-
|
23
|
-
DEPENDENCIES
|
24
|
-
bundler (~> 1.3)
|
25
|
-
process_lock!
|
26
|
-
rake
|
27
|
-
rspec (~> 2.0)
|