Skip to content

Commit

Permalink
remove application interface bangs
Browse files Browse the repository at this point in the history
  • Loading branch information
bedrock-adam committed Apr 6, 2024
1 parent a881f06 commit dc12cba
Show file tree
Hide file tree
Showing 21 changed files with 86 additions and 84 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,16 @@ bin/rails g outboxer:schema
bin/rake db:migrate
```

### create an outboxer message in the same transaction as the event record
### when an event is created, backlog an outboxer message (in same transaction)

```ruby
class Event < ActiveRecord::Base
# ...

after_create do |event|
Outboxer::Message.backlog!(messageable_type: event.class.name, messageable_id: event.id)
Outboxer::Message.backlog(
messageable_type: event.class.name,
messageable_id: event.id)
end
end
```
Expand Down Expand Up @@ -73,10 +75,10 @@ bin/rails g outboxer:sidekiq_publisher
### update the publish block to add an event created job

```ruby
Outboxer::Publisher.publish! do |outboxer_message|
case outboxer_message.messageable_type
Outboxer::Publisher.publish do |outboxer_message|
case outboxer_message['messageable_type']
when 'Event'
EventCreatedJob.perform_async({ 'id' => outboxer_message.messageable_id })
EventCreatedJob.perform_async({ 'id' => outboxer_message['messageable_id'] })
end
end
```
Expand Down
4 changes: 2 additions & 2 deletions lib/outboxer/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def config(environment: ENV['RAILS_ENV'] || 'development', path: 'config/databas
YAML.safe_load(db_config_erb_result, aliases: true)[environment]
end

def connect!(config:, logger: nil)
def connect(config:, logger: nil)
ActiveRecord::Base.logger = logger if logger

ActiveRecord::Base.establish_connection(config)
Expand All @@ -33,7 +33,7 @@ def connected?

class DisconnectError < Error; end

def disconnect!
def disconnect
ActiveRecord::Base.connection_handler.clear_active_connections!
ActiveRecord::Base.connection_handler.connection_pool_list.each(&:disconnect!)
rescue => error
Expand Down
30 changes: 15 additions & 15 deletions lib/outboxer/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Error < Outboxer::Error; end;
class NotFound < Error; end
class InvalidTransition < Error; end

def backlog!(messageable_type:, messageable_id:)
def backlog(messageable_type:, messageable_id:)
ActiveRecord::Base.connection_pool.with_connection do
ActiveRecord::Base.transaction do
message = Models::Message.create!(
Expand All @@ -22,7 +22,7 @@ def backlog!(messageable_type:, messageable_id:)
end
end

def find_by_id!(id:)
def find_by_id(id:)
ActiveRecord::Base.connection_pool.with_connection do
ActiveRecord::Base.transaction do
message = Models::Message.includes(exceptions: :frames).find_by!(id: id)
Expand Down Expand Up @@ -55,7 +55,7 @@ def find_by_id!(id:)
raise NotFound, "Couldn't find Outboxer::Models::Message with id #{id}"
end

def publishing!(id:)
def publishing(id:)
ActiveRecord::Base.connection_pool.with_connection do
ActiveRecord::Base.transaction do
message = Models::Message.lock.find_by!(id: id)
Expand All @@ -79,7 +79,7 @@ def publishing!(id:)
end


def published!(id:)
def published(id:)
ActiveRecord::Base.connection_pool.with_connection do
ActiveRecord::Base.transaction do
message = Models::Message.lock.find_by!(id: id)
Expand All @@ -99,7 +99,7 @@ def published!(id:)
end
end

def failed!(id:, exception:)
def failed(id:, exception:)
ActiveRecord::Base.connection_pool.with_connection do
ActiveRecord::Base.transaction do
message = Models::Message.order(created_at: :asc).lock.find_by!(id: id)
Expand All @@ -124,7 +124,7 @@ def failed!(id:, exception:)
end
end

def delete!(id:)
def delete(id:)
ActiveRecord::Base.connection_pool.with_connection do
ActiveRecord::Base.transaction do
message = Models::Message.includes(exceptions: :frames).lock.find_by!(id: id)
Expand All @@ -138,7 +138,7 @@ def delete!(id:)
end
end

def republish!(id:)
def republish(id:)
ActiveRecord::Base.connection_pool.with_connection do
ActiveRecord::Base.transaction do
message = Models::Message.lock.find_by!(id: id)
Expand All @@ -156,14 +156,14 @@ def republish!(id:)
end
end

def stop_publishing!
def stop_publishing
@publishing = false
end

def publish!(threads: 5, queue: 10, poll: 1,
def publish(threads: 5, queue: 10, poll: 1,
logger: Logger.new($stdout, level: ::Logger::INFO),
kernel: Kernel, &block)
Database.connect!(config: Database.config, logger: logger) unless Database.connected?
Database.connect(config: Database.config, logger: logger) unless Database.connected?

ruby_queue = Queue.new

Expand All @@ -184,19 +184,19 @@ def publish!(threads: 5, queue: 10, poll: 1,
end

logger.info "Publishing message (id: #{message['id']}) }"
message = Message.publishing!(id: message['id'])
message = Message.publishing(id: message['id'])

begin
block.call(message)
rescue Exception => exception
logger.error "Failed to publish message { id: #{message['id']}, error: #{exception} }"
Message.failed!(id: message['id'], exception: exception)
Message.failed(id: message['id'], exception: exception)

raise
end

logger.info "Published message { id: #{message['id']} }"
Message.published!(id: message['id'])
Message.published(id: message['id'])
rescue => exception
logger.error "#{exception.class}: #{exception.message}"
rescue Exception => exception
Expand All @@ -221,7 +221,7 @@ def publish!(threads: 5, queue: 10, poll: 1,
messages = []

queue_remaining = queue - ruby_queue.length
messages = (queue_remaining > 0) ? Messages.queue!(limit: queue_remaining) : []
messages = (queue_remaining > 0) ? Messages.queue(limit: queue_remaining) : []
messages.each { |message| ruby_queue.push({ 'id' => message['id'] }) }

kernel.sleep(poll) if messages.empty? || (ruby_queue.length >= queue)
Expand All @@ -241,7 +241,7 @@ def publish!(threads: 5, queue: 10, poll: 1,

logger.info "Stopped publishing queued messages"

Database.disconnect!
Database.disconnect

logger.info "Shut down gracefully"
end
Expand Down
10 changes: 5 additions & 5 deletions lib/outboxer/messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def counts_by_status
end
end

def queue!(limit: 1)
def queue(limit: 1)
ActiveRecord::Base.connection_pool.with_connection do
ActiveRecord::Base.transaction do
ids = Models::Message
Expand Down Expand Up @@ -99,7 +99,7 @@ def list(status: nil, sort: :updated_at, order: :asc, page: 1, per_page: 100)
end
end

def republish_all!(batch_size: 100)
def republish_all(batch_size: 100)
updated_total_count = 0

ActiveRecord::Base.connection_pool.with_connection do
Expand Down Expand Up @@ -130,7 +130,7 @@ def republish_all!(batch_size: 100)
{ 'count' => updated_total_count }
end

def republish_selected!(ids:)
def republish_selected(ids:)
updated_count = 0

ActiveRecord::Base.connection_pool.with_connection do
Expand All @@ -154,7 +154,7 @@ def republish_selected!(ids:)
{ 'count' => updated_count }
end

def delete_all!(batch_size: 100)
def delete_all(batch_size: 100)
deleted_total_count = 0

ActiveRecord::Base.connection_pool.with_connection do
Expand Down Expand Up @@ -184,7 +184,7 @@ def delete_all!(batch_size: 100)
{ 'count' => deleted_total_count }
end

def delete_selected!(ids:)
def delete_selected(ids:)
deleted_count = 0

ActiveRecord::Base.connection_pool.with_connection do
Expand Down
14 changes: 7 additions & 7 deletions spec/lib/outboxer/database/connect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

module Outboxer
RSpec.describe Database do
describe '.connect!' do
before(:each) { Database.disconnect! }
describe '.connect' do
before(:each) { Database.disconnect }

after(:all) do
config = Database.config(environment: 'test')
Database.connect!(config: config)
Database.connect(config: config)
end

context 'when db config not valid' do
Expand All @@ -23,13 +23,13 @@ module Outboxer

it 'establishes a connection without errors' do
expect do
Database.connect!(config: config)
Database.connect(config: config)
end.to raise_error(Database::ConnectError)
end

it 'does not connect to the database' do
begin
Database.connect!(config: config)
Database.connect(config: config)
rescue Database::ConnectError
# ignore
end
Expand All @@ -42,11 +42,11 @@ module Outboxer
let(:config) { Database.config(environment: 'test') }

it 'establishes a connection without errors' do
expect { Database.connect!(config: config) }.not_to raise_error
expect { Database.connect(config: config) }.not_to raise_error
end

it 'actually connects to the database' do
Database.connect!(config: config)
Database.connect(config: config)

expect(Database.connected?).to be true
end
Expand Down
8 changes: 4 additions & 4 deletions spec/lib/outboxer/database/disconnect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

module Outboxer
RSpec.describe Database do
describe '.disconnect!' do
describe '.disconnect' do
context 'when successful' do
it 'does not raise an error' do
expect { Database.disconnect! }.not_to raise_error
expect { Database.disconnect }.not_to raise_error
end

it 'returns connected false' do
Database.disconnect!
Database.disconnect

expect(Database.connected?).to be false
end
Expand All @@ -22,7 +22,7 @@ module Outboxer
end

it 'raises a DisconnectError' do
expect { Database.disconnect! }.to raise_error(Database::DisconnectError)
expect { Database.disconnect }.to raise_error(Database::DisconnectError)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/outboxer/message/backlog_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

module Outboxer
RSpec.describe Message do
describe '.backlog!' do
describe '.backlog' do
let!(:backlogged_message) do
Message.backlog!(messageable_type: 'Event', messageable_id: '1')
Message.backlog(messageable_type: 'Event', messageable_id: '1')
end

it 'returns backlogged message' do
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/outboxer/message/delete_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Outboxer
let!(:exception) { create(:outboxer_exception, message: message) }
let!(:frame) { create(:outboxer_frame, exception: exception) }

let!(:result) { Message.delete!(id: message.id) }
let!(:result) { Message.delete(id: message.id) }

it 'deletes the message' do
expect(Models::Message).not_to exist(message.id)
Expand Down
8 changes: 4 additions & 4 deletions spec/lib/outboxer/message/failed_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Outboxer
RSpec.describe Message do
describe '.failed!' do
describe '.failed' do
let(:exception) do
def raise_exception
begin
Expand All @@ -18,7 +18,7 @@ def raise_exception
context 'when published message' do
let!(:publishing_message) { create(:outboxer_message, :publishing) }

let!(:failed_message) { Message.failed!(id: publishing_message.id, exception: exception) }
let!(:failed_message) { Message.failed(id: publishing_message.id, exception: exception) }

it 'returns updated message' do
expect(failed_message['id']).to eq(publishing_message.id)
Expand Down Expand Up @@ -54,7 +54,7 @@ def raise_exception

it 'raises invalid transition error' do
expect do
Message.failed!(id: backlogged_message.id, exception: exception)
Message.failed(id: backlogged_message.id, exception: exception)
end.to raise_error(
Message::InvalidTransition,
"cannot transition outboxer message #{backlogged_message.id} " +
Expand All @@ -63,7 +63,7 @@ def raise_exception

it 'does not delete backlogged message' do
begin
Message.failed!(id: backlogged_message.id, exception: exception)
Message.failed(id: backlogged_message.id, exception: exception)
rescue Message::InvalidTransition
# ignore
end
Expand Down
6 changes: 3 additions & 3 deletions spec/lib/outboxer/message/find_by_id_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

module Outboxer
RSpec.describe Message do
describe '.find_by_id!' do
describe '.find_by_id' do
context 'when a failed message exists' do
let!(:message) { create(:outboxer_message, :failed) }
let!(:exception) { create(:outboxer_exception, message: message) }
let!(:frame) { create(:outboxer_frame, exception: exception) }

it 'returns the message, exceptions and frames' do
result = Message.find_by_id!(id: message.id)
result = Message.find_by_id(id: message.id)

expect(result['id']).to eq(message.id)
expect(result['status']).to eq('failed')
Expand All @@ -26,7 +26,7 @@ module Outboxer

context 'when the message does not exist' do
it 'raises a NotFound error' do
expect { Message.find_by_id!(id: -1) }.to raise_error(Message::NotFound)
expect { Message.find_by_id(id: -1) }.to raise_error(Message::NotFound)
end
end
end
Expand Down
Loading

0 comments on commit dc12cba

Please sign in to comment.