Class: Capistrano::ConsulKvLock::Latch
- Inherits:
-
Object
- Object
- Capistrano::ConsulKvLock::Latch
- Defined in:
- lib/capistrano/consul_kv_lock/latch.rb
Instance Attribute Summary collapse
-
#lock_key ⇒ Object
readonly
Returns the value of attribute lock_key.
Class Method Summary collapse
Instance Method Summary collapse
- #create_session ⇒ Object
- #delete_session ⇒ Object
-
#initialize(consul_url, options = {}) ⇒ Latch
constructor
A new instance of Latch.
- #lock ⇒ Object
- #locked? ⇒ Boolean
- #logger ⇒ Object
- #session_request ⇒ Object
- #unlock ⇒ Object
Constructor Details
#initialize(consul_url, options = {}) ⇒ Latch
Returns a new instance of Latch.
34 35 36 37 38 |
# File 'lib/capistrano/consul_kv_lock/latch.rb', line 34 def initialize(consul_url, ={}) Diplomat.configuration.url = consul_url @lock_key = [:consul_lock_key] || 'deployment/locked' @session_id = nil end |
Instance Attribute Details
#lock_key ⇒ Object (readonly)
Returns the value of attribute lock_key.
39 40 41 |
# File 'lib/capistrano/consul_kv_lock/latch.rb', line 39 def lock_key @lock_key end |
Class Method Details
.instance ⇒ Object
26 27 28 |
# File 'lib/capistrano/consul_kv_lock/latch.rb', line 26 def self.instance @_instance end |
.logger ⇒ Object
30 31 32 |
# File 'lib/capistrano/consul_kv_lock/latch.rb', line 30 def self.logger @_logger ||= SSHKittyLogger.new end |
.set_instance(consul_url, options = {}) ⇒ Object
20 21 22 23 24 |
# File 'lib/capistrano/consul_kv_lock/latch.rb', line 20 def self.set_instance(consul_url, ={}) @_instance ||= new(consul_url, ) logger.debug "Registered latch instance: #{@_instance.inspect}" @_instance end |
Instance Method Details
#create_session ⇒ Object
63 64 65 66 |
# File 'lib/capistrano/consul_kv_lock/latch.rb', line 63 def create_session logger.debug "Session request: #{session_request.inspect}" @session_id = Diplomat::Session.create(session_request) end |
#delete_session ⇒ Object
68 69 70 71 72 73 |
# File 'lib/capistrano/consul_kv_lock/latch.rb', line 68 def delete_session with_session { Diplomat::Session.destroy(@session_id) } @session_id = nil end |
#lock ⇒ Object
75 76 77 |
# File 'lib/capistrano/consul_kv_lock/latch.rb', line 75 def lock with_session { Diplomat::Lock.acquire(lock_key, @session_id, "true") } end |
#locked? ⇒ Boolean
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/capistrano/consul_kv_lock/latch.rb', line 52 def locked? !!(Diplomat::Kv.get(lock_key) =~ /\A(t(rue)?|1|y(es)?)\z/) rescue => e # in case of 404 if e..include?('404') return false else raise e end end |
#logger ⇒ Object
41 42 43 |
# File 'lib/capistrano/consul_kv_lock/latch.rb', line 41 def logger self.class.logger end |
#session_request ⇒ Object
45 46 47 48 49 50 |
# File 'lib/capistrano/consul_kv_lock/latch.rb', line 45 def session_request { "LockDelay" => 15, "Name" => "lock-for-#{lock_key.tr("/", "-")}", } end |
#unlock ⇒ Object
79 80 81 |
# File 'lib/capistrano/consul_kv_lock/latch.rb', line 79 def unlock with_session { Diplomat::Lock.release(lock_key, @session_id) } end |