-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add queue-based event pub to make sure events are published in order
- Loading branch information
1 parent
9b5f0f4
commit 58be6ab
Showing
6 changed files
with
123 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
module Sequent | ||
module Core | ||
class EventPublisher | ||
class PublishEventError < RuntimeError | ||
attr_reader :event_handler_class, :event | ||
|
||
def initialize(event_handler_class, event) | ||
@event_handler_class = event_handler_class | ||
@event = event | ||
end | ||
|
||
def message | ||
"Event Handler: #{@event_handler_class.inspect}\nEvent: #{@event.inspect}\nCause: #{cause.inspect}" | ||
end | ||
end | ||
|
||
def initialize | ||
@events_queue = Queue.new | ||
end | ||
def configuration | ||
Sequent.configuration | ||
end | ||
|
||
def publish_events(events) | ||
return if configuration.disable_event_handlers | ||
events.each { |event| @events_queue.push(event) } | ||
process_events | ||
end | ||
|
||
def process_events | ||
while(!@events_queue.empty?) do | ||
event = @events_queue.pop | ||
configuration.event_handlers.each do |handler| | ||
begin | ||
handler.handle_message event | ||
rescue | ||
raise PublishEventError.new(handler.class, event) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
require 'spec_helper' | ||
|
||
describe Sequent::Core::EventPublisher do | ||
class OtherAggregateTriggered < Sequent::Core::Event | ||
attrs other_aggregate_id: String | ||
end | ||
class EventAdded < Sequent::Core::Event; end | ||
class TriggerTestCase < Sequent::Core::Command; end | ||
class TriggerOtherAggregate < Sequent::Core::Command; end | ||
|
||
class TestAggregate < Sequent::Core::AggregateRoot | ||
def trigger_other_aggregate(aggregate_id) | ||
apply OtherAggregateTriggered, other_aggregate_id: aggregate_id | ||
end | ||
|
||
def add_event | ||
apply EventAdded | ||
end | ||
end | ||
|
||
class TestCommandHandler < Sequent::Core::BaseCommandHandler | ||
on TriggerTestCase do |command| | ||
agg1 = TestAggregate.new(aggregate_id: Sequent.new_uuid) | ||
agg2 = TestAggregate.new(aggregate_id: Sequent.new_uuid) | ||
|
||
agg1.trigger_other_aggregate(agg2.id) | ||
agg2.add_event | ||
|
||
repository.add_aggregate(agg1) | ||
repository.add_aggregate(agg2) | ||
end | ||
|
||
on TriggerOtherAggregate do |command| | ||
agg = repository.load_aggregate(command.aggregate_id) | ||
agg.add_event | ||
end | ||
end | ||
|
||
class TestWorkflow < Sequent::Core::Workflow | ||
on OtherAggregateTriggered do |event| | ||
execute_commands TriggerOtherAggregate.new(aggregate_id: event.other_aggregate_id) | ||
end | ||
end | ||
|
||
class TestEventHandler < Sequent::Core::BaseEventHandler | ||
def initialize(*args) | ||
@sequence_numbers = [] | ||
super(*args) | ||
end | ||
|
||
attr_reader :sequence_numbers | ||
|
||
on EventAdded do |event| | ||
@sequence_numbers << event.sequence_number | ||
end | ||
end | ||
|
||
it 'handles events in the proper order' do | ||
Sequent::Configuration.reset | ||
test_event_handler = TestEventHandler.new | ||
Sequent.configuration.event_handlers << TestWorkflow.new | ||
Sequent.configuration.event_handlers << test_event_handler | ||
Sequent.configuration.command_handlers << TestCommandHandler.new | ||
|
||
Sequent.command_service.execute_commands TriggerTestCase.new(aggregate_id: Sequent.new_uuid) | ||
|
||
expect(test_event_handler.sequence_numbers).to eq [1,2] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters