Class: SidekiqUniqueJobs::Lock::UntilAndWhileExecuting

Inherits:
BaseLock
  • Object
show all
Defined in:
lib/sidekiq_unique_jobs/lock/until_and_while_executing.rb

Overview

Locks jobs while the job is executing in the server process

  • Locks on perform_in or perform_async (see UntilExecuting)

  • Unlocks before yielding to the worker’s perform method (see UntilExecuting)

  • Locks before yielding to the worker’s perform method (see WhileExecuting)

  • Unlocks after yielding to the worker’s perform method (see WhileExecuting)

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

Author:

Instance Method Summary collapse

Methods inherited from BaseLock

#initialize, #locksmith, validate_options

Methods included from Reflectable

#reflect

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

Constructor Details

This class inherits a constructor from SidekiqUniqueJobs::Lock::BaseLock

Instance Method Details

#execute { ... } ⇒ Object

Executes in the Sidekiq server process

Yields:

  • to the worker class perform method



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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) { ... } ⇒ String?

Note:

Will call a conflict strategy if lock can’t be achieved.

Locks a sidekiq job

Yields:

  • to the caller when given a block

Returns:

  • (String, nil)

    the locked jid when properly locked, else nil.



25
26
27
28
29
30
31
32
33
34
35
36
# 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