Class: SidekiqUniqueJobs::Lock::UntilExecuted

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Unlockable
Defined in:
lib/sidekiq_unique_jobs/lock/until_executed.rb

Direct Known Subclasses

UntilExecuting, UntilTimeout

Instance Method Summary collapse

Methods included from Unlockable

ensure_job_id_removed, logger, unlock, unlock_by_key

Constructor Details

#initialize(item, redis_pool = nil) ⇒ UntilExecuted

Returns a new instance of UntilExecuted.


13
14
15
16
# File 'lib/sidekiq_unique_jobs/lock/until_executed.rb', line 13

def initialize(item, redis_pool = nil)
  @item = item
  @redis_pool = redis_pool
end

Instance Method Details

#after_yield_yieldObject


59
60
61
# File 'lib/sidekiq_unique_jobs/lock/until_executed.rb', line 59

def after_yield_yield
  yield
end

#execute(callback, &blk) ⇒ Object


18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/sidekiq_unique_jobs/lock/until_executed.rb', line 18

def execute(callback, &blk)
  operative = true
  send(:after_yield_yield, &blk)
rescue Sidekiq::Shutdown
  operative = false
  raise
ensure
  if operative && unlock(:server)
    callback.call
  else
    logger.fatal { "the unique_key: #{unique_key} needs to be unlocked manually" }
  end
end

#lock(scope) ⇒ Object

Raises:

  • (ArgumentError)

40
41
42
43
44
45
46
47
48
49
# File 'lib/sidekiq_unique_jobs/lock/until_executed.rb', line 40

def lock(scope)
  raise ArgumentError, "#{scope} middleware can't #{__method__} #{unique_key}" if scope.to_sym != :client

  Scripts::AcquireLock.execute(
    redis_pool,
    unique_key,
    item[JID_KEY],
    max_lock_time,
  )
end

#max_lock_timeObject


55
56
57
# File 'lib/sidekiq_unique_jobs/lock/until_executed.rb', line 55

def max_lock_time
  @max_lock_time ||= QueueLockTimeoutCalculator.for_item(item).seconds
end

#unique_keyObject


51
52
53
# File 'lib/sidekiq_unique_jobs/lock/until_executed.rb', line 51

def unique_key
  @unique_key ||= UniqueArgs.digest(item)
end

#unlock(scope) ⇒ Object


32
33
34
35
36
37
38
# File 'lib/sidekiq_unique_jobs/lock/until_executed.rb', line 32

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