-
Notifications
You must be signed in to change notification settings - Fork 46
Add override config layer for forced retry behavior #121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,27 @@ | |
| module Retriable | ||
| module_function | ||
|
|
||
| def deep_merge(base, overrides) | ||
| base.merge(overrides) do |_key, base_value, override_value| | ||
| if base_value.is_a?(Hash) && override_value.is_a?(Hash) | ||
| deep_merge(base_value, override_value) | ||
| else | ||
| override_value | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def deep_dup(obj) | ||
| case obj | ||
| when Hash | ||
| obj.each_with_object({}) { |(k, v), h| h[k] = deep_dup(v) } | ||
| when Array | ||
| obj.map { |v| deep_dup(v) } | ||
| else | ||
| obj | ||
| end | ||
| end | ||
|
|
||
| def configure | ||
| yield(config) | ||
| end | ||
|
|
@@ -16,19 +37,40 @@ def config | |
| @config ||= Config.new | ||
| end | ||
|
|
||
| def override(opts = {}) | ||
| opts.each_key do |k| | ||
| raise ArgumentError, "#{k} is not a valid option" unless Config::ATTRIBUTES.include?(k) | ||
| end | ||
| @override_config = opts.empty? ? nil : deep_dup(opts).freeze | ||
| end | ||
|
kamui marked this conversation as resolved.
|
||
|
|
||
| def reset_override | ||
| @override_config = nil | ||
| end | ||
|
|
||
| def with_context(context_key, options = {}, &block) | ||
| if !config.contexts.key?(context_key) | ||
| contexts = merged_contexts | ||
|
|
||
| if !contexts.key?(context_key) | ||
| raise ArgumentError, | ||
| "#{context_key} not found in Retriable.config.contexts. Available contexts: #{config.contexts.keys}" | ||
| "#{context_key} not found in Retriable contexts (including overrides). Available contexts: #{contexts.keys}" | ||
| end | ||
|
|
||
| return unless block_given? | ||
|
|
||
| retriable(config.contexts[context_key].merge(options), &block) | ||
| context_options = merged_context_options(contexts, context_key, options) | ||
|
|
||
| retriable(context_options, &block) | ||
| end | ||
|
|
||
| def retriable(opts = {}, &block) | ||
| local_config = opts.empty? ? config : Config.new(config.to_h.merge(opts)) | ||
| if opts.empty? && !override_config | ||
| local_config = config | ||
| else | ||
| local_config_hash = config.to_h.merge(opts) | ||
| local_config_hash = deep_merge(local_config_hash, override_config) if override_config | ||
| local_config = Config.new(local_config_hash) | ||
| end | ||
|
|
||
| tries = local_config.tries | ||
| intervals = build_intervals(local_config, tries) | ||
|
|
@@ -128,13 +170,52 @@ def hash_exception_match?(exception, on, exception_list) | |
| end | ||
| end | ||
|
|
||
| def override_config | ||
| @override_config | ||
| end | ||
|
|
||
| def merged_contexts | ||
| return config.contexts unless override_config&.key?(:contexts) | ||
|
|
||
| base_contexts = config.contexts | ||
| override_contexts = override_config[:contexts] | ||
|
|
||
| if override_contexts.is_a?(Hash) | ||
| return deep_merge(base_contexts.is_a?(Hash) ? base_contexts : {}, override_contexts) | ||
| end | ||
| return {} if override_contexts.nil? | ||
|
|
||
| base_contexts | ||
| end | ||
|
|
||
| def merged_context_options(contexts, context_key, options) | ||
| base = contexts[context_key] | ||
| base = {} unless base.is_a?(Hash) | ||
| context_options = base.merge(options) | ||
|
|
||
|
kamui marked this conversation as resolved.
kamui marked this conversation as resolved.
|
||
| return context_options unless override_config | ||
|
|
||
| override_contexts = override_config[:contexts] | ||
| return context_options unless override_contexts.is_a?(Hash) | ||
|
|
||
| override_context_options = override_contexts[context_key] | ||
| return context_options unless override_context_options.is_a?(Hash) | ||
|
|
||
| deep_merge(context_options, override_context_options) | ||
| end | ||
|
Comment on lines
+191
to
+205
|
||
|
|
||
| private_class_method( | ||
| :deep_merge, | ||
| :deep_dup, | ||
| :execute_tries, | ||
| :build_intervals, | ||
| :call_with_timeout, | ||
| :call_on_retry, | ||
| :can_retry?, | ||
| :retriable_exception?, | ||
| :hash_exception_match?, | ||
| :override_config, | ||
| :merged_contexts, | ||
| :merged_context_options, | ||
| ) | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Retriable | ||
| VERSION = "3.4.1" | ||
| VERSION = "3.4.2" | ||
| end |
Uh oh!
There was an error while loading. Please reload this page.