diff --git a/lib/sidekiq.rb b/lib/sidekiq.rb index 1e5527da..fcb33647 100644 --- a/lib/sidekiq.rb +++ b/lib/sidekiq.rb @@ -26,6 +26,7 @@ module Sidekiq timeout: 25, poll_interval_average: nil, average_scheduled_poll_interval: 5, + on_complex_arguments: :warn, error_handlers: [], death_handlers: [], lifecycle_events: { @@ -252,8 +253,8 @@ module Sidekiq options[:lifecycle_events][event] << block end - def self.strict_mode!(val = true) - options[:raise_on_complex_arguments] = val + def self.strict_mode!(mode = :raise) + options[:on_complex_arguments] = mode end # We are shutting down Sidekiq but what about workers that diff --git a/lib/sidekiq/job_util.rb b/lib/sidekiq/job_util.rb index 75af6f4d..aff0528c 100644 --- a/lib/sidekiq/job_util.rb +++ b/lib/sidekiq/job_util.rb @@ -13,18 +13,16 @@ module Sidekiq raise(ArgumentError, "Job 'at' must be a Numeric timestamp: `#{item}`") if item.key?("at") && !item["at"].is_a?(Numeric) raise(ArgumentError, "Job tags must be an Array: `#{item}`") if item["tags"] && !item["tags"].is_a?(Array) - if Sidekiq.options[:raise_on_complex_arguments] && !json_safe?(item) + if Sidekiq.options[:on_complex_arguments] == :raise msg = <<~EOM - Arguments must be native JSON types, see https://github.com/mperham/sidekiq/wiki/Best-Practices. - + Job arguments to #{item["class"]} must be native JSON types, see https://github.com/mperham/sidekiq/wiki/Best-Practices. To disable this error, remove `Sidekiq.strict_mode!` from your initializer. EOM - raise(ArgumentError, msg) - elsif !Sidekiq.options[:disable_complex_argument_warning] && Sidekiq.options[:environment] == "development" && !json_safe?(item) - Sidekiq.logger.warn <<~EOM - Job arguments do not serialize to JSON safely. This will raise an error in Sidekiq 7.0. - - See https://github.com/mperham/sidekiq/wiki/Best-Practices or raise the error today + raise(ArgumentError, msg) unless json_safe?(item) + elsif Sidekiq.options[:on_complex_arguments] == :warn + Sidekiq.logger.warn <<~EOM unless json_safe?(item) + Job arguments to #{item["class"]} do not serialize to JSON safely. This will raise an error in + Sidekiq 7.0. See https://github.com/mperham/sidekiq/wiki/Best-Practices or raise an error today by calling `Sidekiq.strict_mode!` during Sidekiq initialization. EOM end @@ -61,7 +59,7 @@ module Sidekiq private def json_safe?(item) - JSON.parse(JSON.dump(item['args'])) == item['args'] + JSON.parse(JSON.dump(item["args"])) == item["args"] end end end