Permalink
Browse files

Configuration revamped inc. renaming drivers to adapters

  • Loading branch information...
cavalle committed Jan 14, 2012
1 parent 3d2bc5c commit 0b762c7b2c277d3c5b57483fb1c417502784680e
Showing with 275 additions and 249 deletions.
  1. +2 −1 .gitignore
  2. +6 −8 README.md
  3. +1 −10 lib/eventwire.rb
  4. +11 −0 lib/eventwire/adapters.rb
  5. +1 −1 lib/eventwire/{drivers → adapters}/amqp.rb
  6. +1 −1 lib/eventwire/{drivers → adapters}/bunny.rb
  7. +2 −2 lib/eventwire/{drivers → adapters}/in_process.rb
  8. +1 −1 lib/eventwire/{drivers → adapters}/mongo.rb
  9. +1 −1 lib/eventwire/{drivers → adapters}/redis.rb
  10. +19 −28 lib/eventwire/configuration.rb
  11. +0 −11 lib/eventwire/drivers.rb
  12. +15 −8 lib/eventwire/middleware/error_handler.rb
  13. +12 −8 lib/eventwire/middleware/logger.rb
  14. +1 −1 lib/eventwire/subscriber.rb
  15. +6 −5 spec/acceptance/project_management_spec.rb
  16. +2 −2 spec/integration/{drivers/drivers_helper.rb → adapters/adapters_helper.rb}
  17. +6 −0 spec/integration/adapters/amqp_spec.rb
  18. +6 −0 spec/integration/adapters/bunny_spec.rb
  19. +5 −0 spec/integration/adapters/in_process_spec.rb
  20. +6 −0 spec/integration/adapters/mongo_spec.rb
  21. +6 −0 spec/integration/adapters/redis_spec.rb
  22. +0 −6 spec/integration/drivers/amqp_spec.rb
  23. +0 −6 spec/integration/drivers/bunny_spec.rb
  24. +0 −5 spec/integration/drivers/in_process_spec.rb
  25. +0 −6 spec/integration/drivers/mongo_spec.rb
  26. +0 −6 spec/integration/drivers/redis_spec.rb
  27. +1 −1 spec/spec_helper.rb
  28. +34 −0 spec/support/adapters.rb
  29. +0 −34 spec/support/drivers.rb
  30. +11 −1 spec/support/helpers.rb
  31. +105 −20 spec/unit/configuration_spec.rb
  32. +0 −59 spec/unit/eventwire_spec.rb
  33. +4 −4 spec/unit/middleware/error_handler_spec.rb
  34. +2 −2 spec/unit/middleware/logger_spec.rb
  35. +5 −7 spec/unit/publisher_spec.rb
  36. +3 −4 spec/unit/subscriber_spec.rb
