Class: SidekiqUniqueJobs::Lock::WhileExecuting

Inherits:
BaseLock
  • Object
show all
Includes:
SidekiqUniqueJobs::Logging::Middleware, OptionsWithFallback
Defined in:
lib/sidekiq_unique_jobs/lock/while_executing.rb

Overview

Locks jobs while the job is executing in the server process

  • Locks before yielding to the worker’s perform method

  • Unlocks after yielding to the worker’s perform method

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

Author:

Direct Known Subclasses

WhileExecutingReject

Constant Summary collapse

RUN_SUFFIX =
":RUN"

Instance Method Summary collapse

Methods included from SidekiqUniqueJobs::Logging::Middleware

included, #logging_context

Methods included from SidekiqUniqueJobs::Logging

#build_message, included, #log_debug, #log_error, #log_fatal, #log_info, #log_warn, #logger, #logging_context, #with_configured_loggers_context, #with_logging_context

Methods included from OptionsWithFallback

included, #lock_class, #lock_instance, #lock_type, #locks, #options, #unique_disabled?, #unique_enabled?

Methods inherited from BaseLock

#locksmith, validate_options

Methods included from Reflectable

#reflect

Constructor Details

#initialize(item, callback, redis_pool = nil) ⇒ WhileExecuting

Returns a new instance of WhileExecuting.

Parameters:

  • item (Hash)

    the Sidekiq job hash

  • callback (Proc)

    callback to call after unlock

  • redis_pool (Sidekiq::RedisConnection, ConnectionPool) (defaults to: nil)

    the redis connection



23
24
25
26
# 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

Instance Method Details

#execute { ... } ⇒ Object

Executes in the Sidekiq server process.

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

Yields:

  • to the worker class perform method



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sidekiq_unique_jobs/lock/while_executing.rb', line 41

def execute(&block)
  with_logging_context do
    executed = locksmith.execute do
      yield
      item[JID]
    ensure
      unlock_and_callback
    end

    unless executed
      reflect(:execution_failed, item)
      call_strategy(origin: :server, &block)
    end
  end
end

#locktrue

Simulate that a client lock was achieved.

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

Returns:

  • (true)

    always returns true



31
32
33
34
35
36
# File 'lib/sidekiq_unique_jobs/lock/while_executing.rb', line 31

def lock
  job_id = item[JID]
  yield if block_given?

  job_id
end