# Outboxer Creating a message in an SQL database and publishing it to redis via a sidekiq worker are two operations that cannot be combined into a single atomic operation. If either database fails, inconsistencies can occur. In Outboxer, when a new message is created in your application's SQL database, Outboxer automatically creates a message in an outbox table with a status of 'unpublished', within the same local transaction. This ensures that either both operations succeed or both fail, preserving atomicity. ## Installation 1. Add the Outboxer gem to your application's Gemfile: ```ruby gem 'outboxer' ``` 2. Install the Outboxer gem: ```bash bundle install ``` 3. Generate the migration and publisher files ```bash bin/rails generate outboxer:install ``` ## Usage ### 1. Migrate your database ```bash bin/rake db:migrate ``` ### 2. Include Outboxer into existing model First, include `Outboxer::Outboxable` into your existing `Message` model: ```ruby class Message < ApplicationRecord include Outboxer::Outboxable end ``` ### 3. Update the publish block By default, the `bin/publisher` script does not do anything with the message in its block. To customize this behavior, you should update the block in the `bin/publisher` file: ```ruby Outboxer::Publisher.publish do |message:, logger:| Worker.perform_async({ message_id: message.id }) end ``` ### 3. Run the Publisher To start publishing messages, run the `bin/publisher` script: ```bash bin/publisher ``` ## Contributing Bug reports and pull requests are welcome on GitHub at https://github.com/fast-programmer/outboxer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/fast-programmer/outboxer/blob/main/CODE_OF_CONDUCT.md). ## License The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). ## Code of Conduct Everyone interacting in the Outboxer project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/fast-programmer/outboxer/blob/main/CODE_OF_CONDUCT.md).