outboxable 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 974efa7874e38a5ca48f6c91b58fbb0b1d8b7508135ecd50dd1e230ad8c9d508
4
+ data.tar.gz: 565c240d3c10c0abd3ffe191af9d189e3f0fcaf95e66dffc17ff3f861cbb87c3
5
+ SHA512:
6
+ metadata.gz: 1f1f87256f1f331ec2d16a1b42453a57f58498894f965d8e7a996266b1ac8749fc304126c3f5ba71d75a89a03e0d2864b2e0216ade977c157db61abf77cb0855
7
+ data.tar.gz: ad131708cecf7baf24a520058118f92bccc835d3a9f7c8874838c43c95d4a0bd88ea5e26932a961e484cb31b4bd79b37972fcceb7794e0b172e30cee67ae5df6
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,113 @@
1
+ require: rubocop-rails
2
+
3
+ AllCops:
4
+ NewCops: enable
5
+ SuggestExtensions: false
6
+ Layout/SpaceBeforeBrackets: # (new in 1.7)
7
+ Enabled: true
8
+ Layout/LineLength:
9
+ Max: 350
10
+ Lint/AmbiguousAssignment: # (new in 1.7)
11
+ Enabled: true
12
+ Lint/DeprecatedConstants: # (new in 1.8)
13
+ Enabled: true
14
+ Lint/DuplicateBranch: # (new in 1.3)
15
+ Enabled: true
16
+ Lint/DuplicateRegexpCharacterClassElement: # (new in 1.1)
17
+ Enabled: true
18
+ Lint/EmptyBlock: # (new in 1.1)
19
+ Enabled: true
20
+ Lint/EmptyClass: # (new in 1.3)
21
+ Enabled: true
22
+ Lint/LambdaWithoutLiteralBlock: # (new in 1.8)
23
+ Enabled: true
24
+ Lint/NoReturnInBeginEndBlocks: # (new in 1.2)
25
+ Enabled: true
26
+ Lint/NumberedParameterAssignment: # (new in 1.9)
27
+ Enabled: true
28
+ Lint/OrAssignmentToConstant: # (new in 1.9)
29
+ Enabled: true
30
+ Lint/RedundantDirGlobSort: # (new in 1.8)
31
+ Enabled: true
32
+ Lint/SymbolConversion: # (new in 1.9)
33
+ Enabled: true
34
+ Lint/ToEnumArguments: # (new in 1.1)
35
+ Enabled: true
36
+ Lint/TripleQuotes: # (new in 1.9)
37
+ Enabled: true
38
+ Lint/UnexpectedBlockArity: # (new in 1.5)
39
+ Enabled: true
40
+ Lint/UnmodifiedReduceAccumulator: # (new in 1.1)
41
+ Enabled: true
42
+ Style/ArgumentsForwarding: # (new in 1.1)
43
+ Enabled: true
44
+ Style/CollectionCompact: # (new in 1.2)
45
+ Enabled: true
46
+ Style/DocumentDynamicEvalDefinition: # (new in 1.1)
47
+ Enabled: true
48
+ Style/Documentation:
49
+ Enabled: false
50
+ Style/FrozenStringLiteralComment:
51
+ Enabled: false
52
+ Style/EndlessMethod: # (new in 1.8)
53
+ Enabled: true
54
+ Style/HashConversion: # (new in 1.10)
55
+ Enabled: true
56
+ Style/HashExcept: # (new in 1.7)
57
+ Enabled: true
58
+ Style/IfWithBooleanLiteralBranches: # (new in 1.9)
59
+ Enabled: true
60
+ Style/NegatedIfElseCondition: # (new in 1.2)
61
+ Enabled: true
62
+ Style/NilLambda: # (new in 1.3)
63
+ Enabled: true
64
+ Style/RedundantArgument: # (new in 1.4)
65
+ Enabled: true
66
+ Style/SwapValues: # (new in 1.1)
67
+ Enabled: true
68
+ Rails/ActiveRecordCallbacksOrder: # (new in 2.7)
69
+ Enabled: true
70
+ Rails/AfterCommitOverride: # (new in 2.8)
71
+ Enabled: true
72
+ Rails/AttributeDefaultBlockValue: # (new in 2.9)
73
+ Enabled: true
74
+ Rails/FindById: # (new in 2.7)
75
+ Enabled: true
76
+ Rails/Inquiry: # (new in 2.7)
77
+ Enabled: true
78
+ Rails/MailerName: # (new in 2.7)
79
+ Enabled: true
80
+ Rails/MatchRoute: # (new in 2.7)
81
+ Enabled: true
82
+ Rails/NegateInclude: # (new in 2.7)
83
+ Enabled: true
84
+ Rails/Pluck: # (new in 2.7)
85
+ Enabled: true
86
+ Rails/PluckInWhere: # (new in 2.7)
87
+ Enabled: true
88
+ Rails/RenderInline: # (new in 2.7)
89
+ Enabled: true
90
+ Rails/RenderPlainText: # (new in 2.7)
91
+ Enabled: true
92
+ Rails/ShortI18n: # (new in 2.7)
93
+ Enabled: true
94
+ Rails/SquishedSQLHeredocs: # (new in 2.8)
95
+ Enabled: true
96
+ Rails/UniqueValidationWithoutIndex:
97
+ Enabled: false
98
+ Rails/WhereEquals: # (new in 2.9)
99
+ Enabled: true
100
+ Rails/WhereExists: # (new in 2.7)
101
+ Enabled: true
102
+ Rails/WhereNot: # (new in 2.8)
103
+ Enabled: true
104
+ Metrics/BlockLength:
105
+ Enabled: false
106
+ Metrics/AbcSize:
107
+ Enabled: false
108
+ Metrics/MethodLength:
109
+ Enabled: false
110
+ Metrics/CyclomaticComplexity:
111
+ Max: 15
112
+ Metrics/PerceivedComplexity:
113
+ Max: 15
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-02-26
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at broosk.edogawa@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in outboxable.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.21"
data/Gemfile.lock ADDED
@@ -0,0 +1,69 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ outboxable (0.1.0)
5
+ bunny (>= 2.19.0)
6
+ connection_pool (~> 2.3.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ amq-protocol (2.3.2)
12
+ ast (2.4.2)
13
+ bunny (2.20.3)
14
+ amq-protocol (~> 2.3, >= 2.3.1)
15
+ sorted_set (~> 1, >= 1.0.2)
16
+ connection_pool (2.3.0)
17
+ diff-lcs (1.5.0)
18
+ json (2.6.3)
19
+ parallel (1.22.1)
20
+ parser (3.2.1.0)
21
+ ast (~> 2.4.1)
22
+ rainbow (3.1.1)
23
+ rake (13.0.6)
24
+ rbtree (0.4.6)
25
+ regexp_parser (2.7.0)
26
+ rexml (3.2.5)
27
+ rspec (3.12.0)
28
+ rspec-core (~> 3.12.0)
29
+ rspec-expectations (~> 3.12.0)
30
+ rspec-mocks (~> 3.12.0)
31
+ rspec-core (3.12.1)
32
+ rspec-support (~> 3.12.0)
33
+ rspec-expectations (3.12.2)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.12.0)
36
+ rspec-mocks (3.12.3)
37
+ diff-lcs (>= 1.2.0, < 2.0)
38
+ rspec-support (~> 3.12.0)
39
+ rspec-support (3.12.0)
40
+ rubocop (1.45.1)
41
+ json (~> 2.3)
42
+ parallel (~> 1.10)
43
+ parser (>= 3.2.0.0)
44
+ rainbow (>= 2.2.2, < 4.0)
45
+ regexp_parser (>= 1.8, < 3.0)
46
+ rexml (>= 3.2.5, < 4.0)
47
+ rubocop-ast (>= 1.24.1, < 2.0)
48
+ ruby-progressbar (~> 1.7)
49
+ unicode-display_width (>= 2.4.0, < 3.0)
50
+ rubocop-ast (1.26.0)
51
+ parser (>= 3.2.1.0)
52
+ ruby-progressbar (1.11.0)
53
+ set (1.0.3)
54
+ sorted_set (1.0.3)
55
+ rbtree
56
+ set (~> 1.0)
57
+ unicode-display_width (2.4.2)
58
+
59
+ PLATFORMS
60
+ x86_64-linux
61
+
62
+ DEPENDENCIES
63
+ outboxable!
64
+ rake (~> 13.0)
65
+ rspec (~> 3.0)
66
+ rubocop (~> 1.21)
67
+
68
+ BUNDLED WITH
69
+ 2.4.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Brusk Awat
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,227 @@
1
+ # Outboxable
2
+
3
+ The Outboxable Gem is tailored for Rails applications to implement the transactional outbox pattern. It currently only supports ActiveRecord.
4
+
5
+ Please take into consideration that this Gem is **opinionated**, meaning it expects you to follow a certain pattern and specific setting. If you don't like it, you can always fork it and change it.
6
+
7
+ ### Restrictions
8
+
9
+ 1. When using RabbitMQ, it only publishes events to a ***topic*** exchange.
10
+ 1. It assumes that you are using routing keys to publish to the topic exchange.
11
+ 1. It publishes events in a background job using [Sidekiq](https://github.com/sidekiq/sidekiq). Therefore, you application must use Sidekiq.
12
+ 1. It implements the [polling publisher pattern](https://microservices.io/patterns/data/polling-publisher.html). For that, it uses [](https://github.com/sidekiq-cron/sidekiq-cron) to check the unpublished outboxes every 5 seconds after the initialization of the application.
13
+
14
+ ## Installation
15
+
16
+ Install the gem and add to the application's Gemfile by executing:
17
+
18
+ ```ruby
19
+ $ bundle add outboxable
20
+ ```
21
+
22
+ If bundler is not being used to manage dependencies, install the gem by executing:
23
+
24
+ ```ruby
25
+ $ gem install outboxable
26
+ ```
27
+
28
+ Then run:
29
+
30
+ ```shell
31
+ $ rails g outboxable:install
32
+ ```
33
+
34
+ The command above will add a migration file and the Outbox model. You will need then to run the migrations:
35
+
36
+ ```shell
37
+ $ rails db:migrate
38
+ ```
39
+
40
+ ## Usage
41
+
42
+ The installation command above will also add a configuration file to your initializer:
43
+
44
+ ```ruby
45
+ # This monkey patch allows you to customize the message format that you publish to your broker.
46
+ # By default, Outboxable publishes a CloudEvent message to your broker.
47
+ module Outboxable
48
+ module RabbitMq
49
+ class Publisher
50
+ # Override this method to customize the message format that you publish to your broker
51
+ # DO NOT CHANGE THE METHOD SIGNATURE
52
+ def to_envelope(resource:)
53
+ {
54
+ id: resource.id,
55
+ source: 'http://localhost:3000',
56
+ specversion: '1.0',
57
+ type: resource.routing_key,
58
+ datacontenttype: 'application/json',
59
+ data: @resource.payload
60
+ }.to_json
61
+ end
62
+ end
63
+ end
64
+ end
65
+
66
+ Outboxable.configure do |config|
67
+ # Specify the ORM you are using. For now, only ActiveRecord is supported.
68
+ config.orm = :activerecord
69
+
70
+ # Specify the message broker you are using. For now, only RabbitMQ is supported.
71
+ config.message_broker = :rabbitmq
72
+
73
+ # RabbitMQ configurations
74
+ config.rabbitmq_host = ENV.fetch('RABBITMQ__HOST')
75
+ config.rabbitmq_port = ENV.fetch('RABBITMQ__PORT', 5672)
76
+ config.rabbitmq_user = ENV.fetch('RABBITMQ__USER')
77
+ config.rabbitmq_password = ENV.fetch('RABBITMQ__PASSWORD')
78
+ config.rabbitmq_vhost = ENV.fetch('RABBITMQ__VHOST')
79
+ config.rabbitmq_event_bus_exchange = ENV.fetch('EVENTBUS__EXCHANGE_NAME')
80
+ end
81
+ ```
82
+
83
+ The monkey patch in the code above is crucial in giving you a way to customize the format of the message that you will publish to the message broker. Be default, it follows the specs of the [Cloud Native Events Specifications v1.0.2](https://github.com/cloudevents/spec/blob/v1.0.2/cloudevents/spec.md).
84
+
85
+
86
+
87
+ ### General use-cases
88
+
89
+ The Outboxable Gem provides a general-purpose polymorphic model for creating outboxes. In order to maintain a transactional outbox, the changes that occur to a resource and the associated outbox must happen within the same transaction:
90
+
91
+ ```ruby
92
+ ActiveRecord::Base.transaction do
93
+ book = Book.find(1)
94
+ book.update!(title: 'Patterns of Enterprise Application Architecture', author: 'Martin Fowler')
95
+ Outbox.create!(
96
+ routing_key: 'books.updated',
97
+ exchange: 'YOUR RABBITMQ EXCHANGE',
98
+ payload: book.as_json,
99
+ outboxable: book
100
+ )
101
+ end
102
+
103
+ ```
104
+
105
+
106
+ If you are intending to create an outbox that is not necessarily associated with a resource, you can use the following configuration that sets the ``size`` attribute to ``:batch``:
107
+
108
+ ```ruby
109
+ ActiveRecord::Base.transaction do
110
+ book = Book.find(1)
111
+ book.update!(status: :published)
112
+ Outbox.create!(
113
+ routing_key: 'notifications.publish',
114
+ exchange: 'YOUR EXCHANGE',
115
+ payload: {
116
+ channels: ['sms', 'email'],
117
+ message: "Hello, world!"
118
+ },
119
+ size: :batch
120
+ )
121
+ end
122
+ ```
123
+
124
+
125
+
126
+ ### The Outboxable Concern
127
+
128
+ The Outboxable Gem provides an ActiveRecord Model Concern that you can reuse in your models, which in turn will take care of transactionally creating events in case of create or update of the resource. All you have to do is to ``include Outboxable`` in your model and implement a method by the name of ``outbox_configurations``:
129
+
130
+
131
+
132
+ ```ruby
133
+ class Book < ApplicationRecord
134
+ include Outboxable
135
+
136
+ # Enums
137
+ enum status: { draft: 0, published: 1 }
138
+
139
+ def outbox_configurations
140
+ @outbox_configurations ||= {
141
+ base: {
142
+ routing_key: 'books'
143
+ },
144
+ run_on: {
145
+ create: {},
146
+ update: {
147
+ condition: proc { |book| book.published? },
148
+ routing_key: 'books.published'
149
+ }
150
+ }
151
+ }
152
+ end
153
+ end
154
+ ```
155
+
156
+
157
+
158
+ The ``outbox_configurations`` method will be called and used by the Outboxable Gem to transactionally create an outbox and publish. In the code above, it will create an outbox when the book is created. For that purpose it will use the routing key ``books.created`` as a convention. It will also publish an event if the book is updated, using the routing key: ``books.published`` since it was specified in the hash.
159
+
160
+
161
+
162
+ Here's the schema of what could be passed to the ``outbox_configurations`` in JSON Schema format:
163
+
164
+ ```json
165
+ {
166
+ "type": "object",
167
+ "properties": {
168
+ "base": {
169
+ "type": "object",
170
+ "properties": {
171
+ "routing_key": { "type": "string" }
172
+ }
173
+ },
174
+ "run_on": {
175
+ "type": "object",
176
+ "properties": {
177
+ "create": {
178
+ "type": "object",
179
+ "properties": {
180
+ "condition": { "type": "Ruby Proc" },
181
+ "routing_key": { "type": "string" }
182
+ }
183
+ },
184
+ "update": {
185
+ "type": "object",
186
+ "properties": {
187
+ "condition": { "type": "Ruby Proc" },
188
+ "routing_key": { "type": "string" }
189
+ }
190
+ }
191
+ }
192
+ }
193
+ }
194
+ }
195
+ ```
196
+
197
+
198
+
199
+ The ``run_on`` key represents another hash that can have the keys ``create`` and ``update``. If one of these keys are not supplied, the outbox will not be created for the unspecified operation; in other words, if you do not specify the configuration for ``update``, for example, an outbox will NOT be created when the book is updated.
200
+
201
+ Each operation key such as ``create`` and ``update`` can also take a ``condition`` key, which represents a Ruby proc that must return a boolean expression. It can also take a ``routing_key`` option, which specifies that routing key with which the outbox will publish the event to the message broker. If you don't specify the ``routing_key``, it will use the base`s routing key dotted by``created`` for create operation and ``updated`` for update operation.
202
+
203
+
204
+
205
+ Last but not least, run sidekiq so that the Outboxable Gem can publish the events to the broker:
206
+
207
+ ```shell
208
+ $ bundle exec sidekiq
209
+ ```
210
+
211
+ ## Development
212
+
213
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
214
+
215
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
216
+
217
+ ## Contributing
218
+
219
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/outboxable. 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/[USERNAME]/outboxable/blob/master/CODE_OF_CONDUCT.md).
220
+
221
+ ## License
222
+
223
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
224
+
225
+ ## Code of Conduct
226
+
227
+ Everyone interacting in the Outboxable project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/outboxable/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,36 @@
1
+ module Outboxable
2
+ class InstallGenerator < Rails::Generators::Base
3
+ include Rails::Generators::Migration
4
+
5
+ source_root File.expand_path('../../../templates', __FILE__)
6
+
7
+ # Copy initializer into user app
8
+ def copy_initializer
9
+ copy_file('initializer.rb', 'config/initializers/outboxable.rb')
10
+ end
11
+
12
+ # Copy user information (model & Migrations) into user app
13
+ def create_user_model
14
+ target_path = "app/models/outbox.rb"
15
+ unless File.exist?(File.join(Rails.root, target_path))
16
+ template("outbox.rb", target_path)
17
+ else
18
+ say_status('skipped', "Model outbox already exists")
19
+ end
20
+ end
21
+
22
+ # Copy migrations
23
+ def copy_migrations
24
+ if self.class.migration_exists?('db/migrate', "create_outboxable_outboxes")
25
+ say_status('skipped', "Migration create_outboxable_outboxes already exists")
26
+ else
27
+ migration_template('create_outboxable_outboxes.rb', "db/migrate/create_outboxable_outboxes.rb")
28
+ end
29
+ end
30
+
31
+ # Use to assign migration time otherwise generator will error
32
+ def self.next_migration_number(dir)
33
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,57 @@
1
+ module Outboxable
2
+ class << self
3
+ attr_accessor :configuration
4
+ end
5
+
6
+ def self.configure
7
+ self.configuration ||= Configuration.new
8
+ yield(configuration)
9
+ end
10
+
11
+ class Configuration
12
+ ALLOWED_MESSAGE_BROKERS = %i[rabbitmq].freeze
13
+ ALLOWED_ORMS = %i[activerecord].freeze
14
+
15
+ attr_accessor :rabbitmq_host,
16
+ :rabbitmq_port,
17
+ :rabbitmq_user,
18
+ :rabbitmq_password,
19
+ :rabbitmq_vhost,
20
+ :rabbitmq_event_bus_exchange,
21
+ :message_broker,
22
+ :orm
23
+
24
+ def initialize
25
+ raise Error, 'Sidekiq is not available. Unfortunately, sidekiq must be available for Outboxable to work' unless Object.const_defined?('Sidekiq')
26
+ raise Error, 'Outboxable Gem only supports Rails but you application does not seem to be a Rails app' unless Object.const_defined?('Rails')
27
+ raise Error, 'Outboxable Gem only support Rails version 7 and newer' if Rails::VERSION::MAJOR < 7
28
+ raise Error, 'Outboxable Gem uses the sidekiq-cron Gem. Make sure you add it to your project' unless Object.const_defined?('Sidekiq::Cron')
29
+
30
+ # In accordance to sidekiq-cron README: https://github.com/sidekiq-cron/sidekiq-cron#under-the-hood
31
+ Sidekiq::Options[:cron_poll_interval] = 5
32
+
33
+ # Create the cron job for the polling publisher
34
+ Sidekiq::Cron::Job.create(name: 'OutboxablePollingPublisher', cron: '*/5 * * * * *', class: 'Outboxable::PollingPublisherWorker')
35
+ end
36
+
37
+ def message_broker=(message_broker)
38
+ raise ArgumentError, "Message broker must be one of #{ALLOWED_MESSAGE_BROKERS}" unless ALLOWED_MESSAGE_BROKERS.include?(message_broker)
39
+
40
+ @message_broker = message_broker
41
+ end
42
+
43
+ def message_broker
44
+ @message_broker || :rabbitmq
45
+ end
46
+
47
+ def orm=(orm)
48
+ raise ArgumentError, "ORM must be one of #{ALLOWED_ORMS}" unless ALLOWED_ORMS.include?(orm)
49
+
50
+ @orm = orm
51
+ end
52
+
53
+ def orm
54
+ @orm || :activerecord
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,26 @@
1
+ require 'singleton'
2
+
3
+ module Outboxable
4
+ class Connection
5
+ include ::Singleton
6
+ attr_reader :connection
7
+
8
+ def initialize
9
+ @connection = Bunny.new(
10
+ host: RabbitCarrots.configuration.rabbitmq_host,
11
+ port: RabbitCarrots.configuration.rabbitmq_port,
12
+ user: RabbitCarrots.configuration.rabbitmq_user,
13
+ password: RabbitCarrots.configuration.rabbitmq_password,
14
+ vhost: RabbitCarrots.configuration.rabbitmq_vhost
15
+ )
16
+
17
+ @connection.start
18
+ end
19
+
20
+ def channel
21
+ @channel ||= ConnectionPool.new do
22
+ connection.create_channel
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,14 @@
1
+ module Outboxable
2
+ class PollingPublisherWorker
3
+ include Sidekiq::Job
4
+ sidekiq_options queue: 'critical'
5
+
6
+ def perform
7
+ Outbox.pending.find_in_batches(batch_size: 100).each do |batch|
8
+ batch.each do |outbox|
9
+ Outboxable::Worker.perform_async(outbox.id)
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,10 @@
1
+ module Outboxable
2
+ class PublishingManager
3
+ def self.publish(resource:)
4
+ case Outboxable.configuration.message_broker
5
+ when :rabbitmq
6
+ Outboxable::RabbitMq::Publisher.new(resource:).publish
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,37 @@
1
+ module Outboxable
2
+ module RabbitMq
3
+ class Publisher
4
+ def initialize(resource:)
5
+ @resource = resource
6
+ end
7
+
8
+ def to_envelope(resource:)
9
+ # throw not implemented method error
10
+ raise NotImplementedError, "Please implement the to_envelope method in your own module"
11
+ end
12
+
13
+ def publish
14
+ confirmed = nil
15
+
16
+ Outboxable::Connection.instance.channel.with do |channel|
17
+ channel.confirm_select
18
+
19
+ # Declare a exchange
20
+ exchange = channel.topic(@resource.exchange, durable: true)
21
+
22
+ # Publish the CloudEvent resource to the exchange
23
+ exchange.publish(to_envelope(resource: @resource), routing_key: @resource.routing_key, headers: @resource.try(:headers) || {})
24
+
25
+ # Wait for confirmation
26
+ confirmed = channel.wait_for_confirms
27
+ end
28
+
29
+ return unless confirmed
30
+
31
+ @resource.reload
32
+ @resource.increment_attempt
33
+ @resource.update(status: :published, retry_at: nil)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Outboxable
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,9 @@
1
+ module Outboxable
2
+ class Worker
3
+ include Sidekiq::Job
4
+
5
+ def perform(outbox_id)
6
+ Outboxable::PublishingManager.publish(resource: Outbox.find(outbox_id))
7
+ end
8
+ end
9
+ end
data/lib/outboxable.rb ADDED
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "outboxable/version"
4
+
5
+ require 'outboxable/worker'
6
+ require 'outboxable/publishing_manager'
7
+ require 'outboxable/polling_publisher_worker'
8
+ require 'outboxable/connection'
9
+ require 'outboxable/configuration'
10
+ require 'outboxable/rabbitmq/publisher'
11
+
12
+
13
+ module Outboxable
14
+ class Error < StandardError; end
15
+
16
+ extend ActiveSupport::Concern
17
+
18
+ included do
19
+ after_create :instantiate_outbox_for_create, if: proc { |object| object.check_outbox_condition(object:, operation: :create) }
20
+ after_update :instantiate_outbox_for_update, if: proc { |object| object.check_outbox_condition(object:, operation: :update) }
21
+
22
+ has_many :outboxes, as: :outboxable, autosave: false
23
+
24
+ def instantiate_outbox(routing_key: )
25
+ outboxes.new(
26
+ routing_key:,
27
+ exchange: Outboxable.configuration.rabbitmq_event_bus_exchange,
28
+ payload: as_json
29
+ )
30
+ end
31
+
32
+ def instantiate_outbox_for_create
33
+ routing_key = outbox_configurations[:run_on][:create]&.[](:routing_key) || "#{outbox_configurations[:base][:routing_key]}.created"
34
+ instantiate_outbox(routing_key:).save!
35
+ end
36
+
37
+ def instantiate_outbox_for_update
38
+ routing_key = outbox_configurations[:run_on][:update]&.[](:routing_key) || "#{outbox_configurations[:base][:routing_key]}.updated"
39
+ instantiate_outbox(routing_key:).save!
40
+ end
41
+
42
+ def check_outbox_condition(object:, operation:)
43
+ # Check if called on create
44
+ operation_is_included = object.outbox_configurations[:run_on].keys.include?(operation)
45
+
46
+ # Check if there is a supplied condition
47
+ supplied_condition_as_proc = object.outbox_configurations[:run_on][operation][:condition]
48
+
49
+ # Return the result of operation is included if supplied condition is blank, which mean that there is no condition to be met
50
+ return operation_is_included if supplied_condition_as_proc.blank?
51
+
52
+ # Else check the condition as well as the inclusion of the operation
53
+ operation_is_included && supplied_condition_as_proc.call(self)
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,25 @@
1
+ class CreateOutboxableOutboxes < ActiveRecord::Migration[7.0]
2
+ def change
3
+ enable_extension 'pgcrypto' unless extension_enabled?('pgcrypto')
4
+
5
+ create_table :outboxes, id: :uuid, default: 'gen_random_uuid()' do |t|
6
+ t.integer :status, null: false, default: 0
7
+
8
+ t.string :exchange, null: false, default: ''
9
+ t.string :routing_key, null: false, default: ''
10
+
11
+ t.integer :attempts, null: false, default: 0
12
+ t.datetime :last_attempted_at, null: true
13
+ t.datetime :retry_at, null: true
14
+
15
+ t.jsonb :payload, default: {}
16
+ t.jsonb :headers, default: {}
17
+
18
+ t.integer :size, null: false, default: 0
19
+
20
+ t.references :outboxable, polymorphic: true, null: true
21
+
22
+ t.timestamps
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,36 @@
1
+ # This monkey patch allows you to customize the message format that you publish to your broker.
2
+ # By default, Outboxable publishes a CloudEvent message to your broker.
3
+ module Outboxable
4
+ module RabbitMq
5
+ class Publisher
6
+ # Override this method to customize the message format that you publish to your broker
7
+ # DO NOT CHANGE THE METHOD SIGNATURE
8
+ def to_envelope(resource:)
9
+ {
10
+ id: resource.id,
11
+ source: 'http://localhost:3000',
12
+ specversion: '1.0',
13
+ type: resource.routing_key,
14
+ datacontenttype: 'application/json',
15
+ data: @resource.payload
16
+ }.to_json
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ Outboxable.configure do |config|
23
+ # Specify the ORM you are using. For now, only ActiveRecord is supported.
24
+ config.orm = :activerecord
25
+
26
+ # Specify the message broker you are using. For now, only RabbitMQ is supported.
27
+ config.message_broker = :rabbitmq
28
+
29
+ # RabbitMQ configurations
30
+ config.rabbitmq_host = ENV.fetch('RABBITMQ__HOST')
31
+ config.rabbitmq_port = ENV.fetch('RABBITMQ__PORT', 5672)
32
+ config.rabbitmq_user = ENV.fetch('RABBITMQ__USER')
33
+ config.rabbitmq_password = ENV.fetch('RABBITMQ__PASSWORD')
34
+ config.rabbitmq_vhost = ENV.fetch('RABBITMQ__VHOST')
35
+ config.rabbitmq_event_bus_exchange = ENV.fetch('EVENTBUS__EXCHANGE_NAME')
36
+ end
@@ -0,0 +1,32 @@
1
+ class Outbox < ApplicationRecord
2
+ attribute :allow_publish, :boolean, default: true
3
+
4
+ # Callbacks
5
+ after_commit :publish, if: :allow_publish?
6
+ before_save :check_publishing
7
+
8
+ # Enums
9
+ enum status: { pending: 0, published: 1, failed: 2 }
10
+ enum size: { single: 0, batch: 1 }
11
+
12
+ # Validations
13
+ validates :payload, presence: true
14
+ validates :exchange, presence: true
15
+ validates :routing_key, presence: true
16
+
17
+ # Associations
18
+ belongs_to :outboxable, polymorphic: true, optional: true
19
+
20
+ def increment_attempt
21
+ self.attempts = attempts + 1
22
+ self.last_attempted_at = Time.zone.now
23
+ end
24
+
25
+ def publish
26
+ Outboxable::Worker.perform_async(id)
27
+ end
28
+
29
+ def check_publishing
30
+ self.allow_publish = false if published?
31
+ end
32
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/outboxable/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "outboxable"
7
+ spec.version = Outboxable::VERSION
8
+ spec.authors = ["Brusk Awat"]
9
+ spec.email = ["broosk.edogawa@gmail.com"]
10
+
11
+ spec.summary = "An opiniated Gem for Rails applications to implement the transactional outbox pattern."
12
+ spec.description = "The Outboxable Gem is tailored for Rails applications to implement the transactional outbox pattern. It currently only supports ActiveRecord."
13
+ spec.homepage = "https://githuh.com/broosk1993/outboxable"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://githuh.com/broosk1993/outboxable"
21
+ spec.metadata["changelog_uri"] = "https://githuh.com/broosk1993/outboxable/CHANGELOG.md"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(__dir__) do
26
+ `git ls-files -z`.split("\x0").reject do |f|
27
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|circleci)|appveyor)})
28
+ end
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ spec.add_dependency 'bunny', '>= 2.19.0'
35
+ spec.add_dependency 'connection_pool', '~> 2.3.0'
36
+ end
@@ -0,0 +1,4 @@
1
+ module Outboxable
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: outboxable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Brusk Awat
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-02-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bunny
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 2.19.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 2.19.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: connection_pool
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 2.3.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 2.3.0
41
+ description: The Outboxable Gem is tailored for Rails applications to implement the
42
+ transactional outbox pattern. It currently only supports ActiveRecord.
43
+ email:
44
+ - broosk.edogawa@gmail.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".rspec"
50
+ - ".rubocop.yml"
51
+ - CHANGELOG.md
52
+ - CODE_OF_CONDUCT.md
53
+ - Gemfile
54
+ - Gemfile.lock
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - lib/generators/outboxable/install_generator.rb
59
+ - lib/outboxable.rb
60
+ - lib/outboxable/configuration.rb
61
+ - lib/outboxable/connection.rb
62
+ - lib/outboxable/polling_publisher_worker.rb
63
+ - lib/outboxable/publishing_manager.rb
64
+ - lib/outboxable/rabbitmq/publisher.rb
65
+ - lib/outboxable/version.rb
66
+ - lib/outboxable/worker.rb
67
+ - lib/templates/create_outboxable_outboxes.rb
68
+ - lib/templates/initializer.rb
69
+ - lib/templates/outbox.rb
70
+ - outboxable.gemspec
71
+ - sig/outboxable.rbs
72
+ homepage: https://githuh.com/broosk1993/outboxable
73
+ licenses:
74
+ - MIT
75
+ metadata:
76
+ allowed_push_host: https://rubygems.org
77
+ homepage_uri: https://githuh.com/broosk1993/outboxable
78
+ source_code_uri: https://githuh.com/broosk1993/outboxable
79
+ changelog_uri: https://githuh.com/broosk1993/outboxable/CHANGELOG.md
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: 2.6.0
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubygems_version: 3.4.1
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: An opiniated Gem for Rails applications to implement the transactional outbox
99
+ pattern.
100
+ test_files: []