Class: MCollective::Util::Playbook::DataStores::ConsulDataStore

Inherits:
Base
  • Object
show all
Defined in:
lib/mcollective/util/playbook/data_stores/consul_data_store.rb

Instance Method Summary collapse

Methods inherited from Base

#from_hash, #initialize, #members, #prepare, #validate_configuration!

Constructor Details

This class inherits a constructor from MCollective::Util::Playbook::DataStores::Base

Instance Method Details

#delete(key) ⇒ Object



36
37
38
# File 'lib/mcollective/util/playbook/data_stores/consul_data_store.rb', line 36

def delete(key)
  Diplomat::Kv.delete(key)
end

#lock(key, timeout) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/mcollective/util/playbook/data_stores/consul_data_store.rb', line 16

def lock(key, timeout)
  Timeout.timeout(timeout) do
    Diplomat::Lock.wait_to_acquire(key, session, nil, 2)
  end
rescue Timeout::Error
  raise("Failed to obtain lock %s after %d seconds" % [key, timeout])
end

#read(key) ⇒ Object



28
29
30
# File 'lib/mcollective/util/playbook/data_stores/consul_data_store.rb', line 28

def read(key)
  Diplomat::Kv.get(key)
end

#release(key) ⇒ Object



24
25
26
# File 'lib/mcollective/util/playbook/data_stores/consul_data_store.rb', line 24

def release(key)
  Diplomat::Lock.release(key, session)
end

#renew_sessionObject



46
47
48
49
50
51
# File 'lib/mcollective/util/playbook/data_stores/consul_data_store.rb', line 46

def renew_session
  if @session_id
    Log.debug("Renewing Consul session %s" % [@session_id])
    Diplomat::Session.renew(@session_id)
  end
end

#sessionObject



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/mcollective/util/playbook/data_stores/consul_data_store.rb', line 71

def session
  @session_mutex.synchronize do
    return @session_id if @session_id

    ttl_spec = "%ss" % ttl

    @session_id ||= Diplomat::Session.create("TTL" => ttl_spec, "Behavior" => "delete")

    start_session_manager

    @session_id
  end
end

#start_session_managerObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mcollective/util/playbook/data_stores/consul_data_store.rb', line 53

def start_session_manager
  @session_manager = Thread.new do
    begin
      loop do
        renew_session

        sleep(ttl - 5)
      end
    rescue
      Log.warn("Session manager for Consul data store %s failed: %s: %s" % [@name, $!.class, $!.to_s])

      sleep(1)

      retry
    end
  end
end

#startup_hookObject



6
7
8
9
10
11
12
13
14
# File 'lib/mcollective/util/playbook/data_stores/consul_data_store.rb', line 6

def startup_hook
  require "diplomat"

  @session_mutex = Mutex.new
  @session_manager = nil
  @session_id = nil
rescue LoadError
  raise("Consul Data Store is not functional. Please install the diplomat Gem.")
end

#ttlObject



40
41
42
43
44
# File 'lib/mcollective/util/playbook/data_stores/consul_data_store.rb', line 40

def ttl
  supplied = Integer(@properties.fetch("ttl", 10)).abs

  supplied < 10 ? 10 : supplied
end

#write(key, value) ⇒ Object



32
33
34
# File 'lib/mcollective/util/playbook/data_stores/consul_data_store.rb', line 32

def write(key, value)
  Diplomat::Kv.put(key, value)
end