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

Add lock to item when lock_type is pulled from Job class sidekiq options #786

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
1 change: 1 addition & 0 deletions lib/sidekiq_unique_jobs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
require "sidekiq_unique_jobs/core_ext"
require "sidekiq_unique_jobs/lock_timeout"
require "sidekiq_unique_jobs/lock_ttl"
require "sidekiq_unique_jobs/lock_type"
require "sidekiq_unique_jobs/lock_args"
require "sidekiq_unique_jobs/lock_digest"
require "sidekiq_unique_jobs/unlockable"
Expand Down
7 changes: 6 additions & 1 deletion lib/sidekiq_unique_jobs/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ module SidekiqUniqueJobs
module Job
extend self

# Adds timeout, expiration, lock_args, lock_prefix and lock_digest to the sidekiq job hash
# Adds lock, timeout, expiration, lock_args, lock_prefix, and lock_digest to the sidekiq job hash
# @return [Hash] the job hash
def prepare(item)
stringify_on_conflict_hash(item)
add_lock_type(item)
add_lock_timeout(item)
add_lock_ttl(item)
add_digest(item)
Expand Down Expand Up @@ -54,5 +55,9 @@ def add_lock_digest(item)
def add_lock_prefix(item)
item[LOCK_PREFIX] ||= SidekiqUniqueJobs.config.lock_prefix
end

def add_lock_type(item)
item[LOCK] ||= SidekiqUniqueJobs::LockType.call(item)
end
end
end
37 changes: 37 additions & 0 deletions lib/sidekiq_unique_jobs/lock_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

module SidekiqUniqueJobs
# Calculates the lock type
#
class LockType
# includes "SidekiqUniqueJobs::SidekiqWorkerMethods"
# @!parse include SidekiqUniqueJobs::SidekiqWorkerMethods
include SidekiqUniqueJobs::SidekiqWorkerMethods

#
# Computes lock type from job arguments, sidekiq_options.
#
# @return [Symbol] the lock type
# @return [NilClass] if no lock type is found.
#
def self.call(item)
new(item).call
end

# @!attribute [r] item
# @return [Hash] the Sidekiq job hash
attr_reader :item

# @param [Hash] item the Sidekiq job hash
# @option item [Symbol, nil] :lock the type of lock to use.
# @option item [String] :class the class of the sidekiq worker
def initialize(item)
@item = item
self.job_class = item[CLASS]
end

def call
item[LOCK] || job_options[LOCK] || default_job_options[LOCK]
end
end
end
2 changes: 1 addition & 1 deletion lib/sidekiq_unique_jobs/options_with_fallback.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def lock_class
# The type of lock for this worker
#
#
# @return [Symbol]
# @return [Symbol, NilClass]
#
def lock_type
@lock_type ||= options[LOCK] || item[LOCK]
Expand Down
1 change: 1 addition & 0 deletions spec/sidekiq_unique_jobs/job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"lock_timeout" => MyUniqueJob.get_sidekiq_options["lock_timeout"].to_i,
"lock_ttl" => MyUniqueJob.get_sidekiq_options["lock_ttl"],
"lock_args" => args,
"lock" => :until_executed,
"lock_digest" => "uniquejobs:6b6835a019cad7c2a7a4e53e20a9184c",
"lock_prefix" => "uniquejobs",
},
Expand Down
46 changes: 46 additions & 0 deletions spec/sidekiq_unique_jobs/lock_type_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

RSpec.describe SidekiqUniqueJobs::LockType do
let(:lock_calculator) { described_class.new("class" => job_class_name, "lock" => lock) }
let(:job_class_name) { "MyUniqueJob" }
let(:lock) { :until_expired }

describe "public api" do
subject { lock_calculator }

it { is_expected.to respond_to(:job_class) }
it { is_expected.to respond_to(:job_options) }
it { is_expected.to respond_to(:default_job_options) }
end

describe "#call" do
subject(:calculated_lock) { lock_calculator.call }

it { is_expected.to eq(lock) }

context "when item's lock is nil" do
let(:lock) { nil }

it "is expected to equal the job class' lock option" do
expect(MyUniqueJob.sidekiq_options["lock"]).to eq(:until_executed)
expect(calculated_lock).to eq(:until_executed)
end

context "when job class sidekiq options lock is nil" do
it "uses the Sidekiq default job options" do
allow(MyUniqueJob).to receive(:get_sidekiq_options).and_return({})
allow(lock_calculator).to receive(:default_job_options).and_return({ "lock" => :until_executing })
expect(calculated_lock).to eq(:until_executing)
end
end
end
end

describe "#job_class" do
subject(:job_class) { lock_calculator.job_class }

let(:job_class_name) { "MyUniqueJob" }

it { is_expected.to eq(MyUniqueJob) }
end
end