class SidekiqUniqueJobs::Lock::WhileExecuting

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>

Constants

RUN_SUFFIX

Public Class Methods

new(item, callback, redis_pool = nil) click to toggle source

@param [Hash] item the Sidekiq job hash @param [Proc] callback callback to call after unlock @param [Sidekiq::RedisConnection, ConnectionPool] redis_pool the redis connection

Calls superclass method SidekiqUniqueJobs::Lock::BaseLock::new
# File lib/sidekiq_unique_jobs/lock/while_executing.rb, line 23
def initialize(item, callback, redis_pool = nil)
  super(item, callback, redis_pool)
  append_unique_key_suffix
end

Public Instance Methods

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

Executes in the Sidekiq server process.

These jobs are locked in the server process not from the client

@yield to the worker class perform method

# File lib/sidekiq_unique_jobs/lock/while_executing.rb, line 41
def execute(&block)
  with_logging_context do
    executed = locksmith.execute do
      yield
      callback_safely if locksmith.unlock
    ensure
      locksmith.unlock
    end

    call_strategy(origin: :server, &block) unless executed
  end
end
lock() { || ... } click to toggle source

Simulate that a client lock was achieved.

These locks should only ever be created in the server process.

@return [true] always returns true

# File lib/sidekiq_unique_jobs/lock/while_executing.rb, line 31
def lock
  job_id = item[JID]
  yield if block_given?

  job_id
end

Private Instance Methods

append_unique_key_suffix() click to toggle source

This is safe as the base_lock always creates a new digest

The append there for needs to be done every time
# File lib/sidekiq_unique_jobs/lock/while_executing.rb, line 58
def append_unique_key_suffix
  return if (lock_digest = item[LOCK_DIGEST]).end_with?(RUN_SUFFIX)

  item[LOCK_DIGEST] = lock_digest + RUN_SUFFIX
end