Class: DaemonRunner::Session

Inherits:
Object
  • Object
show all
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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logger

#logger, #logger_name

Constructor Details

#initialize(name, **options) ⇒ Session

Returns a new instance of Session.

Parameters:

  • name (String)

    Session name

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • ttl (Fixnum) — default: 15

    Session TTL in seconds

  • delay (Fixnum) — default: 15

    Session release dealy in seconds

  • behavior (String) — default: release

    Session release behavior



62
63
64
65
66
67
68
69
70
71
# File 'lib/daemon_runner/session.rb', line 62

def initialize(name, **options)
  logger.info('Initializing a Consul session')

  @name = name
  @ttl = options.fetch(:ttl, 15)
  @delay = options.fetch(:delay, 15)
  @behavior = options.fetch(:behavior, 'release')

  init
end

Class Attribute Details

.sessionObject (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

#behaviorObject (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

#delayObject (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

#idObject (readonly)

Consul session ID



44
45
46
# File 'lib/daemon_runner/session.rb', line 44

def id
  @id
end

#nameObject (readonly)

Session name



47
48
49
# File 'lib/daemon_runner/session.rb', line 47

def name
  @name
end

#ttlObject (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

Parameters:

  • path (String)

    A path in the Consul key-value space to lock

  • lock_session (Session)

    The Session instance to lock the lock to

Returns:

  • (Boolean)

    ‘true` if the lock was acquired



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

Parameters:

  • path (String)

    A path in the Consul key-value space to release

  • lock_session (Session)

    The Session instance that the lock was acquired with



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

Raises:



16
17
18
19
20
21
# File 'lib/daemon_runner/session.rb', line 16

def start(name, **options)
  @session = Session.new(name, options).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

Returns:

  • (Boolean)

    ‘true` if the thread is alive



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