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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7ff24c145223dcc558818028a2866583266372e1a52cbb0dc63661fdad5f94ea
4
- data.tar.gz: 64e19fa685b3f77a85c10bc60f9754a32bbe8534b9aaa5cb153253aefb5bbcb1
3
+ metadata.gz: d6b86cd386f79a70ee20e31ea49d2078df347b515858a8585dfa424a008651f6
4
+ data.tar.gz: 53074d0ba82ac19955a754090f788b326221b1e1a487b185fe0aac330fd927f4
5
5
  SHA512:
6
- metadata.gz: 9dca7a575abbc63e9b033e3726229ce8abe6254ea049f220f66c9b675c4395efbfdac1a12da0dc5c31f50a65bfdab939ee23809c24d9e2182864fdd0b703754f
7
- data.tar.gz: 6673eabf79e2daa9b36bd93c84589dc68f796a22c8b82cf93d035cd9eaaa1b3ba523ee16c9c121c719befe9b1527b3bf32a6c2467ee023b9bfd527b209575ef3
6
+ metadata.gz: 20f03367d91067dc952bfbc45b4038a4b0e944a695f2a3dae4613ded3e139c655d3af1bb56f0f25148c900f1ebe26de8c52a61b1d1e8dde12c5179ada5c41751
7
+ data.tar.gz: 227e6a787c3567b512be1cb1dcf2d66ab532046019fb6f86f6ef4a1f243088388947ab73b00a5f453dd36f2da97e59c24b9a1909514e612ac9cd291a84c28609
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- outboxable (1.0.2)
4
+ outboxable (1.0.3)
5
5
  bunny (>= 2.19.0)
6
6
  connection_pool (~> 2.3.0)
7
7
 
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 currently only supports ActiveRecord.
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
- Then run:
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
- The command above will add a migration file and the Outbox model. You will need then to run the migrations:
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. For now, only ActiveRecord is supported.
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(args)
7
- orm = args['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(orm)
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, orm)
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(orm)
22
- Outbox.pending.where(last_attempted_at: [..Time.zone.now, nil]).each do |outbox|
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, orm)
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Outboxable
4
- VERSION = '1.0.2'
4
+ VERSION = '1.0.3'
5
5
  end
@@ -4,9 +4,9 @@ module Outboxable
4
4
  class Worker
5
5
  include ::Sidekiq::Job
6
6
 
7
- def perform(outbox_id, orm)
8
- Outboxable::PublishingManager.publish(resource: Outbox.find(outbox_id)) if orm == 'activerecord'
9
- Outboxable::PublishingManager.publish(resource: Outbox.find_by!(idempotency_key: outbox_id)) if orm == 'mongoid'
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 message broker you are using. For now, only RabbitMQ is supported.
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 message broker you are using. For now, only RabbitMQ is supported.
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.2
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-13 00:00:00.000000000 Z
11
+ date: 2023-04-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bunny