Class: SidekiqUniqueJobs::Lock::UntilExecuted
- Inherits:
-
Object
- Object
- SidekiqUniqueJobs::Lock::UntilExecuted
show all
- Extended by:
- Forwardable
- Defined in:
- lib/sidekiq_unique_jobs/lock/until_executed.rb
Instance Method Summary
collapse
Constructor Details
#initialize(item, redis_pool = nil) ⇒ UntilExecuted
Returns a new instance of UntilExecuted.
9
10
11
12
|
# File 'lib/sidekiq_unique_jobs/lock/until_executed.rb', line 9
def initialize(item, redis_pool = nil)
@item = item
@redis_pool = redis_pool
end
|
Instance Method Details
#after_yield_yield ⇒ Object
61
62
63
|
# File 'lib/sidekiq_unique_jobs/lock/until_executed.rb', line 61
def after_yield_yield
yield
end
|
#execute(callback, &blk) ⇒ Object
14
15
16
17
18
19
20
21
22
|
# File 'lib/sidekiq_unique_jobs/lock/until_executed.rb', line 14
def execute(callback, &blk)
operative = true
send(:after_yield_yield, &blk)
rescue Sidekiq::Shutdown
operative = false
raise
ensure
callback.call if operative && unlock(:server)
end
|
#lock(scope) ⇒ Object
rubocop:disable MethodLength
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/sidekiq_unique_jobs/lock/until_executed.rb', line 32
def lock(scope)
if scope.to_sym != :client
fail ArgumentError, "#{scope} middleware can't #{__method__} #{unique_key}"
end
result = Scripts.call(:aquire_lock, redis_pool,
keys: [unique_key],
argv: [item[JID_KEY], max_lock_time])
case result
when 1
logger.debug { "successfully locked #{unique_key} for #{max_lock_time} seconds" }
true
when 0
logger.debug { "failed to aquire lock for #{unique_key}" }
false
else
fail "#{__method__} returned an unexpected value (#{result})"
end
end
|
#max_lock_time ⇒ Object
57
58
59
|
# File 'lib/sidekiq_unique_jobs/lock/until_executed.rb', line 57
def max_lock_time
@max_lock_time ||= TimeoutCalculator.for_item(item).seconds
end
|
#unique_key ⇒ Object
rubocop:enable MethodLength
53
54
55
|
# File 'lib/sidekiq_unique_jobs/lock/until_executed.rb', line 53
def unique_key
@unique_key ||= UniqueArgs.digest(item)
end
|
#unlock(scope) ⇒ Object
24
25
26
27
28
29
|
# File 'lib/sidekiq_unique_jobs/lock/until_executed.rb', line 24
def unlock(scope)
unless [:server, :api, :test].include?(scope)
fail ArgumentError, "#{scope} middleware can't #{__method__} #{unique_key}"
end
SidekiqUniqueJobs::Unlockable.unlock(unique_key, item[JID_KEY], redis_pool)
end
|