Outboxer
Background
Outboxer is an ActiveRecord implementation of the transactional outbox pattern for PostgreSQL and MySQL databases.
Setup
1. add gem to gemfile
gem 'outboxer'
2. install gem
bundle install
3. generate schema
bin/rails g outboxer:schema
4. migrate schema
bin/rake db:migrate
5. seed database
bin/rake outboxer:db:seed
6. generate publisher
sidekiq
bin/rails g outboxer:sidekiq_publisher
custom
bin/rails g outboxer:publisher
7. queue message after model creation
# your existing model
class Event < ActiveRecord::Base
after_create do |event|
Outboxer::Message.queue(messageable: event)
end
end
8. publish message out of band
Sidekiq
Outboxer::Publisher.publish do |message|
case message[:messageable_type]
when 'Event'
EventCreatedJob.perform_async({ 'id' => message[:messageable_id] })
end
end
Bunny
Outboxer::Publisher.publish do |message|
case message[:messageable_type]
when 'Event'
queue.publish(JSON.generate({ 'id' => message[:messageable_id] }), persistent: true)
end
end
Custom
Outboxer::Publisher.publish do |message|
case message[:messageable_type]
when 'Event'
# publish message here
logger.info "Outboxer published message #{message[:id]} for "\
"#{message[:messageable_type]}::#{message[:messageable_id]}"
end
end
9. run publisher
bin/outboxer_publisher
10. open rails console
bin/rails c
11. create event
Event.create!
12. Observe published message
Confirm the message has been published out of band
Management
Outboxer provides a sidekiq like UI to help manage your messages
Publishers
Messages
rails
config/routes.rb
require 'outboxer/web'
Rails.application.routes.draw do
mount Outboxer::Web, at: '/outboxer'
end
rack
config.ru
require 'outboxer/web'
map '/outboxer' do
run Outboxer::Web
end
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/fast-programmer/outboxer.
License
This gem is available as open source under the terms of the GNU Lesser General Public License v3.0.