diff --git a/lib/sidekiq-status/client_middleware.rb b/lib/sidekiq-status/client_middleware.rb index f77c88a..b49f928 100644 --- a/lib/sidekiq-status/client_middleware.rb +++ b/lib/sidekiq-status/client_middleware.rb @@ -22,7 +22,7 @@ def initialize(opts = {}) 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 + klass = (!msg["args"][0].is_a?(String) && msg["args"][0]["job_class"]) || worker_class rescue worker_class job_class = if klass.is_a?(Class) klass elsif Module.const_defined?(klass) diff --git a/lib/sidekiq-status/server_middleware.rb b/lib/sidekiq-status/server_middleware.rb index 507fdd1..0df364c 100644 --- a/lib/sidekiq-status/server_middleware.rb +++ b/lib/sidekiq-status/server_middleware.rb @@ -34,7 +34,7 @@ def call(worker, msg, queue) expiry = @expiration # Determine the actual job class - klass = msg["args"][0]["job_class"] || msg["class"] rescue msg["class"] + klass = (!msg["args"][0].is_a?(String) && msg["args"][0]["job_class"]) || msg["class"] rescue msg["class"] job_class = klass.is_a?(Class) ? klass : Module.const_get(klass) # Bypass unless this is a Sidekiq::Status::Worker job diff --git a/spec/lib/sidekiq-status/client_middleware_spec.rb b/spec/lib/sidekiq-status/client_middleware_spec.rb index 8f9796f..cd9e29e 100644 --- a/spec/lib/sidekiq-status/client_middleware_spec.rb +++ b/spec/lib/sidekiq-status/client_middleware_spec.rb @@ -38,6 +38,14 @@ end end + context "when first argument is a string containing substring 'job_class'" do + it "uses the constantized class name" do + expect(StubJob.perform_async 'a string with job_class inside').to eq(job_id) + expect(redis.hget("sidekiq:status:#{job_id}", :status)).to eq('queued') + expect(Sidekiq::Status::queued?(job_id)).to be_truthy + expect(Sidekiq::Status::get_all(job_id)).to include('worker' => 'StubJob') + end + end end describe "with :expiration parameter" do diff --git a/spec/lib/sidekiq-status/server_middleware_spec.rb b/spec/lib/sidekiq-status/server_middleware_spec.rb index 4c2c807..9294c89 100644 --- a/spec/lib/sidekiq-status/server_middleware_spec.rb +++ b/spec/lib/sidekiq-status/server_middleware_spec.rb @@ -45,6 +45,19 @@ expect(Sidekiq::Status::failed?(job_id)).to be_truthy end + context "when first argument is a string containing substring 'job_class'" do + it "uses the default class name" do + allow(SecureRandom).to receive(:hex).once.and_return(job_id) + start_server do + expect(capture_status_updates(3) { + expect(ConfirmationJob.perform_async 'a string with job_class inside').to eq(job_id) + }).to eq([job_id]*3) + end + expect(redis.hget("sidekiq:status:#{job_id}", :status)).to eq('complete') + expect(Sidekiq::Status::get_all(job_id)).to include('worker' => 'ConfirmationJob') + end + end + context "when Sidekiq::Status::Worker is not included in the job" do it "should not set a failed status" do allow(SecureRandom).to receive(:hex).once.and_return(job_id)