-
-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor inline adapter to enable deferred execution after enqueue to…
… allow batch-callbacks to use transaction-based advisory lock (#1433)
- Loading branch information
1 parent
eaace45
commit 02fb0b6
Showing
8 changed files
with
202 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'active_support/core_ext/module/attribute_accessors_per_thread' | ||
|
||
module GoodJob | ||
class Adapter | ||
# The InlineBuffer is integrated into the Adapter and captures jobs that have been enqueued inline. | ||
# The purpose is allow job records to be persisted, in a locked state, while within a transaction, | ||
# and then execute the jobs after the transaction has been committed to ensure that the jobs | ||
# do not run within a transaction. | ||
# | ||
# @private This is intended for internal GoodJob usage only. | ||
class InlineBuffer | ||
# @!attribute [rw] current_buffer | ||
# @!scope class | ||
# Current buffer of jobs to be enqueued. | ||
# @return [GoodJob::Adapter::InlineBuffer, nil] | ||
thread_mattr_accessor :current_buffer | ||
|
||
# This block should be used to wrap the transaction that could enqueue jobs. | ||
# @yield The block that may enqueue jobs. | ||
# @return [Proc] A proc that will execute enqueued jobs after the transaction has been committed. | ||
# @example Wrapping a transaction | ||
# buffer = GoodJob::Adapter::InlineBuffer.capture do | ||
# ActiveRecord::Base.transaction do | ||
# MyJob.perform_later | ||
# end | ||
# end | ||
# buffer.call | ||
def self.capture | ||
if current_buffer | ||
yield | ||
return proc {} | ||
end | ||
|
||
begin | ||
self.current_buffer = new | ||
yield | ||
current_buffer.to_proc | ||
ensure | ||
self.current_buffer = nil | ||
end | ||
end | ||
|
||
# Used within the adapter to wrap inline job execution | ||
def self.perform_now_or_defer(&block) | ||
if defer? | ||
current_buffer.defer(block) | ||
else | ||
yield | ||
end | ||
end | ||
|
||
def self.defer? | ||
current_buffer.present? | ||
end | ||
|
||
def initialize | ||
@callables = [] | ||
end | ||
|
||
def defer(callable) | ||
@callables << callable | ||
end | ||
|
||
def to_proc | ||
proc do | ||
@callables.map(&:call) | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
describe GoodJob::Adapter::InlineBuffer do | ||
before do | ||
stub_const 'SuccessJob', (Class.new(ActiveJob::Base) do | ||
def perform | ||
true | ||
end | ||
end) | ||
SuccessJob.queue_adapter = GoodJob::Adapter.new(execution_mode: :inline) | ||
|
||
stub_const 'ErrorJob', (Class.new(ActiveJob::Base) do | ||
def perform | ||
raise 'Error' | ||
end | ||
end) | ||
ErrorJob.queue_adapter = GoodJob::Adapter.new(execution_mode: :inline) | ||
end | ||
|
||
context "when using enqueue_all" do | ||
it "defers enqueue_all" do | ||
Rails.application.executor.wrap do | ||
buffer = described_class.capture do | ||
SuccessJob.queue_adapter.enqueue_all([SuccessJob.new, SuccessJob.new]) | ||
expect(GoodJob::Job.count).to eq 2 | ||
expect(GoodJob::Job.all).to all have_attributes(finished_at: nil) | ||
end | ||
|
||
buffer.call | ||
|
||
expect(GoodJob::Job.all).to all have_attributes(finished_at: be_present) | ||
end | ||
end | ||
|
||
it "defers enqueue_all with errors" do | ||
Rails.application.executor.wrap do | ||
buffer = described_class.capture do | ||
ErrorJob.queue_adapter.enqueue_all([ErrorJob.new, SuccessJob.new]) | ||
expect(GoodJob::Job.count).to eq 2 | ||
expect(GoodJob::Job.all).to all have_attributes(finished_at: nil) | ||
end | ||
|
||
expect { buffer.call }.to raise_error(/Error/) | ||
expect(GoodJob::Job.find_by(job_class: "ErrorJob")).to have_attributes(finished_at: be_present, error: be_present, error_event: 'unhandled') | ||
expect(GoodJob::Job.find_by(job_class: "SuccessJob")).to have_attributes(finished_at: nil) | ||
end | ||
end | ||
end | ||
|
||
context "when using enqueue" do | ||
it "defers inline enqueued jobs" do | ||
Rails.application.executor.wrap do | ||
buffer = described_class.capture do | ||
SuccessJob.perform_later | ||
expect(GoodJob::Job.count).to eq 1 | ||
end | ||
buffer.call | ||
|
||
expect(GoodJob::Job.count).to eq 1 | ||
expect(GoodJob::Job.first).to have_attributes(finished_at: be_present) | ||
end | ||
end | ||
|
||
it "defers inline enqueued jobs with errors" do | ||
Rails.application.executor.wrap do | ||
buffer = described_class.capture do | ||
ErrorJob.perform_later | ||
expect(GoodJob::Job.count).to eq 1 | ||
end | ||
|
||
expect { buffer.call }.to raise_error(/Error/) | ||
expect(GoodJob::Job.first).to have_attributes(finished_at: be_present, error: be_present, error_event: 'unhandled') | ||
end | ||
end | ||
end | ||
end |