Class: SidekiqUniqueJobs::Scripts::AcquireLock

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/sidekiq_unique_jobs/scripts/acquire_lock.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_redis_pool, unique_key, jid, max_lock_time) ⇒ AcquireLock

Returns a new instance of AcquireLock.

Raises:


15
16
17
18
19
20
21
22
23
# File 'lib/sidekiq_unique_jobs/scripts/acquire_lock.rb', line 15

def initialize(_redis_pool, unique_key, jid, max_lock_time)
  raise UniqueKeyMissing, 'unique_key is required' if unique_key.nil?
  raise JidMissing, 'jid is required' if jid.nil?
  raise MaxLockTimeMissing, 'max_lock_time is required' if max_lock_time.nil?

  @unique_key    = unique_key
  @jid           = jid
  @max_lock_time = max_lock_time
end

Instance Attribute Details

#jidObject (readonly)

Returns the value of attribute jid


13
14
15
# File 'lib/sidekiq_unique_jobs/scripts/acquire_lock.rb', line 13

def jid
  @jid
end

#max_lock_timeObject (readonly)

Returns the value of attribute max_lock_time


13
14
15
# File 'lib/sidekiq_unique_jobs/scripts/acquire_lock.rb', line 13

def max_lock_time
  @max_lock_time
end

#redis_poolObject (readonly)

Returns the value of attribute redis_pool


13
14
15
# File 'lib/sidekiq_unique_jobs/scripts/acquire_lock.rb', line 13

def redis_pool
  @redis_pool
end

#unique_keyObject (readonly)

Returns the value of attribute unique_key


13
14
15
# File 'lib/sidekiq_unique_jobs/scripts/acquire_lock.rb', line 13

def unique_key
  @unique_key
end

Class Method Details

.execute(redis_pool, unique_key, jid, max_lock_time) ⇒ Object


9
10
11
# File 'lib/sidekiq_unique_jobs/scripts/acquire_lock.rb', line 9

def self.execute(redis_pool, unique_key, jid, max_lock_time)
  new(redis_pool, unique_key, jid, max_lock_time).execute
end

Instance Method Details

#executeObject


25
26
27
28
29
30
31
# File 'lib/sidekiq_unique_jobs/scripts/acquire_lock.rb', line 25

def execute
  result = Scripts.call(:acquire_lock, redis_pool,
                        keys: [unique_key],
                        argv: [jid, max_lock_time])

  handle_result(result)
end

#handle_result(result) ⇒ Object


33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sidekiq_unique_jobs/scripts/acquire_lock.rb', line 33

def handle_result(result)
  case result
  when 1
    logger.debug { "successfully acquired lock #{unique_key} for #{max_lock_time} seconds" }
    true
  when 0
    logger.debug { "failed to acquire lock for #{unique_key}" }
    false
  else
    raise UnexpectedValue, "failed to acquire lock : unexpected return value (#{result})"
  end
end