diff --git a/lib/sidekiq-unique-jobs.rb b/lib/sidekiq-unique-jobs.rb index 17b613f3d..18dee983f 100644 --- a/lib/sidekiq-unique-jobs.rb +++ b/lib/sidekiq-unique-jobs.rb @@ -6,8 +6,6 @@ require 'sidekiq_unique_jobs/cli' require 'sidekiq_unique_jobs/core_ext' require 'sidekiq_unique_jobs/timeout_calculator' -require 'sidekiq_unique_jobs/queue_lock_timeout_calculator' -require 'sidekiq_unique_jobs/run_lock_timeout_calculator' require 'sidekiq_unique_jobs/options_with_fallback' require 'sidekiq_unique_jobs/scripts' require 'sidekiq_unique_jobs/unique_args' diff --git a/lib/sidekiq_unique_jobs/queue_lock_timeout_calculator.rb b/lib/sidekiq_unique_jobs/queue_lock_timeout_calculator.rb deleted file mode 100644 index cb08a4a06..000000000 --- a/lib/sidekiq_unique_jobs/queue_lock_timeout_calculator.rb +++ /dev/null @@ -1,25 +0,0 @@ -module SidekiqUniqueJobs - class QueueLockTimeoutCalculator - include TimeoutCalculator - - def self.for_item(item) - new(item) - end - - def initialize(item) - @item = item - end - - def seconds - time_until_scheduled + queue_lock_expiration - end - - def queue_lock_expiration - @queue_lock_expiration ||= - ( - worker_class_queue_lock_expiration || - SidekiqUniqueJobs.config.default_queue_lock_expiration - ).to_i - end - end -end diff --git a/lib/sidekiq_unique_jobs/run_lock_timeout_calculator.rb b/lib/sidekiq_unique_jobs/run_lock_timeout_calculator.rb deleted file mode 100644 index 11d0070e7..000000000 --- a/lib/sidekiq_unique_jobs/run_lock_timeout_calculator.rb +++ /dev/null @@ -1,25 +0,0 @@ -module SidekiqUniqueJobs - class RunLockTimeoutCalculator - include TimeoutCalculator - - def self.for_item(item) - new(item) - end - - def initialize(item) - @item = item - end - - def seconds - run_lock_expiration - end - - def run_lock_expiration - @run_lock_expiration ||= - ( - worker_class_run_lock_expiration || - SidekiqUniqueJobs.config.default_run_lock_expiration - ).to_i - end - end -end diff --git a/lib/sidekiq_unique_jobs/timeout_calculator.rb b/lib/sidekiq_unique_jobs/timeout_calculator.rb index cf322930e..324f14f0a 100644 --- a/lib/sidekiq_unique_jobs/timeout_calculator.rb +++ b/lib/sidekiq_unique_jobs/timeout_calculator.rb @@ -1,5 +1,13 @@ module SidekiqUniqueJobs - module TimeoutCalculator + class TimeoutCalculator + def self.for_item(item) + new(item) + end + + def initialize(item) + @item = item + end + def time_until_scheduled scheduled = item[AT_KEY] return 0 unless scheduled @@ -31,4 +39,27 @@ def worker_class_expiration_for(key) attr_reader :item end + + class RunLockTimeoutCalculator < TimeoutCalculator + def seconds + @seconds ||= ( + worker_class_run_lock_expiration || + SidekiqUniqueJobs.config.default_run_lock_expiration + ).to_i + end + end + + class QueueLockTimeoutCalculator < TimeoutCalculator + def seconds + queue_lock_expiration + time_until_scheduled + end + + def queue_lock_expiration + @queue_lock_expiration ||= + ( + worker_class_queue_lock_expiration || + SidekiqUniqueJobs.config.default_queue_lock_expiration + ).to_i + end + end end diff --git a/spec/lib/sidekiq_unique_jobs/client/middleware_spec.rb b/spec/lib/sidekiq_unique_jobs/client/middleware_spec.rb index 1434155de..5087bfbb5 100644 --- a/spec/lib/sidekiq_unique_jobs/client/middleware_spec.rb +++ b/spec/lib/sidekiq_unique_jobs/client/middleware_spec.rb @@ -225,7 +225,8 @@ def do_it(_arg) # jobs are set around the same time as the scheduled job itself feel free to improve. it 'expires the digest when a scheduled job is scheduled at' do expected_expires_at = - (Time.at(Time.now.to_i + 15 * 60) - Time.now.utc) + SidekiqUniqueJobs.config.default_queue_lock_expiration + (Time.at(Time.now.to_i + 15 * 60) - Time.now.utc) + + SidekiqUniqueJobs.config.default_queue_lock_expiration jid = MyUniqueJob.perform_in(expected_expires_at, 'mike') item = { 'class' => MyUniqueJob, 'queue' => 'customqueue', diff --git a/spec/lib/sidekiq_unique_jobs/lock/while_executing_spec.rb b/spec/lib/sidekiq_unique_jobs/lock/while_executing_spec.rb index 6292e5f70..40ef9e51c 100644 --- a/spec/lib/sidekiq_unique_jobs/lock/while_executing_spec.rb +++ b/spec/lib/sidekiq_unique_jobs/lock/while_executing_spec.rb @@ -38,10 +38,10 @@ worker = SidekiqUniqueJobs.worker_class_constantize(item[SidekiqUniqueJobs::CLASS_KEY]) allow(worker).to receive(:get_sidekiq_options) - .and_return({"retry"=>true, - "queue"=>:dupsallowed, - "run_lock_expiration" => 0, - "unique"=>:until_and_while_executing}) + .and_return('retry' => true, + 'queue' => :dupsallowed, + 'run_lock_expiration' => 0, + 'unique' => :until_and_while_executing) start_times = [] sleep_time = 0.1 diff --git a/spec/lib/sidekiq_unique_jobs/queue_lock_timeout_calculator_spec.rb b/spec/lib/sidekiq_unique_jobs/queue_lock_timeout_calculator_spec.rb index ba4fd566d..6b8fe6792 100644 --- a/spec/lib/sidekiq_unique_jobs/queue_lock_timeout_calculator_spec.rb +++ b/spec/lib/sidekiq_unique_jobs/queue_lock_timeout_calculator_spec.rb @@ -7,8 +7,8 @@ describe 'public api' do it_behaves_like 'generic unscheduled job' do - it { is_expected.to respond_to(:worker_class_queue_lock_expiration) } it { is_expected.to respond_to(:seconds) } + it { is_expected.to respond_to(:queue_lock_expiration) } end end @@ -30,12 +30,18 @@ end describe '#queue_lock_expiration' do - it_behaves_like 'generic unscheduled job' do - its(:queue_lock_expiration) { is_expected.to eq(SidekiqUniqueJobs.config.default_queue_lock_expiration) } + context 'using default unique_expiration' do + subject { described_class.new(nil) } + before { allow(subject).to receive(:worker_class_queue_lock_expiration).and_return(nil) } + + its(:queue_lock_expiration) { is_expected.to eq(1_800) } end - subject { described_class.new('class' => 'MyUniqueJob') } - its(:queue_lock_expiration) { is_expected.to eq(7_200) } - end + context 'using specified sidekiq option unique_expiration' do + subject { described_class.new(nil) } + before { allow(subject).to receive(:worker_class_queue_lock_expiration).and_return(9) } + its(:queue_lock_expiration) { is_expected.to eq(9) } + end + end end diff --git a/spec/lib/sidekiq_unique_jobs/run_lock_timeout_calculator_spec.rb b/spec/lib/sidekiq_unique_jobs/run_lock_timeout_calculator_spec.rb index 2cf706f62..86cf47d4a 100644 --- a/spec/lib/sidekiq_unique_jobs/run_lock_timeout_calculator_spec.rb +++ b/spec/lib/sidekiq_unique_jobs/run_lock_timeout_calculator_spec.rb @@ -7,7 +7,6 @@ describe 'public api' do it_behaves_like 'generic unscheduled job' do - it { is_expected.to respond_to(:worker_class_run_lock_expiration) } it { is_expected.to respond_to(:seconds) } end end @@ -20,21 +19,18 @@ end describe '#seconds' do - subject { described_class.new(nil) } + context 'using default run_lock_expiration' do + subject { described_class.new(nil) } + before { allow(subject).to receive(:worker_class_run_lock_expiration).and_return(9) } - before do - allow(subject).to receive(:run_lock_expiration).and_return(9) + its(:seconds) { is_expected.to eq(9) } end - its(:seconds) { is_expected.to eq(9) } - end - describe '#run_lock_expiration' do - it_behaves_like 'generic unscheduled job' do - its(:run_lock_expiration) { is_expected.to eq(SidekiqUniqueJobs.config.default_run_lock_expiration) } - end + context 'using specified sidekiq option run_lock_expiration' do + subject { described_class.new(nil) } + before { allow(subject).to receive(:worker_class_run_lock_expiration).and_return(nil) } - subject { described_class.new('class' => 'LongRunningJob') } - its(:run_lock_expiration) { is_expected.to eq(7_200) } + its(:seconds) { is_expected.to eq(60) } + end end - end diff --git a/spec/lib/sidekiq_unique_jobs/timeout_calculator_spec.rb b/spec/lib/sidekiq_unique_jobs/timeout_calculator_spec.rb index 5a3e1c075..b1c6f3c41 100644 --- a/spec/lib/sidekiq_unique_jobs/timeout_calculator_spec.rb +++ b/spec/lib/sidekiq_unique_jobs/timeout_calculator_spec.rb @@ -2,49 +2,35 @@ RSpec.describe SidekiqUniqueJobs::TimeoutCalculator do shared_context 'undefined worker class' do - subject do - Class.new do - include SidekiqUniqueJobs::TimeoutCalculator - def initialize(item) - @item = item - end - end.new('class' => 'test') - end + subject { described_class.new('class' => 'test') } end - shared_context 'generic unscheduled job' do - subject do - Class.new do - include SidekiqUniqueJobs::TimeoutCalculator - def initialize(item) - @item = item - end - end.new('class' => 'MyUniqueJob') - end + shared_context 'item not scheduled' do + subject { described_class.new('class' => 'MyUniqueJob') } end describe 'public api' do - it_behaves_like 'generic unscheduled job' do - it { is_expected.to respond_to(:time_until_scheduled) } - it { is_expected.to respond_to(:worker_class_queue_lock_expiration) } - it { is_expected.to respond_to(:worker_class_run_lock_expiration) } - it { is_expected.to respond_to(:worker_class) } + subject { described_class.new(nil) } + it { is_expected.to respond_to(:time_until_scheduled) } + it { is_expected.to respond_to(:worker_class_queue_lock_expiration) } + it { is_expected.to respond_to(:worker_class_run_lock_expiration) } + it { is_expected.to respond_to(:worker_class) } + it { is_expected.to respond_to(:seconds) } + end + + describe '.for_item' do + it 'initializes a new calculator' do + expect(described_class).to receive(:new).with('WAT') + described_class.for_item('WAT') end end describe '#time_until_scheduled' do - it_behaves_like 'generic unscheduled job' do + it_behaves_like 'item not scheduled' do its(:time_until_scheduled) { is_expected.to eq(0) } end - subject do - Class.new do - include SidekiqUniqueJobs::TimeoutCalculator - def initialize(item) - @item = item - end - end.new('class' => 'MyUniqueJob', 'at' => schedule_time) - end + subject { described_class.new('class' => 'MyUniqueJob', 'at' => schedule_time) } let(:schedule_time) { Time.now.utc.to_i + 24 * 60 * 60 } let(:now_in_utc) { Time.now.utc.to_i } @@ -57,28 +43,20 @@ def initialize(item) describe '#worker_class_queue_lock_expiration' do it_behaves_like 'undefined worker class' do - its (:worker_class_queue_lock_expiration) { is_expected.to eq(nil) } + its(:worker_class_queue_lock_expiration) { is_expected.to eq(nil) } end - it_behaves_like 'generic unscheduled job' do - its (:worker_class_queue_lock_expiration) { is_expected.to eq(7_200) } - end + subject { described_class.new('class' => 'MyUniqueJob') } + its(:worker_class_queue_lock_expiration) { is_expected.to eq(7_200) } end describe '#worker_class_run_lock_expiration' do it_behaves_like 'undefined worker class' do - its (:worker_class_queue_lock_expiration) { is_expected.to eq(nil) } + its(:worker_class_run_lock_expiration) { is_expected.to eq(nil) } end - subject do - Class.new do - include SidekiqUniqueJobs::TimeoutCalculator - def initialize(item) - @item = item - end - end.new('class' => 'LongRunningJob') - end - its (:worker_class_run_lock_expiration) { is_expected.to eq(7_200) } + subject { described_class.new('class' => 'LongRunningJob') } + its(:worker_class_run_lock_expiration) { is_expected.to eq(7_200) } end describe '#worker_class' do @@ -86,14 +64,7 @@ def initialize(item) its(:worker_class) { is_expected.to eq('test') } end - subject do - Class.new do - include SidekiqUniqueJobs::TimeoutCalculator - def initialize(item) - @item = item - end - end.new('class' => 'MyJob') - end + subject { described_class.new('class' => 'MyJob') } its(:worker_class) { is_expected.to eq(MyJob) } end end