-
-
Notifications
You must be signed in to change notification settings - Fork 277
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
Provide some configuration DSL for custom Strategies and Locks #383
Merged
mhenrixon
merged 7 commits into
mhenrixon:master
from
mberlanda:mberlanda/configuration-draft
Apr 15, 2019
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5991f30
chore: added sample configuration for LOCKS
mberlanda f6a45bb
chore: extracted SidekiqUniqueJobs::Config into a separated class and…
mberlanda f0d7cf9
chore: added strategies to the config class
mberlanda e747b4e
testing: increased code coverage
mberlanda bd07aa0
chore: add integration test to prove that the PR works
mberlanda 0582984
doc updated
mberlanda 2e99f64
applied #383 review
mberlanda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# frozen_string_literal: true | ||
|
||
module SidekiqUniqueJobs | ||
# Shared class for dealing with gem configuration | ||
# | ||
# @author Mauro Berlanda <mauro.berlanda@gmail.com> | ||
class Config < Concurrent::MutableStruct.new( | ||
:default_lock_timeout, | ||
:enabled, | ||
:unique_prefix, | ||
:logger, | ||
:locks, | ||
:strategies, | ||
) | ||
DEFAULT_LOCKS = { | ||
until_and_while_executing: SidekiqUniqueJobs::Lock::UntilAndWhileExecuting, | ||
until_executed: SidekiqUniqueJobs::Lock::UntilExecuted, | ||
until_executing: SidekiqUniqueJobs::Lock::UntilExecuting, | ||
until_expired: SidekiqUniqueJobs::Lock::UntilExpired, | ||
until_timeout: SidekiqUniqueJobs::Lock::UntilExpired, | ||
while_executing: SidekiqUniqueJobs::Lock::WhileExecuting, | ||
while_executing_reject: SidekiqUniqueJobs::Lock::WhileExecutingReject, | ||
}.freeze | ||
|
||
DEFAULT_STRATEGIES = { | ||
log: SidekiqUniqueJobs::OnConflict::Log, | ||
raise: SidekiqUniqueJobs::OnConflict::Raise, | ||
reject: SidekiqUniqueJobs::OnConflict::Reject, | ||
replace: SidekiqUniqueJobs::OnConflict::Replace, | ||
reschedule: SidekiqUniqueJobs::OnConflict::Reschedule, | ||
}.freeze | ||
|
||
# Returns a default configuration | ||
# @return [Concurrent::MutableStruct] a representation of the configuration object | ||
def self.default | ||
new( | ||
0, | ||
true, | ||
"uniquejobs", | ||
Sidekiq.logger, | ||
DEFAULT_LOCKS, | ||
DEFAULT_STRATEGIES, | ||
) | ||
end | ||
|
||
# Adds a lock type to the configuration. It will raise if the lock exists already | ||
# | ||
# @param [String] name the name of the lock | ||
# @param [Class] klass the class describing the lock | ||
def add_lock(name, klass) | ||
raise ArgumentError, "Lock #{name} already defined, please use another name" if locks.key?(name.to_sym) | ||
|
||
new_locks = locks.dup.merge(name.to_sym => klass).freeze | ||
self.locks = new_locks | ||
end | ||
|
||
# Adds an on_conflict strategy to the configuration. | ||
# It will raise if the strategy exists already | ||
# | ||
# @param [String] name the name of the custom strategy | ||
# @param [Class] klass the class describing the strategy | ||
def add_strategy(name, klass) | ||
raise ArgumentError, "strategy #{name} already defined, please use another name" if strategies.key?(name.to_sym) | ||
|
||
new_strategies = strategies.dup.merge(name.to_sym => klass).freeze | ||
self.strategies = new_strategies | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,17 +15,14 @@ module SidekiqUniqueJobs | |
# @author Mikael Henriksson <mikael@zoolutions.se> | ||
# | ||
module OnConflict | ||
STRATEGIES = { | ||
log: OnConflict::Log, | ||
raise: OnConflict::Raise, | ||
reject: OnConflict::Reject, | ||
replace: OnConflict::Replace, | ||
reschedule: OnConflict::Reschedule, | ||
}.freeze | ||
# A convenience method for using the configured strategies | ||
def self.strategies | ||
SidekiqUniqueJobs.strategies | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've used delegation a lot in many places but I honestly prefer a defined method and was planning on refactoring in this direction in some places 👍 |
||
end | ||
|
||
# returns OnConflict::NullStrategy when no other could be found | ||
def self.find_strategy(strategy) | ||
STRATEGIES.fetch(strategy.to_s.to_sym) { OnConflict::NullStrategy } | ||
strategies.fetch(strategy.to_s.to_sym) { OnConflict::NullStrategy } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
spec/integration/sidekiq_unique_jobs/configuration_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
RSpec.describe SidekiqUniqueJobs do | ||
describe "define custom lock strategies" do | ||
class FoobarJob < MyJob | ||
sidekiq_options lock: :foobar, | ||
queue: :customqueue, | ||
on_conflict: :raise | ||
end | ||
|
||
class CustomLock < SidekiqUniqueJobs::Lock::BaseLock | ||
def lock | ||
true | ||
end | ||
end | ||
|
||
subject(:middleware_call) do | ||
SidekiqUniqueJobs::Client::Middleware.new.call(worker_class, item, queue) do | ||
true | ||
end | ||
end | ||
|
||
let(:queue) { :customqueue } | ||
let(:lock_type) { :foobar } | ||
let(:digest) { "1234567890" } | ||
let(:jid) { "randomjid" } | ||
let(:ttl) { nil } | ||
let(:item) do | ||
{ | ||
SidekiqUniqueJobs::UNIQUE_DIGEST_KEY => digest, | ||
SidekiqUniqueJobs::JID_KEY => jid, | ||
SidekiqUniqueJobs::LOCK_EXPIRATION_KEY => ttl, | ||
SidekiqUniqueJobs::LOCK_KEY => lock_type, | ||
} | ||
end | ||
let(:worker_class) { FoobarJob } | ||
|
||
context "when the lock is not defined" do | ||
it "raises SidekiqUniqueJobs::UnknownLock" do | ||
expect { middleware_call }.to raise_exception( | ||
SidekiqUniqueJobs::UnknownLock, /No implementation for `lock: :foobar`/ | ||
) | ||
end | ||
end | ||
|
||
context "when the lock is defined" do | ||
let(:custom_config) do | ||
SidekiqUniqueJobs::Config.default.tap do |cfg| | ||
cfg.add_lock :foobar, CustomLock | ||
end | ||
end | ||
|
||
before do | ||
allow(described_class).to receive(:config).and_return(custom_config) | ||
end | ||
|
||
it "returns the block given" do | ||
expect(middleware_call).to be(true) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the move to a separate file now that it turned complex again! 👍