Class: Consul::Client::Session

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

Overview

Consul Session Client

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Base

#initialize, #is_reachable

Class Method Details

.for_name(name, lock_delay = '15s', node = nil, checks = ['serfHealth'], behaviour = 'release', ttl = nil) ⇒ Object

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

name - The name of the session. lock_delay - Allowance window for leaders to Valid values between ‘0s’ and ‘60s’ node - The name of the node, defaults to the node the agent is running on checks - Health Checks to associate to this session behaviour - ‘release’ or ‘destroy’ Behaviour when session is invalidated. ttl - When provided Must be between ‘10s’ and ‘3600s’

Returns: Consul::Model::Session instance.



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

def self.for_name(name,
    lock_delay = '15s',
    node = nil,
    checks = ['serfHealth'],
    behaviour = 'release',
    ttl = nil)
  raise ArgumentError.new "Illegal Name: #{name}" if name.nil?
  session = Consul::Model::Session.new(name: name)
  session[:lock_delay] = lock_delay unless lock_delay.nil?
  session[:node] = node unless node.nil?
  checks = [] if checks.nil?
  checks += 'serfHealth' unless checks.include? 'serfHealth'
  session[:checks] = checks
  behaviour = 'release' if behaviour.nil? or behaviour != 'release' or behaviour != 'destroy'
  session[:behaviour] = behaviour
  session[:ttl] = ttl unless ttl.nil?
  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 a

Raises:

  • (TypeError)


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

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}")
  nil
end

#destroy(session, dc = nil) ⇒ Object

Public: Destroys a given session



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

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



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

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



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

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



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

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



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

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.name}"), session.to_json
  success
end