Outboxer is an ActiveRecord implementation of the transactional outbox pattern for PostgreSQL and MySQL databases.
gem 'outboxer'
bundle install
bin/rails g outboxer:schema
bin/rake db:migrate
class Event < ActiveRecord::Base
# your existing model
after_create do |event|
Outboxer::Message.queue(messageable: event)
end
end
bin/rails g outboxer:publisher
Outboxer::Publisher.publish(...) do |message|
case message[:messageable_type]
when 'Event'
event = Event.find(message[:messageable_id])
# handle event here
end
end
bin/outboxer_publisher
# called through your scheduling infrastructure
# sidekiq scheduler, whenever, clockwork or a custom process
Outboxer::Messages.delete_all(
status: Outboxer::Message::PUBLISHED,
batch_size: 100,
older_than: Time.now - 60)
require 'outboxer/web'
Rails.application.routes.draw do
mount Outboxer::Web, at: '/outboxer'
end
require 'outboxer/web'
map '/outboxer' do
run Outboxer::Web
end
run bin/outboxer_publishermon
Bug reports and pull requests are welcome on GitHub at https://github.com/fast-programmer/outboxer.
This gem is available as open source under the terms of the GNU Lesser General Public License v3.0.