Skip to content

Commit

Permalink
Merge pull request #8 from BookingSync/missing-outbox-records
Browse files Browse the repository at this point in the history
add config option to raise error when outbox entry record is not found
  • Loading branch information
Azdaroth authored May 26, 2023
2 parents 4a3d5af + c58d7e5 commit 66a8133
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## [Unreleased]

## [0.3.1] - 2023-05-24

- add config option whether to raise error when outbox entry record is not found

## [0.3.0] - 2022-12-20

- Move to file-based healthchecks, instead of using Redis-based ones.
Expand Down
6 changes: 3 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ GIT
PATH
remote: .
specs:
rails-transactional-outbox (0.3.0)
rails-transactional-outbox (0.3.1)
activerecord (>= 5)
activesupport (>= 3.2)
concurrent-ruby
Expand Down Expand Up @@ -55,7 +55,7 @@ GEM
dry-events (~> 1.0, < 2)
exponential-backoff (0.0.4)
ffi (1.15.5)
file-based-healthcheck (0.1.1)
file-based-healthcheck (0.2.0)
activesupport (>= 3.2)
i18n (1.12.0)
concurrent-ruby (~> 1.0)
Expand Down Expand Up @@ -121,7 +121,7 @@ GEM
tzinfo (2.0.5)
concurrent-ruby (~> 1.0)
unicode-display_width (2.2.0)
zeitwerk (2.6.6)
zeitwerk (2.6.8)

PLATFORMS
x86_64-darwin-18
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Rails.application.config.to_prepare do
config.transactional_outbox_worker_idle_delay_multiplier = 5 # optional, defaults to 1, if there are no outbox entries to be processed, then the sleep time for the thread will be equal to transactional_outbox_worker_idle_delay_multiplier * transactional_outbox_worker_sleep_seconds
config.outbox_batch_size = 100 # optional, defaults to 100
config.add_record_processor(MyCustomOperationProcerssor) # optional, by default it contains only one processor for ActiveRecord, but you could add more
config.raise_not_found_model_error = true # optional, defaults to true. Should the error be raised if outbox entry model is not found

config.lock_client = Redlock::Client.new([ENV["REDIS_URL"]]) # required if you want to use RailsTransactionalOutbox::OutboxEntriesProcessors::OrderedByCausalityKeyProcessor, defaults to RailsTransactionalOutbox::NullLockClient. Check its interface and the interface of `redlock` gem. To cut the long story short, when the lock is acquired, a hash with the structure outlined in RailsTransactionalOutbox::NullLockClient should be yielded, if the lock is not acquired, a nil should be yielded.
config.lock_expiry_time = 10_000 # not required, defaults to 10_000, the unit is milliseconds
Expand Down
11 changes: 10 additions & 1 deletion lib/rails_transactional_outbox/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ class Configuration
attr_accessor :database_connection_provider, :logger, :outbox_model, :transaction_provider
attr_writer :error_handler, :transactional_outbox_worker_sleep_seconds,
:transactional_outbox_worker_idle_delay_multiplier, :outbox_batch_size, :outbox_entries_processor,
:lock_client, :lock_expiry_time, :outbox_entry_causality_key_resolver
:lock_client, :lock_expiry_time, :outbox_entry_causality_key_resolver,
:raise_not_found_model_error

def error_handler
@error_handler || RailsTransactionalOutbox::ErrorHandlers::NullErrorHandler
Expand Down Expand Up @@ -35,6 +36,14 @@ def outbox_entries_processor
@outbox_entries_processor ||= RailsTransactionalOutbox::OutboxEntriesProcessors::NonOrderedProcessor.new
end

def raise_not_found_model_error
return @raise_not_found_model_error if defined?(@raise_not_found_model_error)

true
end

alias_method :raise_not_found_model_error?, :raise_not_found_model_error

def lock_client
@lock_client || RailsTransactionalOutbox::NullLockClient
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ def applies?(record)
end

def call(record)
model = record.infer_model or raise CouldNotFindModelError.new(record)
model = record.infer_model
if model.nil?
if RailsTransactionalOutbox.configuration.raise_not_found_model_error?
raise CouldNotFindModelError.new(record)
end

return
end
model.previous_changes = record.transformed_changeset.with_indifferent_access
model.reliable_after_commit_callbacks.for_event_type(record.event_type).each do |callback|
callback.call(model)
Expand Down
2 changes: 1 addition & 1 deletion lib/rails_transactional_outbox/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
class RailsTransactionalOutbox
module Version
end
VERSION = "0.3.0"
VERSION = "0.3.1"
end
20 changes: 20 additions & 0 deletions spec/rails_transactional_outbox/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,26 @@
end
end

describe "raise_not_found_model_error" do
subject(:raise_not_found_model_error) { configuration.raise_not_found_model_error }

let(:configuration) { described_class.new }

context "when set" do
let(:custom_value) { false }

before do
configuration.raise_not_found_model_error = custom_value
end

it { is_expected.to eq custom_value }
end

context "when not set" do
it { is_expected.to be true }
end
end

describe "lock_client" do
subject(:lock_client) { configuration.lock_client }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@
let(:resource_id) { "unknown" }

it { is_expected_block.to raise_error "could not find model for outbox record: 123" }

context "with configuration set not to raise error" do
before do
RailsTransactionalOutbox.configuration.raise_not_found_model_error = false
end

it { expect { call }.not_to raise_error }
end
end
end
end

0 comments on commit 66a8133

Please sign in to comment.