diff --git a/db/seeds.rb b/db/seeds.rb index 276edb19..77e208ac 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -9,19 +9,19 @@ .create!( messageable_type: messageable_type, messageable_id: messageable_id, - status: Outboxer::Models::Message::STATUS[:unpublished]) + status: Outboxer::Models::Message::Status::UNPUBLISHED) Outboxer::Models::Message .create!( messageable_type: messageable_type, messageable_id: messageable_id, - status: Outboxer::Models::Message::STATUS[:publishing]) + status: Outboxer::Models::Message::Status::PUBLISHING) failed_message = Outboxer::Models::Message .create!( messageable_type: messageable_type, messageable_id: messageable_id, - status: Outboxer::Models::Message::STATUS[:failed]) + status: Outboxer::Models::Message::Status::FAILED) failed_message .outboxer_exceptions diff --git a/lib/outboxer/message.rb b/lib/outboxer/message.rb index 50b2f9d4..47e6a090 100644 --- a/lib/outboxer/message.rb +++ b/lib/outboxer/message.rb @@ -13,21 +13,21 @@ def unpublished!(limit: 5, order: :asc) ActiveRecord::Base.connection_pool.with_connection do message_ids = ActiveRecord::Base.transaction do ids = Models::Message - .where(status: Models::Message::STATUS[:unpublished]) + .where(status: Models::Message::Status::UNPUBLISHED) .order(created_at: order) .lock('FOR UPDATE SKIP LOCKED') .limit(limit) .pluck(:id) if !ids.empty? - Models::Message.where(id: ids).update_all(status: Models::Message::STATUS[:publishing]) + Models::Message.where(id: ids).update_all(status: Models::Message::Status::PUBLISHING) end ids end Models::Message - .where(id: message_ids, status: Models::Message::STATUS[:publishing]) + .where(id: message_ids, status: Models::Message::Status::PUBLISHING) .order(created_at: order) .to_a end @@ -37,7 +37,7 @@ def published!(id:) ActiveRecord::Base.connection_pool.with_connection do outboxer_message = Models::Message.order(created_at: :asc).lock.find_by!(id: id) - if outboxer_message.status != Models::Message::STATUS[:publishing] + if outboxer_message.status != Models::Message::Status::PUBLISHING raise InvalidTransition, "cannot transition outboxer message #{outboxer_message.id} " \ "from #{outboxer_message.status} to (deleted)" @@ -54,13 +54,13 @@ def failed!(id:, exception:) ActiveRecord::Base.transaction do outboxer_message = Models::Message.order(created_at: :asc).lock.find_by!(id: id) - if outboxer_message.status != Models::Message::STATUS[:publishing] + if outboxer_message.status != Models::Message::Status::PUBLISHING raise InvalidTransition, "cannot transition outboxer message #{id} " \ - "from #{outboxer_message.status} to #{Models::Message::STATUS[:failed]}" + "from #{outboxer_message.status} to #{Models::Message::Status::FAILED}" end - outboxer_message.update!(status: Models::Message::STATUS[:failed]) + outboxer_message.update!(status: Models::Message::Status::FAILED) outboxer_message.outboxer_exceptions.create!( class_name: exception.class.name, @@ -76,13 +76,13 @@ def republish!(id:) ActiveRecord::Base.connection_pool.with_connection do outboxer_message = Models::Message.lock.find_by!(id: id) - if outboxer_message.status != Models::Message::STATUS[:failed] + if outboxer_message.status != Models::Message::Status::FAILED raise InvalidTransition, "cannot transition outboxer message #{id} " \ - "from #{outboxer_message.status} to #{Models::Message::STATUS[:unpublished]}" + "from #{outboxer_message.status} to #{Models::Message::Status::UNPUBLISHED}" end - outboxer_message.update!(status: Models::Message::STATUS[:unpublished]) + outboxer_message.update!(status: Models::Message::Status::UNPUBLISHED) outboxer_message end diff --git a/lib/outboxer/models/message.rb b/lib/outboxer/models/message.rb index 3152589d..7fc5bc6a 100644 --- a/lib/outboxer/models/message.rb +++ b/lib/outboxer/models/message.rb @@ -19,17 +19,21 @@ module Models class Message < ::ActiveRecord::Base self.table_name = :outboxer_messages - STATUS = { - unpublished: "unpublished", - publishing: "publishing", - failed: "failed" - } + module Status + UNPUBLISHED = 'unpublished' + PUBLISHING = 'publishing' + PUBLISHED = 'published' + FAILED = 'failed' + end - scope :unpublished, -> { where(status: STATUS[:unpublished]) } - scope :publishing, -> { where(status: STATUS[:publishing]) } - scope :failed, -> { where(status: STATUS[:failed]) } + STATUSES = [Status::UNPUBLISHED, Status::PUBLISHING, Status::PUBLISHED, Status::FAILED] - attribute :status, default: -> { STATUS[:unpublished] } + scope :unpublished, -> { where(status: UNPUBLISHED) } + scope :publishing, -> { where(status: PUBLISHING) } + scope :failed, -> { where(status: FAILED) } + + attribute :status, default: -> { Status::UNPUBLISHED } + # validates :status, inclusion: { in: STATUSES } belongs_to :messageable, polymorphic: true diff --git a/spec/lib/outboxer/message/failed_spec.rb b/spec/lib/outboxer/message/failed_spec.rb index eff11c89..5eb9e905 100644 --- a/spec/lib/outboxer/message/failed_spec.rb +++ b/spec/lib/outboxer/message/failed_spec.rb @@ -10,7 +10,7 @@ module Outboxer Models::Message.create!( messageable_type: 'DummyType', messageable_id: 1, - status: Models::Message::STATUS[:publishing], + status: Models::Message::Status::PUBLISHING, created_at: DateTime.parse('2024-01-14T00:00:00Z')) end @@ -18,13 +18,13 @@ module Outboxer it 'returns updated message' do expect(failed_message.id).to eq(publishing_message.id) - expect(failed_message.status).to eq(Models::Message::STATUS[:failed]) + expect(failed_message.status).to eq(Models::Message::Status::FAILED) end it 'updates publishing message status to failed' do publishing_message.reload - expect(publishing_message.status).to eq(Models::Message::STATUS[:failed]) + expect(publishing_message.status).to eq(Models::Message::Status::FAILED) end end @@ -33,7 +33,7 @@ module Outboxer Models::Message.create!( messageable_type: 'DummyType', messageable_id: 1, - status: Models::Message::STATUS[:unpublished], + status: Models::Message::Status::UNPUBLISHED, created_at: DateTime.parse('2024-01-14T00:00:00Z')) end diff --git a/spec/lib/outboxer/message/published_spec.rb b/spec/lib/outboxer/message/published_spec.rb index 2c1b7ac2..c7d0be33 100644 --- a/spec/lib/outboxer/message/published_spec.rb +++ b/spec/lib/outboxer/message/published_spec.rb @@ -8,7 +8,7 @@ module Outboxer Models::Message.create!( messageable_type: 'DummyType', messageable_id: 1, - status: Models::Message::STATUS[:publishing], + status: Models::Message::Status::PUBLISHING, created_at: DateTime.parse('2024-01-14T00:00:00Z')) end @@ -28,7 +28,7 @@ module Outboxer Models::Message.create!( messageable_type: 'DummyType', messageable_id: 1, - status: Models::Message::STATUS[:unpublished], + status: Models::Message::Status::UNPUBLISHED, created_at: DateTime.parse('2024-01-14T00:00:00Z')) end diff --git a/spec/lib/outboxer/message/republish_spec.rb b/spec/lib/outboxer/message/republish_spec.rb index c626cad1..2e9664c7 100644 --- a/spec/lib/outboxer/message/republish_spec.rb +++ b/spec/lib/outboxer/message/republish_spec.rb @@ -8,7 +8,7 @@ module Outboxer Models::Message.create!( messageable_type: 'DummyType', messageable_id: 1, - status: Models::Message::STATUS[:failed], + status: Models::Message::Status::FAILED, created_at: DateTime.parse('2024-01-14T00:00:00Z')) end @@ -16,13 +16,13 @@ module Outboxer it 'returns unpublished message' do expect(unpublished_message.id).to eq(failed_message.id) - expect(unpublished_message.status).to eq(Models::Message::STATUS[:unpublished]) + expect(unpublished_message.status).to eq(Models::Message::Status::UNPUBLISHED) end it 'updates failed message status to unpublishied' do failed_message.reload - expect(failed_message.status).to eq(Models::Message::STATUS[:unpublished]) + expect(failed_message.status).to eq(Models::Message::Status::UNPUBLISHED) end end @@ -31,7 +31,7 @@ module Outboxer Models::Message.create!( messageable_type: 'DummyType', messageable_id: 1, - status: Models::Message::STATUS[:unpublished], + status: Models::Message::Status::UNPUBLISHED, created_at: DateTime.parse('2024-01-14T00:00:00Z')) end diff --git a/spec/lib/outboxer/message/unpublished_spec.rb b/spec/lib/outboxer/message/unpublished_spec.rb index 2f4b46cf..1b879dfb 100644 --- a/spec/lib/outboxer/message/unpublished_spec.rb +++ b/spec/lib/outboxer/message/unpublished_spec.rb @@ -9,12 +9,12 @@ module Outboxer Models::Message.create!( messageable_type: 'DummyType', messageable_id: 1, - status: Models::Message::STATUS[:unpublished], + status: Models::Message::Status::UNPUBLISHED, created_at: DateTime.parse('2024-01-14T00:00:00Z')), Models::Message.create!( messageable_type: 'DummyType', messageable_id: 2, - status: Models::Message::STATUS[:unpublished], + status: Models::Message::Status::UNPUBLISHED, created_at: DateTime.parse('2024-01-14T00:00:01Z')) ] end @@ -29,11 +29,12 @@ module Outboxer publishing_message = publishing_messages.first expect(publishing_message.messageable_type).to eq('DummyType') expect(publishing_message.messageable_id).to eq(1) - expect(publishing_message.status).to eq(Models::Message::STATUS[:publishing]) + expect(publishing_message.status).to eq(Models::Message::Status::PUBLISHING) end it 'keeps last unpublished message' do - remaining_messages = Models::Message.where(status: Models::Message::STATUS[:unpublished]) + remaining_messages = Models::Message.where(status: Models::Message::Status::UNPUBLISHED) + expect(remaining_messages.count).to eq(1) remaining_message = remaining_messages.last @@ -54,7 +55,7 @@ module Outboxer end it 'keeps first unpublished message' do - remaining_messages = Models::Message.where(status: Models::Message::STATUS[:unpublished]) + remaining_messages = Models::Message.where(status: Models::Message::Status::UNPUBLISHED) expect(remaining_messages.count).to eq(1) remaining_message = remaining_messages.first diff --git a/spec/lib/outboxer/messageable_spec.rb b/spec/lib/outboxer/messageable_spec.rb index f05a5b17..9e0859e6 100644 --- a/spec/lib/outboxer/messageable_spec.rb +++ b/spec/lib/outboxer/messageable_spec.rb @@ -25,7 +25,7 @@ class Event < ActiveRecord::Base let(:outboxer_message) { event.outboxer_message } it 'creates outboxer message' do - expect(outboxer_message.status).to eq(Outboxer::Models::Message::STATUS[:unpublished]) + expect(outboxer_message.status).to eq(Outboxer::Models::Message::Status::UNPUBLISHED) expect(outboxer_message.messageable_type).to eq(event.class.to_s) expect(outboxer_message.messageable_id).to eq(event.id) expect(outboxer_message.created_at).to be_present diff --git a/spec/lib/outboxer/publisher/pop_message_spec.rb b/spec/lib/outboxer/publisher/pop_message_spec.rb index 500ee138..e8242515 100644 --- a/spec/lib/outboxer/publisher/pop_message_spec.rb +++ b/spec/lib/outboxer/publisher/pop_message_spec.rb @@ -11,7 +11,7 @@ module Outboxer Models::Message.create!( messageable_type: 'DummyType', messageable_id: 1, - status: Models::Message::STATUS[:publishing]) + status: Models::Message::Status::PUBLISHING) end before do @@ -40,7 +40,7 @@ module Outboxer Models::Message.create!( messageable_type: 'DummyType', messageable_id: 1, - status: 'publishing') + status: Models::Message::Status::PUBLISHING) end let(:error) { StandardError.new('processing error') } @@ -68,7 +68,7 @@ module Outboxer end updated_message = Models::Message.find(message.id) - expect(updated_message.status).to eq(Models::Message::STATUS[:failed]) + expect(updated_message.status).to eq(Models::Message::Status::FAILED) expect(updated_message.outboxer_exceptions.count).to eq(1) expect(updated_message.outboxer_exceptions.first.class_name).to eq(error.class.name) expect(updated_message.outboxer_exceptions.first.message_text).to eq(error.message) diff --git a/spec/lib/outboxer/publisher/publish_spec.rb b/spec/lib/outboxer/publisher/publish_spec.rb index 259eee02..cb782c74 100644 --- a/spec/lib/outboxer/publisher/publish_spec.rb +++ b/spec/lib/outboxer/publisher/publish_spec.rb @@ -17,7 +17,7 @@ module Outboxer Models::Message.create!( messageable_type: 'DummyType', messageable_id: 1, - status: Models::Message::STATUS[:unpublished]) + status: Models::Message::Status::UNPUBLISHED) end context 'when message published successfully' do @@ -31,7 +31,7 @@ module Outboxer ) do |publishing_message| expect(publishing_message.messageable_type).to eq('DummyType') expect(publishing_message.messageable_id).to eq(1) - expect(publishing_message.status).to eq(Models::Message::STATUS[:publishing]) + expect(publishing_message.status).to eq(Models::Message::Status::PUBLISHING) Publisher.stop! end diff --git a/spec/lib/outboxer/publisher/push_messages_spec.rb b/spec/lib/outboxer/publisher/push_messages_spec.rb index 6e19a2ed..e49fc964 100644 --- a/spec/lib/outboxer/publisher/push_messages_spec.rb +++ b/spec/lib/outboxer/publisher/push_messages_spec.rb @@ -15,7 +15,7 @@ module Outboxer Models::Message.create!( messageable_type: 'DummyType', messageable_id: 2, - status: Models::Message::STATUS[:unpublished]) + status: Models::Message::Status::UNPUBLISHED) end before do @@ -74,7 +74,7 @@ module Outboxer Models::Message.create!( messageable_type: 'DummyType', messageable_id: 1, - status: Models::Message::STATUS[:publishing]) + status: Models::Message::Status::PUBLISHING) end before do