Class: DaemonRunner::Session
- Inherits:
-
Object
- Object
- DaemonRunner::Session
- Includes:
- Logger
- Defined in:
- lib/daemon_runner/session.rb
Overview
Manage distributed locks with Consul
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.
Methods included from Logger
Constructor Details
#initialize(name, **options) ⇒ Session
Returns a new instance of Session.
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/daemon_runner/session.rb', line 57 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.
11 12 13 |
# File 'lib/daemon_runner/session.rb', line 11 def session @session end |
Instance Attribute Details
#behavior ⇒ Object (readonly)
Behavior when a session is invalidated, can be set to either release or delete
51 52 53 |
# File 'lib/daemon_runner/session.rb', line 51 def behavior @behavior end |
#delay ⇒ Object (readonly)
Period, in seconds, that a session’s locks will be
48 49 50 |
# File 'lib/daemon_runner/session.rb', line 48 def delay @delay end |
#id ⇒ Object (readonly)
Consul session ID
39 40 41 |
# File 'lib/daemon_runner/session.rb', line 39 def id @id end |
#name ⇒ Object (readonly)
Session name
42 43 44 |
# File 'lib/daemon_runner/session.rb', line 42 def name @name end |
#ttl ⇒ Object (readonly)
Period, in seconds, after which session expires
45 46 47 |
# File 'lib/daemon_runner/session.rb', line 45 def ttl @ttl end |
Class Method Details
.lock(path) ⇒ Boolean
Acquire a lock with the current session, or initialize a new session
24 25 26 |
# File 'lib/daemon_runner/session.rb', line 24 def lock(path) Diplomat::Lock.acquire(path, session.id) end |
.release(path) ⇒ Object
Release a lock held by the current session
33 34 35 |
# File 'lib/daemon_runner/session.rb', line 33 def release(path) Diplomat::Lock.release(path, session.id) end |
Instance Method Details
#destroy! ⇒ Object
Stop the renew thread and destroy the session
105 106 107 108 |
# File 'lib/daemon_runner/session.rb', line 105 def destroy! @renew.kill if renew? Diplomat::Session.destroy(id) end |
#renew! ⇒ Object
Create a thread to periodically renew the lock session
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/daemon_runner/session.rb', line 77 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
71 72 73 |
# File 'lib/daemon_runner/session.rb', line 71 def renew? @renew.is_a?(Thread) && @renew.alive? end |