with_locking 0.0.1 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +5 -5
- data/lib/with_locking.rb +3 -3
- data/lib/with_locking/version.rb +1 -1
- data/spec/with_locking_spec.rb +7 -7
- metadata +1 -1
data/README.md
CHANGED
@@ -16,13 +16,13 @@ Or install it yourself as:
|
|
16
16
|
|
17
17
|
A block of code can be executed like so:
|
18
18
|
|
19
|
-
WithLocking.
|
19
|
+
WithLocking.run { puts "While I'm running other code run through WithLocking cannot run!" }
|
20
20
|
|
21
21
|
Alternatively, run the block of code with an optional name (recommended), that way multiple WithLocking blocks with different names can be invoked without conflicting:
|
22
22
|
|
23
|
-
WithLocking.
|
24
|
-
WithLocking.
|
25
|
-
WithLocking.
|
23
|
+
WithLocking.run(:name => "sleeper") { sleep 60 }
|
24
|
+
WithLocking.run(:name => "sleeper") { puts "I won't execute and will return false because 'sleeper' is still running the first block." }
|
25
|
+
WithLocking.run(:name => "other_name") { puts "But I will run because 'other_name' isn't running!" }
|
26
26
|
# => "But I will run because 'other_name' isn't running!"
|
27
27
|
|
28
28
|
To simply test if a named block is still running without executing a block use the `locked?` method:
|
@@ -32,7 +32,7 @@ To simply test if a named block is still running without executing a block use t
|
|
32
32
|
|
33
33
|
To raise an exception when the block isn't run (rather than simply returning false), use the `do!` method:
|
34
34
|
|
35
|
-
WithLocking.
|
35
|
+
WithLocking.run!(:name => "sleeper") { puts "Blah" }
|
36
36
|
# => raises an error if 'sleeper' block is still running from before
|
37
37
|
|
38
38
|
## Contributing
|
data/lib/with_locking.rb
CHANGED
@@ -2,7 +2,7 @@ require "with_locking/version"
|
|
2
2
|
|
3
3
|
module WithLocking
|
4
4
|
|
5
|
-
def self.
|
5
|
+
def self.run(options = {}, &block)
|
6
6
|
raise "No block given" unless block_given?
|
7
7
|
|
8
8
|
name = options[:name] || "locking_service_task"
|
@@ -27,8 +27,8 @@ module WithLocking
|
|
27
27
|
return true
|
28
28
|
end
|
29
29
|
|
30
|
-
def self.
|
31
|
-
raise "locked process still running" unless self.
|
30
|
+
def self.run!(options = {}, &block)
|
31
|
+
raise "locked process still running" unless self.run(options, &block)
|
32
32
|
end
|
33
33
|
|
34
34
|
def self.locked?(name)
|
data/lib/with_locking/version.rb
CHANGED
data/spec/with_locking_spec.rb
CHANGED
@@ -9,36 +9,36 @@ describe WithLocking do
|
|
9
9
|
File.stub(:delete)
|
10
10
|
end
|
11
11
|
|
12
|
-
describe "#
|
12
|
+
describe "#run" do
|
13
13
|
|
14
14
|
it "writes a pid file" do
|
15
15
|
File.should_receive(:open).with(path, "w")
|
16
|
-
WithLocking.
|
16
|
+
WithLocking.run(:name => file_name) { }
|
17
17
|
end
|
18
18
|
|
19
19
|
it "executes the given block" do
|
20
20
|
my_test_array = []
|
21
|
-
WithLocking.
|
21
|
+
WithLocking.run { my_test_array << 1 }
|
22
22
|
my_test_array.count.should == 1
|
23
23
|
end
|
24
24
|
|
25
25
|
it "deletes the pid file" do
|
26
26
|
File.should_receive(:delete).with(path)
|
27
|
-
WithLocking.
|
27
|
+
WithLocking.run(:name => file_name) {}
|
28
28
|
end
|
29
29
|
|
30
30
|
it "should not execute the block if the pid file already exists" do
|
31
31
|
File.stub(:exists?).and_return(true)
|
32
|
-
WithLocking.
|
32
|
+
WithLocking.run {}.should == false
|
33
33
|
end
|
34
34
|
|
35
35
|
end
|
36
36
|
|
37
|
-
describe "#
|
37
|
+
describe "#run!" do
|
38
38
|
|
39
39
|
it "raises an exception if the pid file already exists" do
|
40
40
|
File.stub(:exists?).and_return(true)
|
41
|
-
lambda { WithLocking.
|
41
|
+
lambda { WithLocking.run! {} }.should raise_error(RuntimeError, 'locked process still running')
|
42
42
|
end
|
43
43
|
|
44
44
|
end
|