class SidekiqUniqueJobs::Lock::UntilAndWhileExecuting

Locks jobs while the job is executing in the server process

See {#lock} for more information about the client. See {#execute} for more information about the server

@author Mikael Henriksson <mikael@mhenrixon.com>

Public Instance Methods

execute() { || ... } click to toggle source

Executes in the Sidekiq server process @yield to the worker class perform method

# File lib/sidekiq_unique_jobs/lock/until_and_while_executing.rb, line 40
def execute
  if locksmith.unlock
    # ensure_relocked do
    runtime_lock.execute { return yield }
    # end
  else
    reflect(:unlock_failed, item)
  end
rescue Exception # rubocop:disable Lint/RescueException
  reflect(:execution_failed, item)
  locksmith.lock(wait: 2)

  raise
end
lock(origin: :client) { || ... } click to toggle source

Locks a sidekiq job

@note Will call a conflict strategy if lock can't be achieved.

@return [String, nil] the locked jid when properly locked, else nil.

@yield to the caller when given a block

# File lib/sidekiq_unique_jobs/lock/until_and_while_executing.rb, line 25
def lock(origin: :client, &block)
  unless (token = locksmith.lock)
    reflect(:lock_failed, item)
    call_strategy(origin: origin, &block)

    return
  end

  yield if block

  token
end

Private Instance Methods

ensure_relocked() { || ... } click to toggle source
# File lib/sidekiq_unique_jobs/lock/until_and_while_executing.rb, line 57
def ensure_relocked
  yield
rescue Exception # rubocop:disable Lint/RescueException
  reflect(:execution_failed, item)
  locksmith.lock

  raise
end
runtime_lock() click to toggle source
# File lib/sidekiq_unique_jobs/lock/until_and_while_executing.rb, line 66
def runtime_lock
  @runtime_lock ||= SidekiqUniqueJobs::Lock::WhileExecuting.new(item.dup, callback, redis_pool)
end