/outboxer

Transactional outbox for MySQL and PostgreSQL databases

Primary LanguageRubyMIT LicenseMIT

Outboxer

Gem Version Ruby

Background

Outboxer is an ActiveRecord implementation of the transactional outbox pattern for PostgreSQL and MySQL databases.

Installation

1. add gem to gemfile

gem 'outboxer'

2. install gem

bundle install

Usage

1. generate schema

bin/rails g outboxer:schema

2. migrate schema

bin/rake db:migrate

3. queue message after event created

class Event < ActiveRecord::Base
  # your existing model

  after_create do |event|
    Outboxer::Message.queue(messageable: event)
  end
end

4. generate publisher

bin/rails g outboxer:publisher

5. handle event created out of band

Outboxer::Publisher.publish(...) do |message|
  case message[:messageable_type]
  when 'Event'
    event = Event.find(message[:messageable_id])

    # handle event here
  end
end

6. run publisher

bin/outboxer_publisher

7. periodically delete published messages

# 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)

8. manage messages via ui

Screenshot 2024-05-20 at 8 47 57 pm

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

9. monitor message publisher

Screenshot 2024-05-20 at 10 41 57 pm
run bin/outboxer_publishermon

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.