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:, redis:, logger:) ⇒ Locksmith

Returns a new instance of Locksmith.



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

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

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

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#redisObject (readonly)

Returns the value of attribute redis.



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

def redis
  @redis
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



18
19
20
# File 'lib/unique_thread/locksmith.rb', line 18

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

#renew_lock(lock) ⇒ Object



22
23
24
# File 'lib/unique_thread/locksmith.rb', line 22

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