From de7c30452417a81121434add67f29e6cb6742b72 Mon Sep 17 00:00:00 2001 From: Muhammad Nawzad Date: Tue, 2 Apr 2024 14:50:27 +0300 Subject: [PATCH 1/4] Fixes initialize --- lib/generators/outboxable/install_generator.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/generators/outboxable/install_generator.rb b/lib/generators/outboxable/install_generator.rb index caa3062..d1a7c09 100644 --- a/lib/generators/outboxable/install_generator.rb +++ b/lib/generators/outboxable/install_generator.rb @@ -5,8 +5,8 @@ module Outboxable source_root File.expand_path('../../templates', __dir__) class_option :orm, type: :string, default: 'activerecord' - def initialize - super + def initialize(*args) + super(*args) @orm = options[:orm] || 'activerecord' %w[activerecord mongoid].include?(@orm) || raise(ArgumentError, 'Invalid ORM. Only ActiveRecord and Mongoid are supported.') From e5ebfb84d03b8aacb8356ac524c70df3d3b3fbdc Mon Sep 17 00:00:00 2001 From: Muhammad Nawzad Date: Tue, 2 Apr 2024 14:50:42 +0300 Subject: [PATCH 2/4] Add index on outboxes table for status and last_attempted_at --- lib/templates/create_outboxable_outboxes.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/templates/create_outboxable_outboxes.rb b/lib/templates/create_outboxable_outboxes.rb index 800acdb..474ada0 100644 --- a/lib/templates/create_outboxable_outboxes.rb +++ b/lib/templates/create_outboxable_outboxes.rb @@ -21,5 +21,7 @@ class CreateOutboxableOutboxes < ActiveRecord::Migration[7.0] t.timestamps end + + add_index :outboxes, %i[status last_attempted_at], name: 'index_outboxes_on_outboxable_status_and_last_attempted_at' end end From 035a9822ee2a34981ab2371e2461c7c9f9f8a8d6 Mon Sep 17 00:00:00 2001 From: Muhammad Nawzad Date: Tue, 28 May 2024 13:44:54 +0300 Subject: [PATCH 3/4] Adds content_type configuration --- lib/outboxable/rabbitmq/publisher.rb | 13 ++++++++++++- lib/templates/create_outboxable_outboxes.rb | 1 + lib/templates/mongoid_outbox.rb | 1 + 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/outboxable/rabbitmq/publisher.rb b/lib/outboxable/rabbitmq/publisher.rb index 034d1e5..0a02eb2 100644 --- a/lib/outboxable/rabbitmq/publisher.rb +++ b/lib/outboxable/rabbitmq/publisher.rb @@ -19,8 +19,19 @@ module Outboxable # Declare a exchange exchange = channel.topic(@resource.exchange, durable: true) + content_type = if @resource.respond_to?(:content_type) && @resource.content_type.present? + @resource.content_type + else + 'application/json' + end + # Publish the CloudEvent resource to the exchange - exchange.publish(to_envelope(resource: @resource), routing_key: @resource.routing_key, headers: @resource.try(:headers) || {}) + exchange.publish( + to_envelope(resource: @resource), + routing_key: @resource.routing_key, + headers: @resource.try(:headers) || {}, + content_type: + ) # Wait for confirmation confirmed = channel.wait_for_confirms diff --git a/lib/templates/create_outboxable_outboxes.rb b/lib/templates/create_outboxable_outboxes.rb index 474ada0..02a6e7b 100644 --- a/lib/templates/create_outboxable_outboxes.rb +++ b/lib/templates/create_outboxable_outboxes.rb @@ -7,6 +7,7 @@ class CreateOutboxableOutboxes < ActiveRecord::Migration[7.0] t.string :exchange, null: false, default: '' t.string :routing_key, null: false, default: '' + t.string :content_type, null: false, default: 'application/json' t.integer :attempts, null: false, default: 0 t.datetime :last_attempted_at, null: true diff --git a/lib/templates/mongoid_outbox.rb b/lib/templates/mongoid_outbox.rb index f905c51..cbe3dd7 100644 --- a/lib/templates/mongoid_outbox.rb +++ b/lib/templates/mongoid_outbox.rb @@ -10,6 +10,7 @@ class Outbox field :exchange, type: String, default: '' field :routing_key, type: String, default: '' + field :content_type, type: String, default: 'application/json' field :attempts, type: Integer, default: 0 From 816465eced3e7deb0b32845556179f1c1c8c3a63 Mon Sep 17 00:00:00 2001 From: Muhammad Nawzad Date: Tue, 28 May 2024 13:51:17 +0300 Subject: [PATCH 4/4] Refactors code for better readablity --- lib/outboxable/rabbitmq/publisher.rb | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/lib/outboxable/rabbitmq/publisher.rb b/lib/outboxable/rabbitmq/publisher.rb index 0a02eb2..1770881 100644 --- a/lib/outboxable/rabbitmq/publisher.rb +++ b/lib/outboxable/rabbitmq/publisher.rb @@ -19,18 +19,12 @@ module Outboxable # Declare a exchange exchange = channel.topic(@resource.exchange, durable: true) - content_type = if @resource.respond_to?(:content_type) && @resource.content_type.present? - @resource.content_type - else - 'application/json' - end - # Publish the CloudEvent resource to the exchange exchange.publish( to_envelope(resource: @resource), routing_key: @resource.routing_key, headers: @resource.try(:headers) || {}, - content_type: + content_type: @resource.try(:content_type) || 'application/json' ) # Wait for confirmation