Class: SidekiqUniqueJobs::Lock::WhileExecuting

Inherits:
BaseLock
  • Object
show all
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

Instance Method Summary collapse

Methods inherited from BaseLock

#delete, #delete!, #locked?, #unlock

Methods included from SidekiqUniqueJobs::Logging

#log_debug, #log_error, #log_fatal, #log_info, #log_warn, #logger, #logging_context

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



20
21
22
23
# File 'lib/sidekiq_unique_jobs/lock/while_executing.rb', line 20

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



35
36
37
38
39
40
41
42
43
# File 'lib/sidekiq_unique_jobs/lock/while_executing.rb', line 35

def execute
  return strategy&.call unless locksmith.lock(item[LOCK_TIMEOUT_KEY])

  yield
  unlock_with_callback
rescue Exception # rubocop:disable Lint/RescueException
  delete!
  raise
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



28
29
30
# File 'lib/sidekiq_unique_jobs/lock/while_executing.rb', line 28

def lock
  true
end