Class: ZK::Locker::LockerBase
- Inherits:
-
Object
- Object
- ZK::Locker::LockerBase
show all
- Includes:
- ZK::Logging
- Defined in:
- lib/zk/locker.rb
Instance Method Summary
collapse
Constructor Details
#initialize(zookeeper_client, name, root_lock_node = "/_zklocking") ⇒ LockerBase
Returns a new instance of LockerBase.
49
50
51
52
53
54
55
56
|
# File 'lib/zk/locker.rb', line 49
def initialize(zookeeper_client, name, root_lock_node = "/_zklocking")
@zk = zookeeper_client
@root_lock_node = root_lock_node
@path = name
@locked = false
@waiting = false
@root_lock_path = "#{@root_lock_node}/#{@path.gsub("/", "__")}"
end
|
Instance Method Details
#cleanup_lock_path! ⇒ Object
130
131
132
133
134
|
# File 'lib/zk/locker.rb', line 130
def cleanup_lock_path!
logger.debug { "removing lock path #{@lock_path}" }
@zk.delete(@lock_path)
@zk.delete(root_lock_path) rescue Exceptions::NotEmpty
end
|
#create_lock_path!(prefix = 'lock') ⇒ Object
prefix is the string that will appear in front of the sequence num,
defaults to 'lock'
121
122
123
124
125
126
127
128
|
# File 'lib/zk/locker.rb', line 121
def create_lock_path!(prefix='lock')
@lock_path = @zk.create("#{root_lock_path}/#{prefix}", "", :mode => :ephemeral_sequential)
logger.debug { "got lock path #{@lock_path}" }
@lock_path
rescue Exceptions::NoNode
create_root_path!
retry
end
|
#create_root_path! ⇒ Object
115
116
117
|
# File 'lib/zk/locker.rb', line 115
def create_root_path!
@zk.mkdir_p(@root_lock_path)
end
|
#digit_from(path) ⇒ Object
101
102
103
|
# File 'lib/zk/locker.rb', line 101
def digit_from(path)
self.class.digit_from_lock_path(path)
end
|
#in_waiting_status ⇒ Object
94
95
96
97
98
99
|
# File 'lib/zk/locker.rb', line 94
def in_waiting_status
w, @waiting = @waiting, true
yield
ensure
@waiting = w
end
|
#lock_basename ⇒ Object
the basename of our lock path
for the lock_path '/zklocking/foobar/_blah/lock000000007'
lock_basename is 'lock000000007'
returns nil if lock_path is not set
72
73
74
|
# File 'lib/zk/locker.rb', line 72
def lock_basename
lock_path and File.basename(lock_path)
end
|
#lock_children(watch = false) ⇒ Object
105
106
107
|
# File 'lib/zk/locker.rb', line 105
def lock_children(watch=false)
@zk.children(root_lock_path, :watch => watch)
end
|
#locked? ⇒ Boolean
76
77
78
|
# File 'lib/zk/locker.rb', line 76
def locked?
false|@locked
end
|
#ordered_lock_children(watch = false) ⇒ Object
109
110
111
112
113
|
# File 'lib/zk/locker.rb', line 109
def ordered_lock_children(watch=false)
lock_children(watch).tap do |ary|
ary.sort! { |a,b| digit_from(a) <=> digit_from(b) }
end
end
|
#unlock! ⇒ Object
80
81
82
83
84
85
86
|
# File 'lib/zk/locker.rb', line 80
def unlock!
if @locked
cleanup_lock_path!
@locked = false
true
end
end
|
#waiting? ⇒ Boolean
returns true if this locker is waiting to acquire lock
89
90
91
|
# File 'lib/zk/locker.rb', line 89
def waiting? false|@waiting
end
|
#with_lock ⇒ Object
block caller until lock is aquired, then yield
59
60
61
62
63
64
|
# File 'lib/zk/locker.rb', line 59
def with_lock
lock!(true)
yield
ensure
unlock!
end
|