outboxer 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: a573b50b1f20cc0966a9c6baef36216240ab20adee17adcf03da8a193436bf78
4
+ data.tar.gz: 86573195530a94aac112fd0c52169a7564e97f0e677ba03c26d37b87e8eb7c02
5
+ SHA512:
6
+ metadata.gz: edd4bc1e08e57f742985e8382fa1202ee6df3812282f9b0d12054cce63406fb9a7101fa6a53d93f85205faf31ae324b35c3254c9ab586865959cdd1a2ccfe951
7
+ data.tar.gz: 5d5196f1bdcadb519720c4d80b01852d9463f1d5844a90bd8447d392675b17357adae4deb4088b8210ea65f0ab4555707b0affee69aac20b0832b5ff0505b77a
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,229 @@
1
+ AllCops:
2
+ NewCops: enable
3
+ TargetRubyVersion: 2.6
4
+ SuggestExtensions: false
5
+
6
+ Style/StringLiterals:
7
+ Enabled: true
8
+ EnforcedStyle: double_quotes
9
+
10
+ Style/StringLiteralsInInterpolation:
11
+ Enabled: true
12
+ EnforcedStyle: double_quotes
13
+
14
+ Layout/LineLength:
15
+ Max: 100
16
+
17
+ Style/Documentation:
18
+ Enabled: false
19
+
20
+ Metrics/BlockLength:
21
+ Enabled: false
22
+
23
+ Style/FrozenStringLiteralComment:
24
+ Enabled: false
25
+
26
+ Metrics/MethodLength:
27
+ Max: 60
28
+
29
+ Layout/MultilineMethodCallBraceLayout:
30
+ EnforcedStyle: same_line
31
+
32
+ Naming/RescuedExceptionsVariableName:
33
+ PreferredName: exception
34
+
35
+ Layout/MultilineMethodCallIndentation:
36
+ EnforcedStyle: indented
37
+
38
+ Gemspec/DeprecatedAttributeAssignment: # new in 1.30
39
+ Enabled: true
40
+ Gemspec/DevelopmentDependencies: # new in 1.44
41
+ Enabled: true
42
+ Gemspec/RequireMFA: # new in 1.23
43
+ Enabled: true
44
+ Layout/LineContinuationLeadingSpace: # new in 1.31
45
+ Enabled: true
46
+ Layout/LineContinuationSpacing: # new in 1.31
47
+ Enabled: true
48
+ Layout/LineEndStringConcatenationIndentation: # new in 1.18
49
+ Enabled: true
50
+ Layout/SpaceBeforeBrackets: # new in 1.7
51
+ Enabled: true
52
+ Lint/AmbiguousAssignment: # new in 1.7
53
+ Enabled: true
54
+ Lint/AmbiguousOperatorPrecedence: # new in 1.21
55
+ Enabled: true
56
+ Lint/AmbiguousRange: # new in 1.19
57
+ Enabled: true
58
+ Lint/ConstantOverwrittenInRescue: # new in 1.31
59
+ Enabled: true
60
+ Lint/DeprecatedConstants: # new in 1.8
61
+ Enabled: true
62
+ Lint/DuplicateBranch: # new in 1.3
63
+ Enabled: true
64
+ Lint/DuplicateMagicComment: # new in 1.37
65
+ Enabled: true
66
+ Lint/DuplicateMatchPattern: # new in 1.50
67
+ Enabled: true
68
+ Lint/DuplicateRegexpCharacterClassElement: # new in 1.1
69
+ Enabled: true
70
+ Lint/EmptyBlock: # new in 1.1
71
+ Enabled: true
72
+ Lint/EmptyClass: # new in 1.3
73
+ Enabled: true
74
+ Lint/EmptyInPattern: # new in 1.16
75
+ Enabled: true
76
+ Lint/IncompatibleIoSelectWithFiberScheduler: # new in 1.21
77
+ Enabled: true
78
+ Lint/LambdaWithoutLiteralBlock: # new in 1.8
79
+ Enabled: true
80
+ Lint/MixedCaseRange: # new in 1.53
81
+ Enabled: true
82
+ Lint/NoReturnInBeginEndBlocks: # new in 1.2
83
+ Enabled: true
84
+ Lint/NonAtomicFileOperation: # new in 1.31
85
+ Enabled: true
86
+ Lint/NumberedParameterAssignment: # new in 1.9
87
+ Enabled: true
88
+ Lint/OrAssignmentToConstant: # new in 1.9
89
+ Enabled: true
90
+ Lint/RedundantDirGlobSort: # new in 1.8
91
+ Enabled: true
92
+ Lint/RedundantRegexpQuantifiers: # new in 1.53
93
+ Enabled: true
94
+ Lint/RefinementImportMethods: # new in 1.27
95
+ Enabled: true
96
+ Lint/RequireRangeParentheses: # new in 1.32
97
+ Enabled: true
98
+ Lint/RequireRelativeSelfPath: # new in 1.22
99
+ Enabled: true
100
+ Lint/SymbolConversion: # new in 1.9
101
+ Enabled: true
102
+ Lint/ToEnumArguments: # new in 1.1
103
+ Enabled: true
104
+ Lint/TripleQuotes: # new in 1.9
105
+ Enabled: true
106
+ Lint/UnexpectedBlockArity: # new in 1.5
107
+ Enabled: true
108
+ Lint/UnmodifiedReduceAccumulator: # new in 1.1
109
+ Enabled: true
110
+ Lint/UselessRescue: # new in 1.43
111
+ Enabled: true
112
+ Lint/UselessRuby2Keywords: # new in 1.23
113
+ Enabled: true
114
+ Metrics/CollectionLiteralLength: # new in 1.47
115
+ Enabled: true
116
+ Naming/BlockForwarding: # new in 1.24
117
+ Enabled: true
118
+ Security/CompoundHash: # new in 1.28
119
+ Enabled: true
120
+ Security/IoMethods: # new in 1.22
121
+ Enabled: true
122
+ Style/ArgumentsForwarding: # new in 1.1
123
+ Enabled: true
124
+ Style/ArrayIntersect: # new in 1.40
125
+ Enabled: true
126
+ Style/CollectionCompact: # new in 1.2
127
+ Enabled: true
128
+ Style/ComparableClamp: # new in 1.44
129
+ Enabled: true
130
+ Style/ConcatArrayLiterals: # new in 1.41
131
+ Enabled: true
132
+ Style/DataInheritance: # new in 1.49
133
+ Enabled: true
134
+ Style/DirEmpty: # new in 1.48
135
+ Enabled: true
136
+ Style/DocumentDynamicEvalDefinition: # new in 1.1
137
+ Enabled: true
138
+ Style/EmptyHeredoc: # new in 1.32
139
+ Enabled: true
140
+ Style/EndlessMethod: # new in 1.8
141
+ Enabled: true
142
+ Style/EnvHome: # new in 1.29
143
+ Enabled: true
144
+ Style/ExactRegexpMatch: # new in 1.51
145
+ Enabled: true
146
+ Style/FetchEnvVar: # new in 1.28
147
+ Enabled: true
148
+ Style/FileEmpty: # new in 1.48
149
+ Enabled: true
150
+ Style/FileRead: # new in 1.24
151
+ Enabled: true
152
+ Style/FileWrite: # new in 1.24
153
+ Enabled: true
154
+ Style/HashConversion: # new in 1.10
155
+ Enabled: true
156
+ Style/HashExcept: # new in 1.7
157
+ Enabled: true
158
+ Style/IfWithBooleanLiteralBranches: # new in 1.9
159
+ Enabled: true
160
+ Style/InPatternThen: # new in 1.16
161
+ Enabled: true
162
+ Style/MagicCommentFormat: # new in 1.35
163
+ Enabled: true
164
+ Style/MapCompactWithConditionalBlock: # new in 1.30
165
+ Enabled: true
166
+ Style/MapToHash: # new in 1.24
167
+ Enabled: true
168
+ Style/MapToSet: # new in 1.42
169
+ Enabled: true
170
+ Style/MinMaxComparison: # new in 1.42
171
+ Enabled: true
172
+ Style/MultilineInPatternThen: # new in 1.16
173
+ Enabled: true
174
+ Style/NegatedIfElseCondition: # new in 1.2
175
+ Enabled: true
176
+ Style/NestedFileDirname: # new in 1.26
177
+ Enabled: true
178
+ Style/NilLambda: # new in 1.3
179
+ Enabled: true
180
+ Style/NumberedParameters: # new in 1.22
181
+ Enabled: true
182
+ Style/NumberedParametersLimit: # new in 1.22
183
+ Enabled: true
184
+ Style/ObjectThen: # new in 1.28
185
+ Enabled: true
186
+ Style/OpenStructUse: # new in 1.23
187
+ Enabled: true
188
+ Style/OperatorMethodCall: # new in 1.37
189
+ Enabled: true
190
+ Style/QuotedSymbols: # new in 1.16
191
+ Enabled: true
192
+ Style/RedundantArgument: # new in 1.4
193
+ Enabled: true
194
+ Style/RedundantArrayConstructor: # new in 1.52
195
+ Enabled: true
196
+ Style/RedundantConstantBase: # new in 1.40
197
+ Enabled: true
198
+ Style/RedundantCurrentDirectoryInPath: # new in 1.53
199
+ Enabled: true
200
+ Style/RedundantDoubleSplatHashBraces: # new in 1.41
201
+ Enabled: true
202
+ Style/RedundantEach: # new in 1.38
203
+ Enabled: true
204
+ Style/RedundantFilterChain: # new in 1.52
205
+ Enabled: true
206
+ Style/RedundantHeredocDelimiterQuotes: # new in 1.45
207
+ Enabled: true
208
+ Style/RedundantInitialize: # new in 1.27
209
+ Enabled: true
210
+ Style/RedundantLineContinuation: # new in 1.49
211
+ Enabled: true
212
+ Style/RedundantRegexpArgument: # new in 1.53
213
+ Enabled: true
214
+ Style/RedundantRegexpConstructor: # new in 1.52
215
+ Enabled: true
216
+ Style/RedundantSelfAssignmentBranch: # new in 1.19
217
+ Enabled: true
218
+ Style/RedundantStringEscape: # new in 1.37
219
+ Enabled: true
220
+ Style/ReturnNilInPredicateMethodDefinition: # new in 1.53
221
+ Enabled: true
222
+ Style/SelectByRegexp: # new in 1.22
223
+ Enabled: true
224
+ Style/StringChars: # new in 1.12
225
+ Enabled: true
226
+ Style/SwapValues: # new in 1.1
227
+ Enabled: true
228
+ Style/YAMLFileRead: # new in 1.53
229
+ Enabled: true
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-07-30
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. 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/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Adam Mikulasev
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,76 @@
1
+ # Outboxer
2
+
3
+ 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.
4
+
5
+ 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.
6
+
7
+ ## Installation
8
+
9
+ 1. Add the Outboxer gem to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'outboxer'
13
+ ```
14
+
15
+ 2. Install the Outboxer gem:
16
+
17
+ ```bash
18
+ bundle install
19
+ ```
20
+
21
+ 3. Generate the migration and publisher files
22
+
23
+ ```bash
24
+ bin/rails generate outboxer:install
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ ### 1. Migrate your database
30
+
31
+ ```bash
32
+ bin/rake db:migrate
33
+ ```
34
+
35
+
36
+ ### 2. Include Outboxer into existing model
37
+
38
+ First, include `Outboxer::Outboxable` into your existing `Message` model:
39
+
40
+ ```ruby
41
+ class Message < ApplicationRecord
42
+ include Outboxer::Outboxable
43
+ end
44
+ ```
45
+
46
+ ### 3. Update the publish block
47
+
48
+ By default, the `bin/publisher` script does not do anything with the message in its block.
49
+
50
+ To customize this behavior, you should update the block in the `bin/publisher` file:
51
+
52
+ ```ruby
53
+ Outboxer::Publisher.publish do |message:, logger:|
54
+ Worker.perform_async({ message_id: message.id })
55
+ end
56
+ ```
57
+
58
+ ### 3. Run the Publisher
59
+
60
+ To start publishing messages, run the `bin/publisher` script:
61
+
62
+ ```bash
63
+ bin/publisher
64
+ ```
65
+
66
+ ## Contributing
67
+
68
+ 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).
69
+
70
+ ## License
71
+
72
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
73
+
74
+ ## Code of Conduct
75
+
76
+ 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).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ require "rubocop/rake_task"
7
+
8
+ RuboCop::RakeTask.new
9
+
10
+ task default: %i[spec rubocop]
@@ -0,0 +1,30 @@
1
+ module Outboxer
2
+ class InstallGenerator < Rails::Generators::Base
3
+ include Rails::Generators::Migration
4
+
5
+ source_root File.expand_path("templates", __dir__)
6
+
7
+ def self.next_migration_number(dirname)
8
+ next_number = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i
9
+
10
+ next_number += 1 while Dir.glob("#{dirname}/#{next_number}*").any?
11
+
12
+ next_number.to_s
13
+ end
14
+
15
+ def copy_bin_file
16
+ template "bin/publisher.rb", "bin/publisher"
17
+ run "chmod +x bin/publisher"
18
+ end
19
+
20
+ def copy_migrations
21
+ migration_template(
22
+ "migrations/create_outboxer_messages.rb",
23
+ "db/migrate/create_outboxer_messages.rb")
24
+
25
+ migration_template(
26
+ "migrations/create_outboxer_exceptions.rb",
27
+ "db/migrate/create_outboxer_exceptions.rb")
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative "../config/environment"
4
+
5
+ Outboxer::Publisher.publish do |args|
6
+ # Worker.perform_async({ message_id: args.message.id })
7
+ end
@@ -0,0 +1,15 @@
1
+ class CreateOutboxerExceptions < ActiveRecord::Migration[6.1]
2
+ def change
3
+ create_table :outboxer_exceptions do |t|
4
+ t.references :message, null: false, foreign_key: { to_table: :outboxer_messages }
5
+
6
+ t.text :class_name, null: false
7
+ t.text :message_text, null: false
8
+ t.column :backtrace, :text, array: true
9
+
10
+ t.timestamps
11
+ end
12
+
13
+ remove_column :outboxer_exceptions, :updated_at
14
+ end
15
+ end
@@ -0,0 +1,13 @@
1
+ class CreateOutboxerMessages < ActiveRecord::Migration[6.1]
2
+ def change
3
+ create_table :outboxer_messages do |t|
4
+ t.references :message, polymorphic: true, null: false
5
+ t.text :status, null: false
6
+
7
+ t.timestamps
8
+ end
9
+
10
+ add_index :outboxer_messages, %i[status created_at]
11
+ add_index :outboxer_messages, %i[message_type message_id], unique: true
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ require "active_record"
2
+
3
+ module Outboxer
4
+ module Models
5
+ class Exception < ::ActiveRecord::Base
6
+ self.table_name = :outboxer_exceptions
7
+
8
+ belongs_to :message, class_name: "::Outboxer::Models::Message"
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,20 @@
1
+ require "active_record"
2
+
3
+ module Outboxer
4
+ module Models
5
+ class Message < ::ActiveRecord::Base
6
+ self.table_name = :outboxer_messages
7
+
8
+ STATUS = {
9
+ unpublished: "unpublished",
10
+ publishing: "publishing",
11
+ failed: "failed"
12
+ }.freeze
13
+
14
+ belongs_to :message, polymorphic: true
15
+
16
+ has_many :exceptions, -> { order(created_at: :asc) },
17
+ class_name: "::Outboxer::Models::Exception"
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,23 @@
1
+ module Outboxer
2
+ module Models
3
+ module Outboxable
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+
7
+ base.class_eval do
8
+ has_one :message, class_name: "Outboxer::Models::Message", as: :outbox_message,
9
+ dependent: :destroy
10
+
11
+ after_create :create_outbox_message!
12
+ end
13
+ end
14
+
15
+ def create_outbox_message!
16
+ Models::Message.create!(message: self, status: Models::Message::STATUS[:unpublished])
17
+ end
18
+
19
+ module ClassMethods
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,113 @@
1
+ require "logger"
2
+
3
+ module Outboxer
4
+ module Publisher
5
+ module_function
6
+
7
+ @publishing = false
8
+ @logger = Logger.new($stdout)
9
+
10
+ Args = Struct.new(:message, :logger)
11
+
12
+ def publish(poll: 1, backoff: ->(current_backoff) { [current_backoff * 2, 5 * 60].min })
13
+ @publishing = true
14
+
15
+ ActiveRecord::Base.connection_pool.with_connection do
16
+ while @publishing
17
+ outboxer_message = dequeue(backoff: backoff)
18
+
19
+ if outboxer_message.nil?
20
+ sleep poll
21
+
22
+ next
23
+ end
24
+
25
+ begin
26
+ yield Args.new(outboxer_message.message, @logger)
27
+ rescue StandardError => exception
28
+ failed(
29
+ outboxer_message: outboxer_message,
30
+ backoff: backoff,
31
+ exception: exception)
32
+
33
+ next
34
+ end
35
+
36
+ published(
37
+ outboxer_message: outboxer_message,
38
+ backoff: backoff)
39
+ end
40
+ end
41
+ end
42
+
43
+ def dequeue(backoff:)
44
+ retry_on_error(backoff: backoff) do
45
+ ActiveRecord::Base.transaction do
46
+ message = Models::Message
47
+ .where(status: Models::Message::STATUS[:unpublished])
48
+ .order(created_at: :asc)
49
+ .limit(1)
50
+ .lock("FOR UPDATE SKIP LOCKED")
51
+ .first
52
+
53
+ message&.update!(status: Models::Message::STATUS[:publishing])
54
+
55
+ message
56
+ end
57
+ end
58
+ end
59
+
60
+ def published(outboxer_message:, backoff:)
61
+ retry_on_error(backoff: backoff) do
62
+ outboxer_message.destroy!
63
+ end
64
+ end
65
+
66
+ def failed(outboxer_message:, exception:, backoff:)
67
+ @logger.error(
68
+ "Exception raised: #{exception.class}: #{exception.message}\n" \
69
+ "#{exception.backtrace.join("\n")}")
70
+
71
+ retry_on_error(backoff: backoff) do
72
+ ActiveRecord::Base.transaction do
73
+ outboxer_message.update!(status: Models::Message::STATUS[:failed])
74
+
75
+ outboxer_message.exceptions.create!(
76
+ class_name: exception.class.name,
77
+ message_text: exception.message,
78
+ backtrace: exception.backtrace)
79
+ end
80
+ end
81
+ end
82
+
83
+ def retry_on_error(backoff:, &block)
84
+ current_backoff = 1
85
+
86
+ begin
87
+ block.call
88
+ rescue StandardError => exception
89
+ @logger.fatal(
90
+ "Exception raised: #{exception.class}: #{exception.message}\n" \
91
+ "#{exception.backtrace.join("\n")}")
92
+
93
+ raise exception unless @publishing
94
+
95
+ sleep current_backoff
96
+
97
+ current_backoff = backoff.call(current_backoff)
98
+
99
+ retry
100
+ end
101
+ end
102
+
103
+ def stop
104
+ @publishing = false
105
+ end
106
+
107
+ Signal.trap("TERM") do
108
+ @logger.info("Received SIGTERM, stopping...")
109
+
110
+ stop
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,12 @@
1
+ require "outboxer"
2
+ require "rails"
3
+
4
+ module Outboxer
5
+ class Railtie < Rails::Railtie
6
+ railtie_name :outboxer
7
+
8
+ generators do
9
+ require_relative "../../generators/outboxer/install_generator"
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module Outboxer
2
+ VERSION = "0.1.0".freeze
3
+ end
data/lib/outboxer.rb ADDED
@@ -0,0 +1,13 @@
1
+ require "active_support"
2
+
3
+ require_relative "outboxer/version"
4
+ require_relative "outboxer/railtie" if defined?(Rails)
5
+ require_relative "outboxer/models/outboxable"
6
+ require_relative "outboxer/models/message"
7
+ require_relative "outboxer/models/exception"
8
+
9
+ require_relative "outboxer/publisher"
10
+
11
+ module Outboxer
12
+ Outboxable = Models::Outboxable
13
+ end
data/outboxer.gemspec ADDED
@@ -0,0 +1,33 @@
1
+ # rubocop:disable Layout/LineLength
2
+ require_relative "lib/outboxer/version"
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "outboxer"
6
+ spec.version = Outboxer::VERSION
7
+ spec.authors = ["Adam Mikulasev"]
8
+ spec.email = ["adam@fastprogrammer.co"]
9
+
10
+ spec.summary = "Transactional outbox implementation for event driven Ruby on Rails applications that use SQL"
11
+ spec.homepage = "https://github.com/fast-programmer/outboxer"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = ">= 2.6.0"
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = "https://github.com/fast-programmer/outboxer"
17
+ # spec.metadata["changelog_uri"] = "Put your gem's CHANGELOG.md URL here."
18
+ spec.metadata["rubygems_mfa_required"] = "true"
19
+
20
+ spec.files = Dir.chdir(__dir__) do
21
+ `git ls-files -z`.split("\x0").reject do |f|
22
+ (File.expand_path(f) == __FILE__) ||
23
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor Gemfile])
24
+ end
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_dependency "activerecord", "~> 7.0"
31
+ spec.add_dependency "pg", "~> 1.5"
32
+ end
33
+ # rubocop:enable Layout/LineLength
data/sig/outboxer.rbs ADDED
@@ -0,0 +1,19 @@
1
+ module Outboxer
2
+ VERSION: String
3
+
4
+ module Publisher
5
+ class Args
6
+ attr_reader message: untyped
7
+ attr_reader logger: Logger
8
+
9
+ def initialize: (untyped message, Logger logger) -> void
10
+ end
11
+
12
+ def self.publish: (?Integer poll, ?Proc[Integer, Integer] backoff) { (Args) -> untyped } -> void
13
+ def self.dequeue: ({ backoff: Proc[Integer, Integer] }) -> untyped
14
+ def self.published: ({ outboxer_message: untyped, backoff: Proc[Integer, Integer] }) -> void
15
+ def self.failed: ({ outboxer_message: untyped, exception: Exception, backoff: Proc[Integer, Integer] }) -> void
16
+ def self.retry_on_error: ({ backoff: Proc[Integer, Integer] }) { () -> untyped } -> untyped
17
+ def self.stop: () -> void
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: outboxer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Mikulasev
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-08-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '7.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '7.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pg
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ description:
42
+ email:
43
+ - adam@fastprogrammer.co
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".rspec"
49
+ - ".rubocop.yml"
50
+ - CHANGELOG.md
51
+ - CODE_OF_CONDUCT.md
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - generators/outboxer/install_generator.rb
56
+ - generators/outboxer/templates/bin/publisher.rb
57
+ - generators/outboxer/templates/migrations/create_outboxer_exceptions.rb
58
+ - generators/outboxer/templates/migrations/create_outboxer_messages.rb
59
+ - lib/outboxer.rb
60
+ - lib/outboxer/models/exception.rb
61
+ - lib/outboxer/models/message.rb
62
+ - lib/outboxer/models/outboxable.rb
63
+ - lib/outboxer/publisher.rb
64
+ - lib/outboxer/railtie.rb
65
+ - lib/outboxer/version.rb
66
+ - outboxer.gemspec
67
+ - sig/outboxer.rbs
68
+ homepage: https://github.com/fast-programmer/outboxer
69
+ licenses:
70
+ - MIT
71
+ metadata:
72
+ homepage_uri: https://github.com/fast-programmer/outboxer
73
+ source_code_uri: https://github.com/fast-programmer/outboxer
74
+ rubygems_mfa_required: 'true'
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: 2.6.0
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubygems_version: 3.1.6
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Transactional outbox implementation for event driven Ruby on Rails applications
94
+ that use SQL
95
+ test_files: []