Class: Stealth::Lock

Inherits:
Object
  • Object
show all
Includes:
Redis
Defined in:
lib/stealth/lock.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session_id:, session_slug: nil, position: nil) ⇒ Lock

Returns a new instance of Lock.



11
12
13
14
15
16
# File 'lib/stealth/lock.rb', line 11

def initialize(session_id:, session_slug: nil, position: nil)
  @session_id = session_id
  @session_slug = session_slug
  @position = position
  @tid = Stealth.tid
end

Instance Attribute Details

#positionObject

Returns the value of attribute position.



9
10
11
# File 'lib/stealth/lock.rb', line 9

def position
  @position
end

#session_idObject

Returns the value of attribute session_id.



9
10
11
# File 'lib/stealth/lock.rb', line 9

def session_id
  @session_id
end

#session_slugObject

Returns the value of attribute session_slug.



9
10
11
# File 'lib/stealth/lock.rb', line 9

def session_slug
  @session_slug
end

#tidObject

Returns the value of attribute tid.



9
10
11
# File 'lib/stealth/lock.rb', line 9

def tid
  @tid
end

Class Method Details

.find_lock(session_id:) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/stealth/lock.rb', line 18

def self.find_lock(session_id:)
  lock = Lock.new(session_id: session_id)
  lock_slug = lock.slug # fetch lock from Redis

  return if lock_slug.nil?

  # parse the lock slug
  tid_and_session_slug, position = lock_slug.split(':')
  tid, session_slug = tid_and_session_slug.split('#')

  # set the values from the slug to the lock object
  lock.session_slug = session_slug
  lock.position = position&.to_i
  lock.tid = tid
  lock
end

Instance Method Details

#createObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/stealth/lock.rb', line 35

def create
  if session_slug.blank?
    raise(
      ArgumentError,
      'A session_slug must be specified before a lock can be created.'
    )
  end

  # Expire locks after 30 seconds to prevent zombie locks from blocking
  # other threads to interact with a session.
  persist_key(
    key: lock_key,
    value: generate_lock,
    expiration: Stealth.config.lock_autorelease
  )
end

#flow_and_stateObject

Returns a hash:

{ flow: 'flow_name', state: 'state_name' }


64
65
66
# File 'lib/stealth/lock.rb', line 64

def flow_and_state
  Session.flow_and_state_from_session_slug(slug: session_slug)
end

#releaseObject



52
53
54
# File 'lib/stealth/lock.rb', line 52

def release
  delete_key(lock_key)
end

#slugObject



56
57
58
59
60
# File 'lib/stealth/lock.rb', line 56

def slug
  # We don't want to extend the expiration time that would result if
  # we specified one here.
  get_key(lock_key, expiration: 0)
end