-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reorganized infrastructure code, particularly event buses, so that li…
…bzeromq is not required unless EventBus::Zero is used
- Loading branch information
Showing
16 changed files
with
186 additions
and
186 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
Rails.application.class.configure do | ||
config.event_bus = 'RedisEventBus' | ||
config.event_bus = 'EventBus::Redis' | ||
config.event_subscribers = %w{ClientReport ClientDetailsReport AccountDetailsReport MoneyTransferSaga} | ||
config.to_prepare { EventBus.init } | ||
end |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,11 @@ | ||
module EventBus | ||
class << self | ||
attr_accessor :current | ||
delegate :publish, :subscribe, :wait_for_events, :start, :purge, :stop, :to => :current | ||
|
||
def init | ||
EventBus.current = Rails.configuration.event_bus.constantize.new | ||
Rails.configuration.event_subscribers.each(&:constantize) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require 'carrot' | ||
require 'mq' | ||
|
||
class EventBus::AMQP | ||
def publish(event) | ||
Carrot.queue('events').publish(event.id) | ||
end | ||
|
||
def subscriptions(event_name) | ||
@subscriptions ||= Hash.new | ||
@subscriptions[event_name.to_s] ||= Set.new | ||
end | ||
|
||
def subscribe(event_name, &handler) | ||
subscriptions(event_name.to_s) << handler | ||
end | ||
|
||
def wait_for_events | ||
sleep(0.15) # next_tick | ||
end | ||
|
||
def purge | ||
Carrot.queue("events").purge | ||
end | ||
|
||
def start | ||
AMQP.start do | ||
MQ.new.queue("events").subscribe do |event_id| | ||
event = Event[event_id] | ||
subscriptions(event.name).each do |subscription| | ||
subscription.call(event) | ||
end | ||
end | ||
end | ||
end | ||
|
||
def stop | ||
AMQP.stop { EM.stop } | ||
wait_for_events | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class EventBus::InProcess | ||
def publish(event) | ||
subscriptions(event.name).each do |subscription| | ||
subscription.call(event) | ||
end | ||
end | ||
|
||
def subscriptions(event_name) | ||
@subscriptions ||= Hash.new | ||
@subscriptions[event_name] ||= Set.new | ||
end | ||
|
||
def subscribe(event_name, &handler) | ||
subscriptions(event_name) << handler | ||
end | ||
|
||
def wait_for_events; end | ||
def purge; end | ||
def start; end | ||
def stop; 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
class EventBus::Redis | ||
def publish(event) | ||
Redis.new.publish "events", event.id | ||
end | ||
|
||
def subscriptions(event_name) | ||
@subscriptions ||= Hash.new | ||
@subscriptions[event_name.to_s] ||= Set.new | ||
end | ||
|
||
def subscribe(event_name, &handler) | ||
subscriptions(event_name.to_s) << handler | ||
end | ||
|
||
def wait_for_events | ||
sleep(0.05) # next_tick | ||
end | ||
|
||
def purge | ||
Redis.new.del "events" | ||
end | ||
|
||
def start | ||
Redis.new.subscribe("events") do |on| | ||
on.message do |channel, event_id| | ||
event = Event[event_id] | ||
subscriptions(event.name).each do |subscription| | ||
subscription.call(event) | ||
end | ||
end | ||
end | ||
end | ||
|
||
def stop; end | ||
end |
Oops, something went wrong.