View
@@ -5,4 +5,5 @@ pkg/*
doc/*
.yardoc
*.rdb
-.bin
+.bin
+*.swp
View
@@ -1,6 +1,5 @@
# Eventwire: Event Collaboration for the Masses[ ![Build Status](https://secure.travis-ci.org/cavalle/eventwire.png?branch=master)](http://travis-ci.org/cavalle/eventwire)
-
Eventwire is a generic and simple interface to various backends (AMQP, Redis, ZeroMQ, MongoDB) to help building event-driven systems
_**WARNING:** This gem is in a very early stage of development. No first version has been released yet. That means that some of things described in this file might not be implemented, and those which are, might not be production ready._
@@ -81,15 +80,14 @@ It provides two modules, `Eventwire::Publisher` and `Eventwire::Subscriber`, whi
See the code examples above.
-### Drivers
+### Adapters
-Committing to that simple generic interface, Eventwire includes various drivers for different backends. Developers can choose which one to use depending on what is available in their current infrastructure or what are their needs in terms of reliability, performance or scalability. Currently five drivers are provided:
+Committing to that simple generic interface, Eventwire includes various drivers for different backends. Developers can choose which one to use depending on what is available in their current infrastructure or what are their needs in terms of reliability, performance or scalability. Currently four drivers are provided:
-- `Eventwire::Driver::InProcess` (for testing and development mainly)
-- `Eventwire::Driver::AMQP` (default driver. Requires an AMQP server like RabbitMQ)
-- `Eventwire::Driver::Redis` (experimental. Requires a Redis server)
-- `Eventwire::Driver::Zero` (experimental. Uses ØMQ)
-- `Eventwire::Driver::Mongo` (experimental. Requires a MongoDB server)
+- `Eventwire::Adapter::InProcess` (for testing and development mainly)
+- `Eventwire::Adapter::AMQP` (default driver. Requires an AMQP server like RabbitMQ)
+- `Eventwire::Adapter::Redis` (experimental. Requires a Redis server)
+- `Eventwire::Adapter::Mongo` (experimental. Requires a MongoDB server)
### Workers
View
@@ -2,7 +2,7 @@
require 'eventwire/configuration'
require 'eventwire/publisher'
require 'eventwire/subscriber'
-require 'eventwire/drivers'
+require 'eventwire/adapters'
require 'eventwire/middleware'
module Eventwire
@@ -18,7 +18,6 @@ def configuration
end
def driver
- configuration.decorate unless configuration.decorated?
configuration.driver
end
@@ -29,14 +28,6 @@ def logger
def namespace
configuration.namespace
end
-
- def middleware
- configuration.middleware
- end
-
- def error_handler
- configuration.on_error
- end
def start_worker
driver.start
View
@@ -0,0 +1,11 @@
+require 'json'
+
+module Eventwire
+ module Adapters
+ autoload :InProcess, 'eventwire/adapters/in_process'
+ autoload :AMQP, 'eventwire/adapters/amqp'
+ autoload :Bunny, 'eventwire/adapters/bunny'
+ autoload :Redis, 'eventwire/adapters/redis'
+ autoload :Mongo, 'eventwire/adapters/mongo'
+ end
+end
@@ -1,7 +1,7 @@
require 'bunny'
require 'amqp'
-class Eventwire::Drivers::AMQP
+class Eventwire::Adapters::AMQP
def publish(event_name, event_data = nil)
Bunny.run do |mq|
mq.exchange(event_name.to_s, :type => :fanout).publish(event_data)
@@ -1,6 +1,6 @@
require 'bunny'
-class Eventwire::Drivers::Bunny
+class Eventwire::Adapters::Bunny
def publish(event_name, event_data = nil)
Bunny.run do |mq|
@@ -1,4 +1,4 @@
-class Eventwire::Drivers::InProcess
+class Eventwire::Adapters::InProcess
def initialize
@handlers = {}
end
@@ -20,4 +20,4 @@ def subscribe(event_name, handler_id, &handler)
def start; end
def stop; end
def purge; end
-end
+end
@@ -1,6 +1,6 @@
require 'mongo'
-class Eventwire::Drivers::Mongo
+class Eventwire::Adapters::Mongo
DB_NAME = 'broker'
def initialize
@@ -1,7 +1,7 @@
require 'redis'
require 'em-redis'
-class Eventwire::Drivers::Redis
+class Eventwire::Adapters::Redis
def initialize
@handlers = []
end
@@ -1,46 +1,37 @@
module Eventwire
class Configuration
- attr_reader :driver, :error_handler
-
- attr_accessor :middleware, :namespace, :logger
+ attr_reader :middleware, :error_handler
+ attr_accessor :namespace, :logger
def initialize
- @driver = Drivers::InProcess.new
- @logger = Logger.new(nil)
- @error_handler = lambda{|ex| }
- @middleware = [[Eventwire::Middleware::ErrorHandler, {:error_handler => error_handler, :logger => logger}],
- [Eventwire::Middleware::Logger, {:logger => logger}],
- Eventwire::Middleware::JSONSerializer,
- Eventwire::Middleware::DataObjects]
- @decorated = false
+ @adapter = Adapters::AMQP.new
+ @logger = Logger.new($stdout)
+ @error_handler = lambda {|ex| }
+ @middleware = [
+ [ Eventwire::Middleware::ErrorHandler, self ],
+ [ Eventwire::Middleware::Logger, self ],
+ Eventwire::Middleware::JSONSerializer,
+ Eventwire::Middleware::DataObjects
+ ]
end
- def driver=(driver)
- klass = Drivers.const_get(driver.to_sym) if driver.respond_to?(:to_sym)
- @driver = klass ? klass.new : driver
+ def adapter=(adapter)
+ klass = Adapters.const_get(adapter.to_sym) if adapter.respond_to?(:to_sym)
+ @adapter = klass ? klass.new : adapter
end
def on_error(&block)
@error_handler = block
end
- def decorated?
- !!@decorated
- end
-
- def decorate
- @decorated = true
- @driver = middleware.inject(driver) do |driver, args|
- args = Array(args)
+ def driver
+ @middleware.inject(@adapter) do |driver, args|
+ args = Array(args).clone
klass = args.shift
- if args && args.any?
- klass.new(driver, *args)
- else
- klass.new(driver)
- end
+ klass.new(driver, *args)
end
end
end
-end
+end
View
@@ -1,11 +0,0 @@
-require 'json'
-
-module Eventwire
- module Drivers
- autoload :InProcess, 'eventwire/drivers/in_process'
- autoload :AMQP, 'eventwire/drivers/amqp'
- autoload :Bunny, 'eventwire/drivers/bunny'
- autoload :Redis, 'eventwire/drivers/redis'
- autoload :Mongo, 'eventwire/drivers/mongo'
- end
-end
@@ -1,24 +1,31 @@
module Eventwire
module Middleware
class ErrorHandler < Base
-
- def initialize(app, options = {})
+ def initialize(app, config = nil)
super(app)
- @error_handler = options.delete(:error_handler) || lambda{|ex|}
- @logger = options.delete(:logger) || ::Logger.new(nil)
+ @config = config
end
def subscribe(event_name, handler_id, &handler)
@app.subscribe event_name, handler_id do |data|
begin
handler.call(data)
rescue Exception => ex
- @logger.error "\nAn error occurred: `#{ex.message}`\n#{ex.backtrace.join("\n")}\n"
- @error_handler.call(ex)
+ logger.error "\nAn error occurred: `#{ex.message}`\n#{ex.backtrace.join("\n")}\n"
+ error_handler.call(ex)
end
end
end
-
+
+ private
+
+ def error_handler
+ @config.error_handler
+ end
+
+ def logger
+ @config.logger
+ end
end
end
-end
+end
@@ -1,28 +1,32 @@
module Eventwire
module Middleware
class Logger < Base
-
- def initialize(app, options = {})
+ def initialize(app, config)
super(app)
- @logger = options.delete(:logger) || ::Logger.new(nil)
+ @config = config
end
def subscribe(event_name, handler_id, &handler)
@app.subscribe event_name, handler_id do |data|
begin
- @logger.info "Starting to process `#{event_name}` with handler `#{handler_id}` and data `#{data.inspect}`"
+ logger.info "Starting to process `#{event_name}` with handler `#{handler_id}` and data `#{data.inspect}`"
handler.call data
ensure
- @logger.info "End processing `#{event_name}`"
+ logger.info "End processing `#{event_name}`"
end
end
end
def publish(event_name, event_data = nil)
@app.publish event_name, event_data
- @logger.info "Event published `#{event_name}` with data `#{event_data.inspect}`"
+ logger.info "Event published `#{event_name}` with data `#{event_data.inspect}`"
end
-
+
+ private
+
+ def logger
+ @config.logger
+ end
end
end
-end
+end
@@ -36,4 +36,4 @@ def increment_handler_counter
end
end
end
-end
+end
@@ -3,15 +3,16 @@
describe 'Project Management System' do
- drivers = %w{InProcess AMQP Redis}
+ adapters = %w{InProcess AMQP Redis}
- drivers.each do |driver|
+ adapters.each do |adapter|
- with_driver driver do
+ with_adapter adapter do
before do
Eventwire.configure do |c|
- c.driver = driver
+ c.adapter = adapter
+ c.logger = Logger.new(nil)
end
load_environment
@@ -89,4 +90,4 @@ def purge
Eventwire.driver.purge
end
-end
+end
@@ -1,7 +1,7 @@
require 'spec_helper'
require 'timeout'
-shared_examples_for 'a driver with single-process support' do
+shared_examples_for 'a adapter with single-process support' do
def start_worker
@t = Thread.new { subject.start }
@@ -69,7 +69,7 @@ def start_worker
end
-shared_examples_for 'a driver with multi-process support' do
+shared_examples_for 'a adapter with multi-process support' do
before do
initialize_proccesses
@@ -0,0 +1,6 @@
+require 'integration/adapters/adapters_helper'
+
+describe_adapter Eventwire::Adapters::AMQP do
+ it_should_behave_like 'a adapter with single-process support'
+ it_should_behave_like 'a adapter with multi-process support'
+end
@@ -0,0 +1,6 @@
+require 'integration/adapters/adapters_helper'
+
+describe_adapter Eventwire::Adapters::Bunny do
+ it_should_behave_like 'a adapter with single-process support'
+ it_should_behave_like 'a adapter with multi-process support'
+end
@@ -0,0 +1,5 @@
+require 'integration/adapters/adapters_helper'
+
+describe_adapter Eventwire::Adapters::InProcess do
+ it_should_behave_like 'a adapter with single-process support'
+end
@@ -0,0 +1,6 @@
+require 'integration/adapters/adapters_helper'
+
+describe_adapter Eventwire::Adapters::Mongo do
+ it_should_behave_like 'a adapter with single-process support'
+ it_should_behave_like 'a adapter with multi-process support'
+end
@@ -0,0 +1,6 @@
+require 'integration/adapters/adapters_helper'
+
+describe_adapter Eventwire::Adapters::Redis do
+ it_should_behave_like 'a adapter with single-process support'
+ it_should_behave_like 'a adapter with multi-process support'
+end
@@ -1,6 +0,0 @@
-require 'integration/drivers/drivers_helper'
-
-describe_driver Eventwire::Drivers::AMQP do
- it_should_behave_like 'a driver with single-process support'
- it_should_behave_like 'a driver with multi-process support'
-end
@@ -1,6 +0,0 @@
-require 'integration/drivers/drivers_helper'
-
-describe_driver Eventwire::Drivers::Bunny do
- it_should_behave_like 'a driver with single-process support'
- it_should_behave_like 'a driver with multi-process support'
-end
@@ -1,5 +0,0 @@
-require 'integration/drivers/drivers_helper'
-
-describe_driver Eventwire::Drivers::InProcess do
- it_should_behave_like 'a driver with single-process support'
-end
Oops, something went wrong.

0 comments on commit 0b762c7

Please sign in to comment.