Class: DaemonRunner::Session

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/daemon_runner/session.rb

Overview

Manage distributed locks with Consul

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



57
58
59
60
61
62
63
64
65
66
# File 'lib/daemon_runner/session.rb', line 57

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.



11
12
13
# File 'lib/daemon_runner/session.rb', line 11

def session
  @session
end

Instance Attribute Details

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

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

#idObject (readonly)

Consul session ID



39
40
41
# File 'lib/daemon_runner/session.rb', line 39

def id
  @id
end

#nameObject (readonly)

Session name



42
43
44
# File 'lib/daemon_runner/session.rb', line 42

def name
  @name
end

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

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



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

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



33
34
35
# File 'lib/daemon_runner/session.rb', line 33

def release(path)
  Diplomat::Lock.release(path, session.id)
end

.start(name, **options) ⇒ Object



13
14
15
# File 'lib/daemon_runner/session.rb', line 13

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

Returns:

  • (Boolean)

    true if the thread is alive



71
72
73
# File 'lib/daemon_runner/session.rb', line 71

def renew?
  @renew.is_a?(Thread) && @renew.alive?
end