Class: UniqueThread::Locksmith
- Inherits:
-
Object
- Object
- UniqueThread::Locksmith
- Defined in:
- lib/unique_thread/locksmith.rb
Defined Under Namespace
Classes: RedisResult
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#stopwatch ⇒ Object
readonly
Returns the value of attribute stopwatch.
Instance Method Summary collapse
-
#initialize(name:, stopwatch:) ⇒ Locksmith
constructor
A new instance of Locksmith.
- #new_lock ⇒ Object
- #renew_lock(lock) ⇒ Object
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
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/unique_thread/locksmith.rb', line 5 def name @name end |
#stopwatch ⇒ Object (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_lock ⇒ Object
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 |