Permalink
Browse files

Better logging and configuration. Closes #1

  • Loading branch information...
1 parent 16471a5 commit 4278f48bc8c01f8cdc4d81f5646ccf260cfd771e @guiocavalcanti guiocavalcanti committed Oct 23, 2012
Showing with 71 additions and 48 deletions.
  1. +1 −0 Gemfile
  2. +6 −0 Gemfile.lock
  3. +9 −9 config/server.rb
  4. +1 −1 examples/inserting.rb
  5. +22 −4 lib/permit/config.rb
  6. +13 −9 lib/permit/connection.rb
  7. +3 −11 lib/permit/worker.rb
  8. +13 −0 spec/config_spec.rb
  9. +3 −13 spec/consumer_spec.rb
  10. +0 −1 worker.rb
View
@@ -9,6 +9,7 @@ gem 'bson_ext'
gem 'amqp'
gem 'yajl-ruby'
gem 'rake'
+gem 'configurable'
group :development, :test do
gem 'rspec'
View
@@ -29,6 +29,10 @@ GEM
bson_ext (1.7.0)
bson (~> 1.7.0)
columnize (0.3.6)
+ config_parser (0.5.5)
+ configurable (1.0.0)
+ config_parser (~> 0.5.4)
+ lazydoc (~> 1.0)
debugger (1.2.0)
columnize (>= 0.3.1)
debugger-linecache (~> 1.1.1)
@@ -61,6 +65,7 @@ GEM
rack-respond_to
hashie (1.2.0)
http_parser.rb (0.5.3)
+ lazydoc (1.0)
log4r (1.1.10)
multi_json (1.3.6)
multi_xml (0.5.1)
@@ -92,6 +97,7 @@ PLATFORMS
DEPENDENCIES
amqp
bson_ext
+ configurable
debugger
em-mongo
em-synchrony
View
@@ -1,14 +1,14 @@
require 'em-synchrony/em-mongo'
-Permit::Config.logger
+$stdout.sync = true
-db_conf = {
- :host => ENV['MONGO_HOST'] || "127.0.0.1",
- :port => ENV['MONGO_PORT'] || "27017",
- :user => ENV['MONGO_USER'],
- :pass => ENV['MONGO_PASS'],
- :db_name => ENV['MONGO_DB_NAME'] || "permit_#{Goliath.env}"
-}
+Permit.configure do |c|
+ if Goliath.env?(:devel) || Goliath.env?(:test)
+ dirname = "#{File.dirname(__FILE__)}/logs"
+ File.mkdir dirname unless File.exists?(dirname)
+ c.logger = Logger.new("#{dirname}/#{Goliath.env}.log")
+ end
+end
-Permit::Connection.establish_connections(db_conf, 3)
+Permit::Connection.establish_connections
config['db'] = Permit::Connection.pool
View
@@ -3,7 +3,7 @@
require 'policy'
EM.synchrony do
- Permit::Connection.establish_connections({}, 1, 'development')
+ Permit::Connection.establish_connections
p = Permit::Policy.new(:resource_id => 'r',
:collection => Permit::Connection.pool.collection('rules'))
p.insert(:subject_id => 's', :actions => { :foo => 'bar' })
View
@@ -1,8 +1,26 @@
+require 'singleton'
+require 'configurable'
+require 'logger'
+
module Permit
+ def self.configure(&block)
+ yield(config)
+ end
+
+ def self.config
+ @config ||= Config.new
+ end
+
class Config
- def self.logger(logger=nil)
- l = logger || Logger.new("logs/#{Goliath.env}.log")
- @@logger ||= l
- end
+ include Configurable
+ config :logger, Logger.new(STDOUT)
+ config :db, {
+ :host => ENV['MONGO_HOST'] || "127.0.0.1",
+ :port => ENV['MONGO_PORT'] || "27017",
+ :user => ENV['MONGO_USER'],
+ :pass => ENV['MONGO_PASS'],
+ :db_name => ENV['MONGO_DB_NAME'] || "permit_#{Goliath.env}" || "permit_development",
+ :pool_size => 3
+ }
end
end
View
@@ -1,20 +1,24 @@
module Permit
class Connection
- def self.establish_connections(db_conf, pool_size, environment=Goliath.env)
- @host = db_conf[:host]
- @port = db_conf[:port]
- @db_name = db_conf[:db_name]
+ def self.establish_connections
+ @host = Permit.config.db.host
+ @port = Permit.config.db.port
+ @db_name = Permit.config.db.db_name
@opts = { :recconnect_in => 1 }
- @opts[:user] = db_conf[:user] if db_conf[:user]
- @opts[:password] = db_conf[:pass] if db_conf[:pass]
+ @user = Permit.config.db.user if Permit.config.db.user
+ @password = Permit.config.db.pass if Permit.config.db.pass
- @@connections = EM::Synchrony::ConnectionPool.new(:size => pool_size) do
- conn = EM::Mongo::Connection.new(@host, @port, 1, @opts).db(@db_name)
+ @@conns = \
+ EM::Synchrony::ConnectionPool.new(:size => Permit.config.db.pool_size) do
+ conn = EM::Mongo::Connection.new(@host, @port, 1, @opts)
+ db = conn.db(@db_name)
+ db.authenticate(@user, @password) if @user && @password
+ db
end
end
def self.pool
- @@connections
+ @@conns
end
end
end
View
@@ -6,22 +6,14 @@ def initialize(opts)
@consumer = opts[:consumer] || Consumer.new
@exchange = opts[:exchange]
- db_conf = {
- :host => ENV['MONGO_HOST'] || "127.0.0.1",
- :port => ENV['MONGO_PORT'] || "27017",
- :user => ENV['MONGO_USER'],
- :pass => ENV['MONGO_PASS'],
- :db_name => ENV['MONGO_DB_NAME'] || "permit_#{Goliath.env}"
- }
-
- Permit::Connection.establish_connections(1)
- Config.logger.info "Worker initialized and listening"
+ Permit::Connection.establish_connections
+ Permit.config.logger.info "Worker initialized and listening"
end
def start
@channel.queue(@queue_name, :exclusive => true) do |queue|
queue.bind(@exchange, :routing_key => "permit.#").subscribe do |h,p|
- Config.logger.info "Message #{p} with headers #{h} arrived"
+ Permit.config.logger.info "Message #{p} with headers #{h} arrived"
p = JSON.parse(p, :symbolize_keys => true)
@consumer.call(h,p)
end
View
@@ -0,0 +1,13 @@
+require 'spec_helper'
+
+module Permit
+ describe Config do
+ let(:subject) do
+ Config.new
+ end
+
+ it "should be initialied when calling Permit.config" do
+ Permit.config.should be_a Config
+ end
+ end
+end
View
@@ -2,16 +2,6 @@
module Permit
describe Consumer do
- let(:db_conf) do
- db_conf = {
- :host => ENV['MONGO_HOST'] || "127.0.0.1",
- :port => ENV['MONGO_PORT'] || "27017",
- :user => ENV['MONGO_USER'],
- :pass => ENV['MONGO_PASS'],
- :db_name => ENV['MONGO_DB_NAME'] || "permit_#{Goliath.env}"
- }
- end
-
it "should respondo to #call" do
EventMachine.synchrony do
Permit::Consumer.new.should respond_to :call
@@ -21,7 +11,7 @@ module Permit
it "should insert rules" do
EventMachine.synchrony do
- Permit::Connection.establish_connections(db_conf, 1)
+ Permit::Connection.establish_connections
rules = Permit::Connection.pool.collection('rules')
rules.remove({})
rule = \
@@ -36,7 +26,7 @@ module Permit
it "should insert multiple actions" do
EventMachine.synchrony do
- Permit::Connection.establish_connections(db_conf, 1)
+ Permit::Connection.establish_connections
rules = Permit::Connection.pool.collection('rules')
rules.remove({})
rule = \
@@ -52,7 +42,7 @@ module Permit
it "should insert the rules correctly" do
EventMachine.synchrony do
- Permit::Connection.establish_connections(db_conf, 1)
+ Permit::Connection.establish_connections
rules = Permit::Connection.pool.collection('rules')
rules.remove({})
r = { :resource_id => 'r', :subject_id => 's', :actions => {:read => true} }
View
@@ -8,7 +8,6 @@
Goliath.env = :development
channel = AMQP::Channel.new(connection)
exchange = channel.topic("permit", :auto_delete => true)
- Permit::Config.logger(Logger.new(STDOUT))
consumer = Permit::Consumer.new
worker = Permit::Worker.new(:consumer => consumer, :channel => channel,
:exchange => exchange)

0 comments on commit 4278f48

Please sign in to comment.