Class: Consul::Client::Session

Inherits:
Base
  • Object
show all
Defined in:
lib/consul/client/session.rb

Overview

Consul Session Client

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize, #is_reachable

Constructor Details

This class inherits a constructor from Consul::Client::Base

Class Method Details

.for_name(name, opts = {}) ⇒ Object

Public: Creates an instance of Consul::Model::Session with as many preset defaults as possible.

Params - name - (Required) The name of the session. opts - (Optional) Options hash opts - Allowance window for leaders to Valid values between ‘0s’ and ‘60s’ opts - The name of the node, defaults to the node the agent is running on opts - Health Checks to associate to this session opts - ‘release’ or ‘destroy’ Behaviour when session is invalidated. opts - When provided Must be between ‘10s’ and ‘3600s’

Returns: Consul::Model::Session instance.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/consul/client/session.rb', line 23

def self.for_name(name, opts = {})
    # lock_delay = '15s',
    # node = nil,
    # checks = ['serfHealth'],
    # behaviour = 'release',
    # ttl = nil)
  raise ArgumentError.new "Illegal Name: #{name}" if name.nil?
  opts = {} if opts.nil?
  session = Consul::Model::Session.new(name: name)
  session.lock_delay = opts[:lock_delay] unless opts.has_key?(:lock_delay)
  session.node = opts[:node]  unless opts.has_key?(:node)
  session.checks = opts[:checks] if opts.has_key?(:checks)
  session.behavior = opts[:behavior] if opts.has_key?(:behavior)
  session.ttl = opts[:ttl] unless opts.has_key?(:ttl)
  session
end

Instance Method Details

#create(session, dc = nil) ⇒ Object

Public: Creates a new Consul Session.

session - Session to create. dc - Consul data center

Returns The Session ID

Raises:

  • (TypeError)


46
47
48
49
50
51
52
53
54
# File 'lib/consul/client/session.rb', line 46

def create(session, dc = nil)
  raise TypeError, 'Session must be of type Consul::Model::Session' unless session.kind_of? Consul::Model::Session
  params = {}
  params[:dc] = dc unless dc.nil?
  success, body = _put(build_url('create'), session.extend(Consul::Model::Session::Representer).to_json, params)
  return Consul::Model::Session.new.extend(Consul::Model::Service::Representer).from_json(body) if success
  logger.warn("Unable to create session with #{session} reason: #{body}")
  nil
end

#destroy(session, dc = nil) ⇒ Object

Public: Destroys a given session



57
58
59
60
61
62
63
64
# File 'lib/consul/client/session.rb', line 57

def destroy(session, dc = nil)
  return false if session.nil?
  session = extract_session_id(session)
  params = nil
  params = {:dc => dc} unless dc.nil?
  success, _ = _put build_url("destroy/#{session}"), '', params
  success
end

#info(session, dc = nil) ⇒ Object

Public: Return the session info for a given session name. ccs



68
69
70
71
72
73
74
75
# File 'lib/consul/client/session.rb', line 68

def info(session, dc = nil)
  return nil if session.nil?
  session = extract_session_id(session)
  params = {}
  params[:dc] = dc unless dc.nil?
  resp = _get build_url("info/#{session}"), params
  JSON.parse(resp).map{|session_hash| session(session_hash)} unless resp.nil?
end

#list(dc = nil) ⇒ Object

Lists all active sessions



88
89
90
91
92
93
# File 'lib/consul/client/session.rb', line 88

def list(dc = nil)
  params = {}
  params[:dc] = dc unless dc.nil?
  resp = _get build_url('list'), params
  JSON.parse(resp).map{|session_hash| session(session_hash)} unless resp.nil?
end

#node(session, dc = nil) ⇒ Object

Lists sessions belonging to a node



78
79
80
81
82
83
84
85
# File 'lib/consul/client/session.rb', line 78

def node(session, dc = nil)
  return nil if session.nil?
  session = extract_session_id(session)
  params = {}
  params[:dc] = dc unless dc.nil?
  resp = _get build_url("node/#{session}"), params
  JSON.parse(resp).map{|session_hash| session(session_hash)} unless resp.nil?
end

#renew(session, dc = nil) ⇒ Object

Renews a TTL-based session



96
97
98
99
100
101
102
103
# File 'lib/consul/client/session.rb', line 96

def renew(session, dc = nil)
  return nil if session.nil?
  session = extract_session_id(session)
  params = {}
  params[:dc] = dc unless dc.nil?
  success, _ = _put build_url("renew/#{session}"), session.to_json
  success
end