Skip to content

Commit

Permalink
outbocer event rename to outboxer message
Browse files Browse the repository at this point in the history
  • Loading branch information
bedrock-adam committed Dec 4, 2023
1 parent b935867 commit f351c52
Show file tree
Hide file tree
Showing 14 changed files with 244 additions and 232 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ As these operations span multiple database types (_SQL_ and _Redis_) however, th

Outboxer is an `ActiveRecord` implementation of the [transactional outbox pattern](https://microservices.io/patterns/data/transactional-outbox.html): a well established solution to this problem.

By creating an outboxer event in the same transaction as an event, we can guarantee the event is published out to another system _eventually_, even if there are failures.
By creating an outboxer message in the same transaction as an event, we can guarantee the event is published out to another system _eventually_, even if there are failures.

### Getting started

Expand Down Expand Up @@ -78,7 +78,7 @@ bin/rake db:migrate

```ruby
class Event < ActiveRecord::Base
self.inheritance_column = :_type_disabled
self.inheritance_column = nil

attribute :created_at, :datetime, default: -> { Time.current }

Expand Down Expand Up @@ -125,18 +125,18 @@ bin/rails generate outboxer:install
bin/rake db:migrate
```

##### c. associate outboxer event with event
##### c. associate outboxer message with event

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

has_one :outboxer_event,
class_name: 'Outboxer::Models::Event',
as: :outboxer_eventable,
has_one :outboxer_message,
class_name: 'Outboxer::Models::Message',
as: :outboxer_messageable,
dependent: :destroy

after_create -> { create_outboxer_event! }
after_create -> { create_outboxer_message! }
end
```

Expand All @@ -146,22 +146,22 @@ end

```ruby
loop do
Outboxer::Event.publish! do |outboxer_eventable|
EventHandlerWorker.perform_async({ 'event' => { 'id' => outboxer_eventable.id } })
Outboxer::Message.publish! do |outboxer_messageable|
EventHandlerWorker.perform_async({ 'id' => outboxer_messageable.id })
end
end
```

##### b. run the event publisher
##### b. run the outboxer publisher

```bash
bin/event_publisher
bin/outboxer_publisher
```


## Implementation

To see all the parts working together in a single place, check out the [publisher_spec.rb](https://github.com/fast-programmer/outboxer/blob/master/spec/outboxer/event_spec.rb)
To see all the parts working together in a single place, check out the [publisher_spec.rb](https://github.com/fast-programmer/outboxer/blob/master/spec/outboxer/message_spec.rb)


## Motivation
Expand Down
8 changes: 4 additions & 4 deletions generators/outboxer/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ def self.next_migration_number(dirname)
end

def copy_bin_file
template "bin/event_publisher.rb", "bin/event_publisher"
run "chmod +x bin/event_publisher"
template "bin/outboxer_publisher.rb", "bin/event_publisher"
run "chmod +x bin/outboxer_publisher"
end

def copy_migrations
migration_template(
"migrations/create_outboxer_events.rb",
"db/migrate/create_outboxer_events.rb")
"migrations/create_outboxer_messages.rb",
"db/migrate/create_outboxer_messages.rb")

migration_template(
"migrations/create_outboxer_exceptions.rb",
Expand Down
23 changes: 0 additions & 23 deletions generators/outboxer/templates/bin/event_publisher.rb

This file was deleted.

32 changes: 32 additions & 0 deletions generators/outboxer/templates/bin/message_publisher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env ruby

require_relative '../config/environment'

logger = Logger.new($stdout)
logger.level = Logger::INFO

interrupted = false

trap("INT") do
interrupted = true

logger.warn "\nCTRL+C detected. Preparing to exit gracefully..."
end

while !interrupted
begin
Outboxer::Message.publish! do |outboxer_messageable|
case outboxer_messageable.type
when 'Event'
EventHandlerWorker.perform_async({ 'id' => outboxer_messageable.id })
end
end
rescue => Outboxer::Message::NotFound
sleep 1
rescue => exception
logger.error("Exception raised: #{exception.class}: #{exception.message}\n" \
"#{exception.backtrace.join("\n")}")
end
end

logger.info "Exiting..."
13 changes: 0 additions & 13 deletions generators/outboxer/templates/migrations/create_outboxer_events.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class CreateOutboxerExceptions < ActiveRecord::Migration[6.1]
def change
create_table :outboxer_exceptions do |t|
t.references :outboxer_event, null: false, foreign_key: { to_table: :outboxer_events }
t.references :outboxer_message, null: false, foreign_key: { to_table: :outboxer_messages }

t.text :class_name, null: false
t.text :message_text, null: false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CreateOutboxerMessages < ActiveRecord::Migration[6.1]
def change
create_table :outboxer_messages do |t|
t.text :status, null: false

t.references :outboxer_messageable, polymorphic: true, null: false

t.timestamps
end

add_index :outboxer_messages, %i[status created_at]
end
end
2 changes: 1 addition & 1 deletion lib/outboxer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

require_relative "outboxer/models"

require_relative "outboxer/event"
require_relative "outboxer/message"

module Outboxer
end
127 changes: 0 additions & 127 deletions lib/outboxer/event.rb

This file was deleted.

Loading

0 comments on commit f351c52

Please sign in to comment.