Class: DaemonRunner::Session
- Inherits:
-
Object
- Object
- DaemonRunner::Session
- Includes:
- Logger
- Defined in:
- lib/daemon_runner/session.rb
Overview
Manage distributed locks with Consul
Defined Under Namespace
Classes: CreateSessionError, SessionError
Class Attribute Summary collapse
-
.session ⇒ Object
readonly
Returns the value of attribute session.
Instance Attribute Summary collapse
-
#behavior ⇒ Object
readonly
Behavior when a session is invalidated, can be set to either release or delete.
-
#delay ⇒ Object
readonly
Period, in seconds, that a session’s locks will be.
-
#id ⇒ Object
readonly
Consul session ID.
-
#name ⇒ Object
readonly
Session name.
-
#ttl ⇒ Object
readonly
Period, in seconds, after which session expires.
Class Method Summary collapse
-
.lock(path) ⇒ Boolean
Acquire a lock with the current session, or initialize a new session.
-
.release(path) ⇒ Object
Release a lock held by the current session.
- .start(name, **options) ⇒ Object
Instance Method Summary collapse
-
#destroy! ⇒ Object
Stop the renew thread and destroy the session.
-
#initialize(name, **options) ⇒ Session
constructor
A new instance of Session.
-
#renew! ⇒ Object
Create a thread to periodically renew the lock session.
-
#renew? ⇒ Boolean
Check if there is an active renew thread.
-
#verify_session(wait_time = 2) ⇒ Object
Verify wheather the session exists after a period of time.
Methods included from Logger
Constructor Details
#initialize(name, **options) ⇒ Session
Returns a new instance of Session.
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/daemon_runner/session.rb', line 62 def initialize(name, **) logger.info('Initializing a Consul session') @name = name @ttl = .fetch(:ttl, 15) @delay = .fetch(:delay, 15) @behavior = .fetch(:behavior, 'release') init end |
Class Attribute Details
.session ⇒ Object (readonly)
Returns the value of attribute session.
14 15 16 |
# File 'lib/daemon_runner/session.rb', line 14 def session @session end |
Instance Attribute Details
#behavior ⇒ Object (readonly)
Behavior when a session is invalidated, can be set to either release or delete
56 57 58 |
# File 'lib/daemon_runner/session.rb', line 56 def behavior @behavior end |
#delay ⇒ Object (readonly)
Period, in seconds, that a session’s locks will be
53 54 55 |
# File 'lib/daemon_runner/session.rb', line 53 def delay @delay end |
#id ⇒ Object (readonly)
Consul session ID
44 45 46 |
# File 'lib/daemon_runner/session.rb', line 44 def id @id end |
#name ⇒ Object (readonly)
Session name
47 48 49 |
# File 'lib/daemon_runner/session.rb', line 47 def name @name end |
#ttl ⇒ Object (readonly)
Period, in seconds, after which session expires
50 51 52 |
# File 'lib/daemon_runner/session.rb', line 50 def ttl @ttl end |
Class Method Details
.lock(path) ⇒ Boolean
Acquire a lock with the current session, or initialize a new session
29 30 31 |
# File 'lib/daemon_runner/session.rb', line 29 def lock(path) Diplomat::Lock.wait_to_acquire(path, session.id) end |
.release(path) ⇒ Object
Release a lock held by the current session
38 39 40 |
# File 'lib/daemon_runner/session.rb', line 38 def release(path) Diplomat::Lock.release(path, session.id) end |
.start(name, **options) ⇒ Object
16 17 18 19 20 21 |
# File 'lib/daemon_runner/session.rb', line 16 def start(name, **) @session = Session.new(name, ).renew! raise CreateSessionError, 'Failed to create session' if @session == false @session.verify_session @session end |
Instance Method Details
#destroy! ⇒ Object
Stop the renew thread and destroy the session
110 111 112 113 |
# File 'lib/daemon_runner/session.rb', line 110 def destroy! @renew.kill if renew? Diplomat::Session.destroy(id) end |
#renew! ⇒ Object
Create a thread to periodically renew the lock session
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/daemon_runner/session.rb', line 82 def renew! return if renew? @renew = Thread.new do ## Wakeup every TTL/2 seconds and renew the session loop do sleep ttl / 2 begin logger.debug(" - Renewing Consul session #{id}") Diplomat::Session.renew(id) rescue Faraday::ResourceNotFound logger.warn("Consul session #{id} has expired!") init rescue StandardError => e ## Keep the thread from exiting logger.error(e) end end end self end |
#renew? ⇒ Boolean
Check if there is an active renew thread
76 77 78 |
# File 'lib/daemon_runner/session.rb', line 76 def renew? @renew.is_a?(Thread) && @renew.alive? end |
#verify_session(wait_time = 2) ⇒ Object
Verify wheather the session exists after a period of time
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/daemon_runner/session.rb', line 116 def verify_session(wait_time = 2) logger.info(" - Wait until Consul session #{id} exists") wait_time.times do exists = session_exist? raise CreateSessionError, 'Error creating session' unless exists sleep 1 end logger.info(" - Found Consul session #{id}") rescue CreateSessionError init end |