Class: SidekiqUniqueJobs::Lock::UntilExecuted
- Inherits:
-
Object
- Object
- SidekiqUniqueJobs::Lock::UntilExecuted
- Extended by:
- Forwardable
- Includes:
- Unlockable
- Defined in:
- lib/sidekiq_unique_jobs/lock/until_executed.rb
Direct Known Subclasses
Instance Method Summary collapse
- #after_yield_yield ⇒ Object
- #execute(callback, &blk) ⇒ Object
-
#initialize(item, redis_pool = nil) ⇒ UntilExecuted
constructor
A new instance of UntilExecuted.
-
#lock(scope) ⇒ Object
rubocop:disable MethodLength.
- #max_lock_time ⇒ Object
-
#unique_key ⇒ Object
rubocop:enable MethodLength.
- #unlock(scope) ⇒ Object
Methods included from Unlockable
after_unlock, logger, unlock, unlock_by_arguments, unlock_by_jid, unlock_by_key
Constructor Details
#initialize(item, redis_pool = nil) ⇒ UntilExecuted
Returns a new instance of UntilExecuted.
11 12 13 14 |
# File 'lib/sidekiq_unique_jobs/lock/until_executed.rb', line 11 def initialize(item, redis_pool = nil) @item = item @redis_pool = redis_pool end |
Instance Method Details
#after_yield_yield ⇒ Object
64 65 66 |
# File 'lib/sidekiq_unique_jobs/lock/until_executed.rb', line 64 def after_yield_yield yield end |
#execute(callback, &blk) ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/sidekiq_unique_jobs/lock/until_executed.rb', line 16 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
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/sidekiq_unique_jobs/lock/until_executed.rb', line 35 def lock(scope) if scope.to_sym != :client raise 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 raise "#{__method__} returned an unexpected value (#{result})" end end |
#max_lock_time ⇒ Object
60 61 62 |
# File 'lib/sidekiq_unique_jobs/lock/until_executed.rb', line 60 def max_lock_time @max_lock_time ||= TimeoutCalculator.for_item(item).seconds end |
#unique_key ⇒ Object
rubocop:enable MethodLength
56 57 58 |
# File 'lib/sidekiq_unique_jobs/lock/until_executed.rb', line 56 def unique_key @unique_key ||= UniqueArgs.digest(item) end |
#unlock(scope) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/sidekiq_unique_jobs/lock/until_executed.rb', line 26 def unlock(scope) unless [:server, :api, :test].include?(scope) raise ArgumentError, "#{scope} middleware can't #{__method__} #{unique_key}" end unlock_by_key(unique_key, item[JID_KEY], redis_pool) end |