outboxable 1.0.2 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +17 -5
- data/lib/outboxable/configuration.rb +6 -6
- data/lib/outboxable/polling_publisher_worker.rb +7 -9
- data/lib/outboxable/version.rb +1 -1
- data/lib/outboxable/worker.rb +3 -3
- data/lib/outboxable.rb +3 -2
- data/lib/templates/activerecord_initializer.rb +1 -1
- data/lib/templates/mongoid_initializer.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d6b86cd386f79a70ee20e31ea49d2078df347b515858a8585dfa424a008651f6
|
4
|
+
data.tar.gz: 53074d0ba82ac19955a754090f788b326221b1e1a487b185fe0aac330fd927f4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 20f03367d91067dc952bfbc45b4038a4b0e944a695f2a3dae4613ded3e139c655d3af1bb56f0f25148c900f1ebe26de8c52a61b1d1e8dde12c5179ada5c41751
|
7
|
+
data.tar.gz: 227e6a787c3567b512be1cb1dcf2d66ab532046019fb6f86f6ef4a1f243088388947ab73b00a5f453dd36f2da97e59c24b9a1909514e612ac9cd291a84c28609
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Outboxable
|
2
2
|
|
3
|
-
The Outboxable Gem is tailored for Rails applications to implement the transactional outbox pattern. It
|
3
|
+
The Outboxable Gem is tailored for Rails applications to implement the transactional outbox pattern. It supports both ActiveRecord and Mongoid.
|
4
4
|
|
5
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
6
|
|
@@ -25,13 +25,19 @@ If bundler is not being used to manage dependencies, install the gem by executin
|
|
25
25
|
$ gem install outboxable
|
26
26
|
```
|
27
27
|
|
28
|
-
|
28
|
+
For use with ActiveRecord, run:
|
29
29
|
|
30
30
|
```shell
|
31
|
-
$ rails g outboxable:install
|
31
|
+
$ rails g outboxable:install --orm activerecord
|
32
32
|
```
|
33
33
|
|
34
|
-
|
34
|
+
For use with Mongoid, run:
|
35
|
+
|
36
|
+
```shell
|
37
|
+
$ rails g outboxable:install --orm mongoid
|
38
|
+
```
|
39
|
+
|
40
|
+
The command above will add a migration file and the Outbox model. You will need then to run the migrations (ActiveRecord only):
|
35
41
|
|
36
42
|
```shell
|
37
43
|
$ rails db:migrate
|
@@ -64,7 +70,7 @@ module Outboxable
|
|
64
70
|
end
|
65
71
|
|
66
72
|
Outboxable.configure do |config|
|
67
|
-
# Specify the ORM you are using.
|
73
|
+
# Specify the ORM you are using. Supported values are :activerecord and :mongoid
|
68
74
|
config.orm = :activerecord
|
69
75
|
|
70
76
|
# Specify the message broker you are using. For now, only RabbitMQ is supported.
|
@@ -208,6 +214,12 @@ Last but not least, run sidekiq so that the Outboxable Gem can publish the event
|
|
208
214
|
$ bundle exec sidekiq
|
209
215
|
```
|
210
216
|
|
217
|
+
|
218
|
+
|
219
|
+
### Mongoid
|
220
|
+
|
221
|
+
The Outboxable gem works smoothly with Mongoid. It is to be noted that when used with Mongoid, Outboxable does not use the `_id` as the idempotency key. It creates a field called ``idempotency_key`` which is a UUID generated at the time of the insertion of the document.
|
222
|
+
|
211
223
|
## Development
|
212
224
|
|
213
225
|
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.
|
@@ -6,12 +6,6 @@ module Outboxable
|
|
6
6
|
def self.configure
|
7
7
|
self.configuration ||= Configuration.new
|
8
8
|
yield(configuration)
|
9
|
-
|
10
|
-
# In accordance to sidekiq-cron README: https://github.com/sidekiq-cron/sidekiq-cron#under-the-hood
|
11
|
-
Sidekiq::Options[:cron_poll_interval] = 5
|
12
|
-
|
13
|
-
# Create the cron job for the polling publisher
|
14
|
-
Sidekiq::Cron::Job.create(name: 'OutboxablePollingPublisher', cron: '*/5 * * * * *', class: 'Outboxable::PollingPublisherWorker', args: [{ orm: configuration.orm }])
|
15
9
|
end
|
16
10
|
|
17
11
|
class Configuration
|
@@ -32,6 +26,12 @@ module Outboxable
|
|
32
26
|
raise Error, 'Outboxable Gem only supports Rails but you application does not seem to be a Rails app' unless Object.const_defined?('Rails')
|
33
27
|
raise Error, 'Outboxable Gem only support Rails version 7 and newer' if Rails::VERSION::MAJOR < 7
|
34
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
35
|
end
|
36
36
|
|
37
37
|
def message_broker=(message_broker)
|
@@ -1,27 +1,25 @@
|
|
1
1
|
module Outboxable
|
2
2
|
class PollingPublisherWorker
|
3
3
|
include Sidekiq::Job
|
4
|
-
sidekiq_options queue: 'critical'
|
5
4
|
|
6
|
-
def perform
|
7
|
-
orm
|
8
|
-
orm == 'mongoid' ? perform_mongoid(orm) : perform_activerecord(orm)
|
5
|
+
def perform
|
6
|
+
Outboxable.configuration.orm == :mongoid ? perform_mongoid : perform_activerecord
|
9
7
|
end
|
10
8
|
|
11
|
-
def perform_activerecord
|
9
|
+
def perform_activerecord
|
12
10
|
Outbox.pending.where(last_attempted_at: [..Time.zone.now, nil]).find_in_batches(batch_size: 100).each do |batch|
|
13
11
|
batch.each do |outbox|
|
14
12
|
# This is to prevent a job from being retried too many times. Worst-case scenario is 1 minute delay in jobs.
|
15
|
-
Outboxable::Worker.perform_async(outbox.id
|
13
|
+
::Outboxable::Worker.perform_async(outbox.id)
|
16
14
|
outbox.update(last_attempted_at: 1.minute.from_now, status: :processing, allow_publish: false)
|
17
15
|
end
|
18
16
|
end
|
19
17
|
end
|
20
18
|
|
21
|
-
def perform_mongoid
|
22
|
-
Outbox.pending.
|
19
|
+
def perform_mongoid
|
20
|
+
Outbox.pending.any_of({ last_attempted_at: ..Time.zone.now }, { last_attempted_at: nil }).each do |outbox|
|
23
21
|
# This is to prevent a job from being retried too many times. Worst-case scenario is 1 minute delay in jobs.
|
24
|
-
Outboxable::Worker.perform_async(outbox.idempotency_key
|
22
|
+
::Outboxable::Worker.perform_async(outbox.idempotency_key)
|
25
23
|
outbox.update(last_attempted_at: 1.minute.from_now, status: :processing, allow_publish: false)
|
26
24
|
end
|
27
25
|
end
|
data/lib/outboxable/version.rb
CHANGED
data/lib/outboxable/worker.rb
CHANGED
@@ -4,9 +4,9 @@ module Outboxable
|
|
4
4
|
class Worker
|
5
5
|
include ::Sidekiq::Job
|
6
6
|
|
7
|
-
def perform(outbox_id
|
8
|
-
Outboxable::PublishingManager.publish(resource: Outbox.find(outbox_id)) if orm ==
|
9
|
-
Outboxable::PublishingManager.publish(resource: Outbox.find_by!(idempotency_key: outbox_id)) if orm ==
|
7
|
+
def perform(outbox_id)
|
8
|
+
Outboxable::PublishingManager.publish(resource: Outbox.find(outbox_id)) if Outboxable.configuration.orm == :activerecord
|
9
|
+
Outboxable::PublishingManager.publish(resource: Outbox.find_by!(idempotency_key: outbox_id)) if Outboxable.configuration.orm == :mongoid
|
10
10
|
end
|
11
11
|
end
|
12
12
|
end
|
data/lib/outboxable.rb
CHANGED
@@ -3,14 +3,15 @@
|
|
3
3
|
require_relative 'outboxable/version'
|
4
4
|
|
5
5
|
require 'outboxable/worker'
|
6
|
-
require 'outboxable/publishing_manager'
|
7
|
-
require 'outboxable/polling_publisher_worker'
|
8
6
|
require 'outboxable/connection'
|
9
7
|
require 'outboxable/configuration'
|
10
8
|
require 'outboxable/rabbitmq/publisher'
|
11
9
|
|
12
10
|
require 'active_support/concern'
|
13
11
|
|
12
|
+
require 'outboxable/publishing_manager'
|
13
|
+
require 'outboxable/polling_publisher_worker'
|
14
|
+
|
14
15
|
module Outboxable
|
15
16
|
class Error < StandardError; end
|
16
17
|
|
@@ -23,7 +23,7 @@ Outboxable.configure do |config|
|
|
23
23
|
# Specify the ORM you are using. For now, only ActiveRecord is supported.
|
24
24
|
config.orm = :activerecord
|
25
25
|
|
26
|
-
# Specify the
|
26
|
+
# Specify the ORM you are using. Supported values are :activerecord and :mongoid
|
27
27
|
config.message_broker = :rabbitmq
|
28
28
|
|
29
29
|
# RabbitMQ configurations
|
@@ -23,7 +23,7 @@ Outboxable.configure do |config|
|
|
23
23
|
# Specify the ORM you are using. For now, only ActiveRecord is supported.
|
24
24
|
config.orm = :mongoid
|
25
25
|
|
26
|
-
# Specify the
|
26
|
+
# Specify the ORM you are using. Supported values are :activerecord and :mongoid
|
27
27
|
config.message_broker = :rabbitmq
|
28
28
|
|
29
29
|
# RabbitMQ configurations
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: outboxable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brusk Awat
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-04-
|
11
|
+
date: 2023-04-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bunny
|