Skip to content

Commit

Permalink
Deprecate configuration options with default_ (#504)
Browse files Browse the repository at this point in the history
Close #502
  • Loading branch information
mhenrixon committed May 19, 2020
1 parent ff3c17f commit 2355738
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 28 deletions.
26 changes: 24 additions & 2 deletions lib/sidekiq_unique_jobs/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
module SidekiqUniqueJobs
# ThreadSafe config exists to be able to document the config class without errors
ThreadSafeConfig = Concurrent::MutableStruct.new("ThreadSafeConfig",
:default_lock_timeout,
:default_lock_ttl,
:lock_timeout,
:lock_ttl,
:enabled,
:unique_prefix,
:logger,
Expand All @@ -24,6 +24,7 @@ module SidekiqUniqueJobs
# Shared class for dealing with gem configuration
#
# @author Mauro Berlanda <mauro.berlanda@gmail.com>
# rubocop:disable Metrics/ClassLength
class Config < ThreadSafeConfig
#
# @return [Hash<Symbol, SidekiqUniqueJobs::Lock::BaseLock] all available queued locks
Expand Down Expand Up @@ -183,6 +184,26 @@ def self.default # rubocop:disable Metrics/MethodLength
)
end

def default_lock_ttl=(obj)
warn "[DEPRECATION] `#{self.class}##{__method__}` is deprecated. Please use `#{self.class}#lock_ttl=` instead."
self.lock_ttl = obj
end

def default_lock_timeout=(obj)
warn "[DEPRECATION] `#{self.class}##{__method__}` is deprecated. Please use `#{self.class}#lock_timeout=` instead."
self.lock_timeout = obj
end

def default_lock_ttl
warn "[DEPRECATION] `#{self.class}##{__method__}` is deprecated. Please use `#{self.class}#lock_ttl` instead."
lock_ttl
end

def default_lock_timeout
warn "[DEPRECATION] `#{self.class}##{__method__}` is deprecated. Please use `#{self.class}#lock_timeout` instead."
lock_timeout
end

#
# Adds a lock type to the configuration. It will raise if the lock exists already
#
Expand Down Expand Up @@ -234,4 +255,5 @@ def redis_version
current_redis_version
end
end
# rubocop:enable Metrics/ClassLength
end
4 changes: 2 additions & 2 deletions lib/sidekiq_unique_jobs/lock_timeout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ def calculate

#
# The configured default_lock_timeout
# @see SidekiqUniqueJobs::Config#default_lock_timeout
# @see SidekiqUniqueJobs::Config#lock_timeout
#
#
# @return [Integer, nil]
#
def default_lock_timeout
SidekiqUniqueJobs.config.default_lock_timeout
SidekiqUniqueJobs.config.lock_timeout
end
end
end
17 changes: 3 additions & 14 deletions lib/sidekiq_unique_jobs/lock_ttl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class LockTTL

#
# Computes lock ttl from job arguments, sidekiq_options.
# Falls back to {default_lock_ttl}
# Falls back to {SidekiqUniqueJobs::Config#lock_ttl}
#
# @note this method takes into consideration the time
# until a job is scheduled
Expand Down Expand Up @@ -57,7 +57,7 @@ def scheduled_at

#
# Computes lock ttl from job arguments, sidekiq_options.
# Falls back to {default_lock_ttl}
# Falls back to {SidekiqUniqueJobs::Config#lock_ttl}
#
# @note this method takes into consideration the time
# until a job is scheduled
Expand All @@ -70,19 +70,8 @@ def calculate
ttl ||= worker_options[LOCK_TTL]
ttl ||= item[LOCK_EXPIRATION] # TODO: Deprecate at some point
ttl ||= worker_options[LOCK_EXPIRATION] # TODO: Deprecate at some point
ttl ||= default_lock_ttl
ttl ||= SidekiqUniqueJobs.config.lock_ttl
ttl && ttl.to_i + time_until_scheduled
end

#
# The configured default_lock_ttl
# @see SidekiqUniqueJobs::Config#default_lock_ttl
#
#
# @return [Integer, nil]
#
def default_lock_ttl
SidekiqUniqueJobs.config.default_lock_ttl
end
end
end
3 changes: 2 additions & 1 deletion lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ def toggle(enabled)
#
# This is usually called once at startup of an application
# @param [Hash] options global gem options
# @option options [Integer] :default_lock_timeout (default is 0)
# @option options [Integer] :lock_timeout (default is 0)
# @option options [Integer] :lock_ttl (default is 0)
# @option options [true,false] :enabled (default is true)
# @option options [String] :unique_prefix (default is 'uniquejobs')
# @option options [Logger] :logger (default is Sidekiq.logger)
Expand Down
14 changes: 14 additions & 0 deletions sidekiq-unique-jobs.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ Gem::Specification.new do |spec|
spec.metadata["source_code_uri"] = "https://github.com/mhenrixon/sidekiq-unique-jobs"
spec.metadata["changelog_uri"] = "https://github.com/mhenrixon/sidekiq-unique-jobs/CHANGELOG.md"

spec.post_install_message = <<~POST_INSTALL
This version deprecated the configuration options:
- default_lock_ttl
- default_lock_ttl=
- default_lock_timeout
- default_lock_timeout=
The new methods to use are:
- lock_ttl
- lock_ttl=
- lock_timeout
- lock_timeout=
POST_INSTALL

spec.bindir = "bin"
spec.executables = %w[uniquejobs]

Expand Down
78 changes: 74 additions & 4 deletions spec/sidekiq_unique_jobs/config_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,81 @@
# frozen_string_literal: true

RSpec.describe SidekiqUniqueJobs::Config do
describe ".locks" do
let(:config) { described_class.default }
let(:config) { described_class.default }

describe "#default_lock_ttl=" do
subject(:set_config) { config.default_lock_ttl = new_value }

let(:new_value) { 99 }

before do
allow(config).to receive(:warn)
end

it "warns about deprecation" do
set_config
expect(config).to have_received(:warn).with(
"[DEPRECATION] `SidekiqUniqueJobs::Config#default_lock_ttl=` is deprecated." \
" Please use `SidekiqUniqueJobs::Config#lock_ttl=` instead.",
)
expect(config.lock_ttl).to eq(new_value)
end
end

describe "#default_lock_timeout=" do
subject(:set_config) { config.default_lock_timeout = new_value }

let(:new_value) { 99 }

before do
allow(config).to receive(:warn)
end

it "warns about deprecation" do
set_config
expect(config).to have_received(:warn).with(
"[DEPRECATION] `SidekiqUniqueJobs::Config#default_lock_timeout=` is deprecated." \
" Please use `SidekiqUniqueJobs::Config#lock_timeout=` instead.",
)
expect(config.lock_timeout).to eq(new_value)
end
end

describe "#default_lock_ttl" do
subject(:get_config) { config.default_lock_ttl }

before do
allow(config).to receive(:warn)
end

it "warns about deprecation" do
get_config
expect(config).to have_received(:warn).with(
"[DEPRECATION] `SidekiqUniqueJobs::Config#default_lock_ttl` is deprecated." \
" Please use `SidekiqUniqueJobs::Config#lock_ttl` instead.",
)
expect(config.lock_ttl).to eq(get_config)
end
end

describe "#default_lock_timeout" do
subject(:get_config) { config.default_lock_timeout }

before do
allow(config).to receive(:warn)
end

it "warns about deprecation" do
get_config
expect(config).to have_received(:warn).with(
"[DEPRECATION] `SidekiqUniqueJobs::Config#default_lock_timeout` is deprecated." \
" Please use `SidekiqUniqueJobs::Config#lock_timeout` instead.",
)
expect(config.lock_timeout).to eq(get_config)
end
end

describe ".locks" do
context "when using default config" do
it "falls back on default option" do
expect(config.locks).to eq(SidekiqUniqueJobs::Config::LOCKS)
Expand Down Expand Up @@ -49,8 +121,6 @@
end

describe ".strategies" do
let(:config) { described_class.default }

context "when using default config" do
it "falls back on default option" do
expect(config.strategies).to eq(SidekiqUniqueJobs::Config::STRATEGIES)
Expand Down
2 changes: 1 addition & 1 deletion spec/sidekiq_unique_jobs/lock_timeout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
let(:sidekiq_options) { { lock_timeout: 99 } }

context "with global config", :with_global_config do
let(:global_config) { { default_lock_timeout: 66 } }
let(:global_config) { { lock_timeout: 66 } }

# rubocop:disable RSpec/NestedGroups
context "with worker options", :with_worker_options do
Expand Down
8 changes: 4 additions & 4 deletions spec/sidekiq_unique_jobs/sidekiq_unique_jobs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
describe ".config" do
subject(:config) { described_class.config }

it { is_expected.to be_a(SidekiqUniqueJobs::Config) }
its(:default_lock_timeout) { is_expected.to eq(0) }
its(:enabled) { is_expected.to eq(true) }
its(:unique_prefix) { is_expected.to eq("uniquejobs") }
it { is_expected.to be_a(SidekiqUniqueJobs::Config) }
its(:lock_timeout) { is_expected.to eq(0) }
its(:enabled) { is_expected.to eq(true) }
its(:unique_prefix) { is_expected.to eq("uniquejobs") }
end

describe ".use_config" do
Expand Down

0 comments on commit 2355738

Please sign in to comment.