-
-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OnConflict::Replace: yield when lock was achieved (#640)
* OnConflict, return nil Close #629 The problem originally was that in some situations, a string was returned (for the lock). This should never happen, any conflict strategy must return nil to avoid pretending to be successful. * Refactor (thanks reek for pointing it out) * Ensure the replace strategy yields This should be considered a success * Adds documentation
- Loading branch information
Showing
14 changed files
with
209 additions
and
67 deletions.
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
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
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 |
---|---|---|
|
@@ -11,7 +11,7 @@ class Lock | |
# | ||
# @author Mikael Henriksson <[email protected]> | ||
class WhileExecuting < BaseLock | ||
RUN_SUFFIX ||= ":RUN" | ||
RUN_SUFFIX = ":RUN" | ||
|
||
include SidekiqUniqueJobs::OptionsWithFallback | ||
include SidekiqUniqueJobs::Logging::Middleware | ||
|
@@ -30,22 +30,24 @@ def initialize(item, callback, redis_pool = nil) | |
# @return [true] always returns true | ||
def lock | ||
job_id = item[JID] | ||
yield job_id if block_given? | ||
yield if block_given? | ||
|
||
job_id | ||
end | ||
|
||
# Executes in the Sidekiq server process. | ||
# These jobs are locked in the server process not from the client | ||
# @yield to the worker class perform method | ||
def execute | ||
def execute(&block) | ||
with_logging_context do | ||
call_strategy(origin: :server) unless locksmith.execute do | ||
executed = locksmith.execute do | ||
yield | ||
callback_safely if locksmith.unlock | ||
ensure | ||
locksmith.unlock | ||
end | ||
|
||
call_strategy(origin: :server, &block) unless executed | ||
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
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
20 changes: 20 additions & 0 deletions
20
spec/support/workers/until_and_while_executing_reject_job.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,20 @@ | ||
# frozen_string_literal: true | ||
|
||
# :nocov: | ||
|
||
class UntilAndWhileExecutingRejectJob | ||
include Sidekiq::Worker | ||
|
||
sidekiq_options lock: :until_and_while_executing, | ||
queue: :working, | ||
on_conflict: { | ||
client: :reject, | ||
server: :reject, | ||
} | ||
|
||
def self.lock_args(args) | ||
[args[0]] | ||
end | ||
|
||
def perform(key); end | ||
end |
20 changes: 20 additions & 0 deletions
20
spec/support/workers/until_and_while_executing_replace_job.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,20 @@ | ||
# frozen_string_literal: true | ||
|
||
# :nocov: | ||
|
||
class UntilAndWhileExecutingReplaceJob | ||
include Sidekiq::Worker | ||
|
||
sidekiq_options lock: :until_and_while_executing, | ||
queue: :working, | ||
on_conflict: { | ||
client: :replace, | ||
server: :reschedule, | ||
} | ||
|
||
def self.lock_args(args) | ||
[args[0]] | ||
end | ||
|
||
def perform(key); 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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe UntilAndWhileExecutingRejectJob do | ||
it_behaves_like "sidekiq with options" do | ||
let(:options) do | ||
{ | ||
"queue" => :working, | ||
"retry" => true, | ||
"lock" => :until_and_while_executing, | ||
"on_conflict" => { client: :reject, server: :reject }, | ||
} | ||
end | ||
end | ||
|
||
it "rejects the job successfully" do | ||
Sidekiq::Testing.disable! do | ||
set = Sidekiq::ScheduledSet.new | ||
|
||
described_class.perform_at(Time.now + 30, 1) | ||
expect(set.size).to eq(1) | ||
|
||
expect(described_class.perform_at(Time.now + 30, 1)).to be_nil | ||
|
||
set.each(&:delete) | ||
end | ||
end | ||
|
||
it "rejects job successfully when using perform_in" do | ||
Sidekiq::Testing.disable! do | ||
set = Sidekiq::ScheduledSet.new | ||
|
||
described_class.perform_in(30, 1) | ||
expect(set.size).to eq(1) | ||
|
||
expect(described_class.perform_in(30, 1)).to be_nil | ||
|
||
set.each(&:delete) | ||
end | ||
end | ||
end |
Oops, something went wrong.