Skip to content
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

Fix access to both server and client conflict #503

Merged
merged 2 commits into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions lib/sidekiq_unique_jobs/core_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,30 @@ def slice(*keys)
end
end

unless {}.respond_to?(:deep_stringify_keys)
#
# Depp converts all keys to string
#
#
# @return [Hash<String>]
#
def deep_stringify_keys
deep_transform_keys(&:to_s)
end
end

unless {}.respond_to?(:deep_transform_keys)
#
# Deep transfor all keys by yielding to the caller
#
#
# @return [Hash<String>]
#
def deep_transform_keys(&block)
_deep_transform_keys_in_object(self, &block)
end
end

unless {}.respond_to?(:stringify_keys)
#
# Converts all keys to string
Expand Down Expand Up @@ -66,6 +90,24 @@ def slice!(*keys)
omit
end
end

private

unless {}.respond_to?(:_deep_transform_keys_in_object)
# support methods for deep transforming nested hashes and arrays
def _deep_transform_keys_in_object(object, &block)
case object
when Hash
object.each_with_object({}) do |(key, value), result|
result[yield(key)] = _deep_transform_keys_in_object(value, &block)
end
when Array
object.map { |e| _deep_transform_keys_in_object(e, &block) }
else
object
end
end
end
end

#
Expand Down
6 changes: 3 additions & 3 deletions lib/sidekiq_unique_jobs/lock_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class LockConfig
# @return [LockConfig]
#
def self.from_worker(options)
new(options.stringify_keys)
new(options.deep_stringify_keys)
end

def initialize(job_hash = {})
Expand Down Expand Up @@ -107,13 +107,13 @@ def errors_as_string

# the strategy to use as conflict resolution from sidekiq client
def on_client_conflict
@on_client_conflict ||= on_conflict[:client] if on_conflict.is_a?(Hash)
@on_client_conflict ||= on_conflict["client"] if on_conflict.is_a?(Hash)
@on_client_conflict ||= on_conflict
end

# the strategy to use as conflict resolution from sidekiq server
def on_server_conflict
@on_server_conflict ||= on_conflict[:server] if on_conflict.is_a?(Hash)
@on_server_conflict ||= on_conflict["server"] if on_conflict.is_a?(Hash)
@on_server_conflict ||= on_conflict
end
end
Expand Down