Skip to content
This repository was archived by the owner on Apr 1, 2023. It is now read-only.
Open
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
28 changes: 19 additions & 9 deletions lib/sidekiq-status/client_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,10 @@ def initialize(opts = {})
# @param [String] queue the queue's name
# @param [ConnectionPool] redis_pool optional redis connection pool
def call(worker_class, msg, queue, redis_pool=nil)

# Determine the actual job class
klass = msg["args"][0]["job_class"] || worker_class rescue worker_class
job_class = if klass.is_a?(Class)
klass
elsif Module.const_defined?(klass)
Module.const_get(klass)
else
nil
end

job_class = resolve_job_class(klass)

# Store data if the job is a Sidekiq::Status::Worker
if job_class && job_class.ancestors.include?(Sidekiq::Status::Worker)
Expand All @@ -37,11 +31,11 @@ def call(worker_class, msg, queue, redis_pool=nil)
worker: Sidekiq::Job.new(msg, queue).display_class,
args: display_args(msg, queue)
}

store_for_id msg['jid'], initial_metadata, job_class.new.expiration || @expiration, redis_pool
end

yield

end

def display_args(msg, queue)
Expand All @@ -51,6 +45,22 @@ def display_args(msg, queue)
# For Sidekiq ~> 2.7
return msg['args'].to_a.empty? ? nil : msg['args'].to_json
end

def resolve_job_class(klass)
return klass if klass.is_a?(Class)

load_constant(klass)
end

# There is a condition where Module.const_defined? will return false if the Module has not yet been loaded. This
# is can lead to some confusing debug. Instead, this will attempt to load the const, and if that fails defer
# to #const_defiend? to return the proper result boolean response.
##
def load_constant(name)
Module.const_get(name)
rescue NameError
nil
end
end

# Helper method to easily configure sidekiq-status client middleware
Expand Down