Skip to content

Commit

Permalink
define Message::STATUSES as Status::X constants
Browse files Browse the repository at this point in the history
  • Loading branch information
bedrock-adam committed Feb 13, 2024
1 parent df51034 commit 8562624
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 45 deletions.
6 changes: 3 additions & 3 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions lib/outboxer/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)"
Expand All @@ -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,
Expand All @@ -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
Expand Down
22 changes: 13 additions & 9 deletions lib/outboxer/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions spec/lib/outboxer/message/failed_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ 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

let!(:failed_message) { Message.failed!(id: publishing_message.id, exception: exception) }

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

Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions spec/lib/outboxer/message/published_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
8 changes: 4 additions & 4 deletions spec/lib/outboxer/message/republish_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ 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

let!(:unpublished_message) { Message.republish!(id: failed_message.id) }

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

Expand All @@ -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

Expand Down
11 changes: 6 additions & 5 deletions spec/lib/outboxer/message/unpublished_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/outboxer/messageable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions spec/lib/outboxer/publisher/pop_message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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') }
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/outboxer/publisher/publish_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/outboxer/publisher/push_messages_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8562624

Please sign in to comment.