Module: Sidekiq::JobUtil
- Included in:
- Client, Worker::Setter
- Defined in:
- lib/sidekiq/job_util.rb
Constant Summary collapse
- TRANSIENT_ATTRIBUTES =
These functions encapsulate various job utilities.
%w[
Instance Method Summary collapse
- #normalize_item(item) ⇒ Object
- #normalized_hash(item_class) ⇒ Object
- #validate(item) ⇒ Object
- #verify_json(item) ⇒ Object
Instance Method Details
#normalize_item(item) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/sidekiq/job_util.rb', line 35 def normalize_item(item) validate(item) # merge in the default sidekiq_options for the item's class and/or wrapped element # this allows ActiveJobs to control sidekiq_options too. defaults = normalized_hash(item["class"]) defaults = defaults.merge(item["wrapped"].) if item["wrapped"].respond_to?(:get_sidekiq_options) item = defaults.merge(item) raise(ArgumentError, "Job must include a valid queue name") if item["queue"].nil? || item["queue"] == "" # remove job attributes which aren't necessary to persist into Redis TRANSIENT_ATTRIBUTES.each { |key| item.delete(key) } item["jid"] ||= SecureRandom.hex(12) item["class"] = item["class"].to_s item["queue"] = item["queue"].to_s item["created_at"] ||= Time.now.to_f item end |
#normalized_hash(item_class) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/sidekiq/job_util.rb', line 56 def normalized_hash(item_class) if item_class.is_a?(Class) raise(ArgumentError, "Message must include a Sidekiq::Job class, not class name: #{item_class.ancestors.inspect}") unless item_class.respond_to?(:get_sidekiq_options) item_class. else Sidekiq. end end |
#validate(item) ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/sidekiq/job_util.rb', line 10 def validate(item) raise(ArgumentError, "Job must be a Hash with 'class' and 'args' keys: `#{item}`") unless item.is_a?(Hash) && item.key?("class") && item.key?("args") raise(ArgumentError, "Job args must be an Array: `#{item}`") unless item["args"].is_a?(Array) raise(ArgumentError, "Job class must be either a Class or String representation of the class name: `#{item}`") unless item["class"].is_a?(Class) || item["class"].is_a?(String) 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) end |
#verify_json(item) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/sidekiq/job_util.rb', line 18 def verify_json(item) job_class = item["wrapped"] || item["class"] if Sidekiq[:on_complex_arguments] == :raise msg = <<~EOM Job arguments to #{job_class} must be native JSON types, see https://github.com/mperham/sidekiq/wiki/Best-Practices. To disable this error, remove `Sidekiq.strict_args!` from your initializer. EOM raise(ArgumentError, msg) unless json_safe?(item) elsif Sidekiq[:on_complex_arguments] == :warn Sidekiq.logger.warn <<~EOM unless json_safe?(item) Job arguments to #{job_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_args!` during Sidekiq initialization. EOM end end |