Module: Retriable

Defined in:
lib/retriable.rb,
lib/retriable/config.rb,
lib/retriable/version.rb,
lib/retriable/exponential_backoff.rb

Defined Under Namespace

Classes: Config, ExponentialBackoff

Constant Summary collapse

VERSION =
"3.5.0"

Class Method Summary collapse

Class Method Details

.configObject



15
16
17
# File 'lib/retriable.rb', line 15

def config
  @config ||= Config.new
end

.configure {|config| ... } ⇒ Object

Yields:



11
12
13
# File 'lib/retriable.rb', line 11

def configure
  yield(config)
end

.override(opts = {}) ⇒ Object

Raises:

  • (ArgumentError)


19
20
21
22
23
24
# File 'lib/retriable.rb', line 19

def override(opts = {})
  raise ArgumentError, "empty override options are not allowed; use reset_override instead" if opts.empty?

  validate_override_options(opts)
  @override_config = opts
end

.reset_overrideObject



26
27
28
# File 'lib/retriable.rb', line 26

def reset_override
  @override_config = nil
end

.retriable(opts = {}, &block) ⇒ Object



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/retriable.rb', line 43

def retriable(opts = {}, &block)
  local_config = if opts.empty? && !@override_config
                   config
                 else
                   Config.new(apply_override_options(config.to_h.merge(opts), @override_config))
                 end

  tries = local_config.tries
  intervals = build_intervals(local_config, tries)
  timeout = local_config.timeout
  on = local_config.on
  retry_if = local_config.retry_if
  on_retry = local_config.on_retry
  sleep_disabled = local_config.sleep_disabled
  max_elapsed_time = local_config.max_elapsed_time

  exception_list = on.is_a?(Hash) ? on.keys : on
  exception_list = [*exception_list]
  start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  elapsed_time = -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time }

  tries = intervals.size + 1

  execute_tries(
    tries: tries, intervals: intervals, timeout: timeout,
    exception_list: exception_list, on: on, retry_if: retry_if, on_retry: on_retry,
    elapsed_time: elapsed_time, max_elapsed_time: max_elapsed_time,
    sleep_disabled: sleep_disabled, &block
  )
end

.with_context(context_key, options = {}, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/retriable.rb', line 30

def with_context(context_key, options = {}, &block)
  contexts = available_contexts

  if !contexts.key?(context_key)
    raise ArgumentError,
          "#{context_key} not found in Retriable contexts (including overrides). Available contexts: #{contexts.keys}"
  end

  return unless block_given?

  retriable(context_options_for(context_key, options), &block)
end