Commit 0f24e1e3
Changed files (5)
app
services
application
handlers
infrastructure
config
initializers
app/services/application/handlers/publish_cake_to_twitter.rb
@@ -1,7 +1,6 @@
class PublishCakeToTwitter
- def initialize(twitter_publisher, configuration = ENV)
+ def initialize(twitter_publisher)
@twitter = twitter_publisher
- @configuration = configuration
end
def handles?(event)
@@ -22,5 +21,5 @@ class PublishCakeToTwitter
"#{cake.name} By #{cake.user.name} on https://www.cakeside.com/creations/#{cake.to_param}!"
end
- handle_asynchronously :handle, :run_at => Proc.new { 1.hour.from_now }
+ handle_asynchronously :handle, run_at: Proc.new { 10.minutes.from_now }
end
app/services/infrastructure/environment_variables.rb
@@ -1,9 +1,5 @@
class EnvironmentVariables
- def initialize(configuration = ENV)
- @configuration = configuration
- end
-
def [](key)
- @configuration[key]
+ ENV[key]
end
end
app/services/infrastructure/message_bus.rb
@@ -1,17 +1,9 @@
class MessageBus
- def initialize(container)
- @container = container
+ def initialize(queue = Delayed::Job)
+ @queue = queue
end
def publish(event, payload)
- handlers_for(event).each { |handler| handler.handle(payload) }
- end
-
- private
-
- def handlers_for(event)
- @container.resolve_all(:message_handler).find_all do |handler|
- handler.handles?(event)
- end
+ @queue.enqueue(QueuedJob.new(event, payload))
end
end
app/services/infrastructure/queued_job.rb
@@ -0,0 +1,22 @@
+class QueuedJob < Struct.new(:event, :payload)
+ def perform
+ handlers_for(event).each { |handler| handler.handle(payload) }
+ end
+
+ def error(job, exception)
+ ExceptionNotifier.notify_exception(exception)
+ end
+
+ private
+
+ def handlers_for(event)
+ container.resolve_all(:message_handler).find_all do |handler|
+ handler.handles?(event)
+ end
+ end
+
+ def container
+ @container ||= Spank::IOC.resolve(:container)
+ end
+end
+
config/initializers/container.rb
@@ -1,7 +1,9 @@
container = Spank::Container.new
container.register(:configuration) { EnvironmentVariables.new }
container.register(:message_handler) { |builder| builder.build(PublishCakeToTwitter) }
-container.register(:message_bus) { |c| MessageBus.new(c) }.as_singleton
+
+container.register(:queue) { |c| Delayed::Job }
+container.register(:message_bus) { |c| c.build(MessageBus) }.as_singleton
container.register(:twitter_publisher) { |c| c.build(TwitterPublisher) }.as_singleton
Spank::IOC.bind_to(container)