Module: RedisQueuedLocks::Acquirer::AcquireLock::WithAcqTimeout Private

Included in:
RedisQueuedLocks::Acquirer::AcquireLock
Defined in:
lib/redis_queued_locks/acquirer/acquire_lock/with_acq_timeout.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Since:

  • 1.0.0

Instance Method Summary collapse

Instance Method Details

#with_acq_timeout(redis, timeout, lock_key, lock_name, raise_errors, detailed_acq_timeout_error, on_timeout: nil) { ... } ⇒ Any

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • redis (RedisClient) —

    Redis connection manager required for additional data extraction for error message.

  • timeout (NilClass, Integer) —

    Time period after which the logic will fail with timeout error.

  • lock_key (String) —

    Lock name in RQL notation (rql:lock:some-lock-name).

  • lock_name (String) —

    Original lock name passed by the businessl logic (without RQL notaiton parts).

  • raise_errors (Boolean) —

    Raise erros on exceptional cases.

  • detailed_acq_timeout_error (Boolean) —

    Add additional error data about lock queue and required lock to the timeout error or not.

  • on_timeout (Hash) (defaults to: nil) —

    a customizable set of options

Options Hash (on_timeout:):

  • Callback (Proc, NilClass) —

    invoked on Timeout::Error.

Yields:

  • Custom logic that should be invoked under the obtained lock.

Yield Returns:

  • (Any)

Returns:

  • (Any)

Raises:

Since:

  • 1.0.0

Version:

  • 1.11.0



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/redis_queued_locks/acquirer/acquire_lock/with_acq_timeout.rb', line 30

def with_acq_timeout(
  redis,
  timeout,
  lock_key,
  lock_name,
  raise_errors,
  detailed_acq_timeout_error,
  on_timeout: nil,
  &block
)
  ::Timeout.timeout(timeout, RedisQueuedLocks::LockAcquirementIntermediateTimeoutError, &block)
rescue RedisQueuedLocks::LockAcquirementIntermediateTimeoutError
  if on_timeout != nil
    # @type var on_timeout: Proc
    on_timeout.call
  end

  if raise_errors
    if detailed_acq_timeout_error
      # TODO: rewrite these invocations to separated inner-AcquireLock-related modules
      #   in order to remove any dependencies from the other public RQL commands cuz
      #   all AcquireLock logic elements should be fully independent from others as a core;
      lock_info = RedisQueuedLocks::Acquirer::LockInfo.lock_info(redis, lock_name)
      queue_info = RedisQueuedLocks::Acquirer::QueueInfo.queue_info(redis, lock_name)

      # rubocop:disable Metrics/BlockNesting
      raise(
        RedisQueuedLocks::LockAcquirementTimeoutError,
        "Failed to acquire the lock \"#{lock_key}\" " \
        "for the given <#{timeout} seconds> timeout. Details: " \
        "<Lock Data> => #{lock_info ? lock_info.inspect : '<no_data>'}; " \
        "<Queue Data> => #{queue_info ? queue_info.inspect : '<no_data>'};"
      )
      # rubocop:enable Metrics/BlockNesting
    else
      raise(
        RedisQueuedLocks::LockAcquirementTimeoutError,
        "Failed to acquire the lock \"#{lock_key}\" " \
        "for the given <#{timeout} seconds> timeout."
      )
    end
  end
end