dustin-elock-client 0.1 → 0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/elock-client.rb +11 -2
- metadata +1 -1
data/lib/elock-client.rb
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
require 'socket'
|
2
2
|
require 'fcntl'
|
3
3
|
|
4
|
+
# Exception thrown from with_lock when the lock was not acquired.
|
5
|
+
class Locked < RuntimeError
|
6
|
+
end
|
7
|
+
|
4
8
|
# A client for elock.
|
5
9
|
class ELock
|
6
10
|
|
@@ -26,8 +30,13 @@ class ELock
|
|
26
30
|
end
|
27
31
|
|
28
32
|
# Run a block while holding the named lock.
|
33
|
+
# raises Locked if the lock could not be acquired.
|
29
34
|
def with_lock(name, timeout=nil)
|
30
|
-
|
35
|
+
if lock(name, timeout).first == 200
|
36
|
+
yield
|
37
|
+
else
|
38
|
+
raise Locked
|
39
|
+
end
|
31
40
|
ensure
|
32
41
|
unlock name
|
33
42
|
end
|
@@ -50,4 +59,4 @@ class ELock
|
|
50
59
|
[res.to_i, res[4..-3]]
|
51
60
|
end
|
52
61
|
|
53
|
-
end
|
62
|
+
end
|