Class: UniqueThread::Locksmith

Inherits:
Object
  • Object
show all
Defined in:
lib/unique_thread/locksmith.rb

Defined Under Namespace

Classes: RedisResult

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, stopwatch:) ⇒ Locksmith

Returns a new instance of Locksmith.



7
8
9
10
11
12
13
14
# File 'lib/unique_thread/locksmith.rb', line 7

def initialize(name:, stopwatch:)
  @name      = name
  @stopwatch = stopwatch

  @lua_scripts = Hash[Dir[File.join(__dir__, 'redis_lua', '*.lua')].map do |lua_file|
    [File.basename(lua_file, '.lua').to_sym, UniqueThread.redis.script(:load, File.read(lua_file))]
  end]
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/unique_thread/locksmith.rb', line 5

def name
  @name
end

#stopwatchObject (readonly)

Returns the value of attribute stopwatch.



5
6
7
# File 'lib/unique_thread/locksmith.rb', line 5

def stopwatch
  @stopwatch
end

Instance Method Details

#new_lockObject



16
17
18
# File 'lib/unique_thread/locksmith.rb', line 16

def new_lock
  lock_from_redis_command(:get_lock, name, stopwatch.now, stopwatch.next_renewal)
end

#renew_lock(lock) ⇒ Object



20
21
22
# File 'lib/unique_thread/locksmith.rb', line 20

def renew_lock(lock)
  lock_from_redis_command(:extend_lock, name, lock.locked_until, stopwatch.next_renewal)
end