diff options
-rw-r--r-- | lib/tapicero.rb | 14 | ||||
-rw-r--r-- | lib/tapicero/user_database.rb | 53 | ||||
-rw-r--r-- | lib/tapicero/user_event_handler.rb | 44 | ||||
-rw-r--r-- | lib/tapicero_daemon.rb | 26 | ||||
-rw-r--r-- | test/test_helper.rb | 9 |
5 files changed, 91 insertions, 55 deletions
diff --git a/lib/tapicero.rb b/lib/tapicero.rb index 390257d..a098287 100644 --- a/lib/tapicero.rb +++ b/lib/tapicero.rb @@ -8,7 +8,11 @@ module Tapicero attr_accessor :config end - + # reraise exceptions instead of retrying + # used in tests + unless defined? RERAISE + RERAISE = false + end # # Load Config # this must come first, because CouchRest needs the connection @@ -24,12 +28,4 @@ module Tapicero puts "flags: #{FLAGS}" if FLAGS.any? end - # - # Load Tapicero Parts - # - require 'tapicero/user_database' - - def self.user_database(id) - UserDatabase.new(config.couch_host, config.options[:db_prefix] + id) - end end diff --git a/lib/tapicero/user_database.rb b/lib/tapicero/user_database.rb index 3cfdd70..26d013d 100644 --- a/lib/tapicero/user_database.rb +++ b/lib/tapicero/user_database.rb @@ -4,16 +4,8 @@ require 'json' module Tapicero class UserDatabase - def initialize(host, name) - @host = host - @name = name - end - - def prepare(config) - create - secure(config.options[:security]) - add_design_docs - Tapicero.logger.info "Prepared storage " + name + def initialize(user_id) + @db = couch.database(config.options[:db_prefix] + user_id) end def create @@ -22,7 +14,8 @@ module Tapicero end end - def secure(security) + def secure(security = nil) + security ||= config.options[:security] # let's not overwrite if we have a security doc already return if secured? && !Tapicero::FLAGS.include?('--overwrite-security') retry_request_once "Writing security to" do @@ -42,9 +35,9 @@ module Tapicero end def upload_design_doc(file) - url = design_url(file.basename('.json')) - CouchRest.put url, JSON.parse(file.read) - rescue RestClient::Conflict + old = CouchRest.get design_url(file.basename('.json')) + rescue RestClient::ResourceNotFound + CouchRest.put design_url(file.basename('.json')), JSON.parse(file.read) end @@ -54,28 +47,33 @@ module Tapicero end end + def name + db.name + end + protected def create_db - CouchRest.new(host).create_db(name) - rescue RestClient::PreconditionFailed # database already existed + db.info # test if db exists + rescue RestClient::ResourceNotFound + couch.create_db(db.name) end def delete_db - db = CouchRest.new(host).database(name) db.delete! if db rescue RestClient::ResourceNotFound # no database found end def retry_request_once(action) second_try ||= false - Tapicero.logger.debug "#{action} #{name}" + Tapicero.logger.debug "#{action} #{db.name}" yield rescue RestClient::Exception => exc + raise exc if Tapicero::RERAISE if second_try - log_error "#{action} #{name} failed twice due to:", exc + log_error "#{action} #{db.name} failed twice due to:", exc else - log_error "#{action} #{name} failed due to:", exc + log_error "#{action} #{db.name} failed due to:", exc sleep 5 second_try = true retry @@ -95,13 +93,22 @@ module Tapicero end def security_url - "#{host}/#{name}/_security" + db.root + "/_security" end def design_url(doc_name) - "#{host}/#{name}/_design/#{doc_name}" + db.root + "/_design/#{doc_name}" + end + + attr_reader :db + + def couch + @couch ||= CouchRest.new(config.couch_host) + end + + def config + Tapicero.config end - attr_reader :host, :name end end diff --git a/lib/tapicero/user_event_handler.rb b/lib/tapicero/user_event_handler.rb new file mode 100644 index 0000000..38cf8f8 --- /dev/null +++ b/lib/tapicero/user_event_handler.rb @@ -0,0 +1,44 @@ +require 'tapicero/user_database' +module Tapicero + class UserEventHandler + def initialize(users) + users.created do |hash| + prepare_db(hash['id']) + end + + # Sometimes changes log starts with rev 2. So the + # detection of is currently not working properly + # Working around this until a new version of + # couchrest changes takes this into account. + users.updated do |hash| + prepare_db(hash['id']) + end + + users.deleted do |hash| + destroy_db(hash['id']) + end + end + + protected + + def prepare_db(id) + db = user_database(id) + db.create + db.secure + db.add_design_docs + logger.info "Prepared storage " + db.name + end + + def destroy_db(id) + user_database(id).destroy + end + + def logger + Tapicero.logger + end + + def user_database(id) + UserDatabase.new(id) + end + end +end diff --git a/lib/tapicero_daemon.rb b/lib/tapicero_daemon.rb index 23248e3..89566de 100644 --- a/lib/tapicero_daemon.rb +++ b/lib/tapicero_daemon.rb @@ -4,30 +4,14 @@ # # Daemons.run('tapicero_daemon.rb') # - require 'tapicero' module Tapicero - users = CouchRest::Changes.new('users') - - users.created do |hash| - logger.debug "Created user " + hash['id'] - user_database(hash['id']).prepare(config) - end + module Daemon + require 'tapicero/user_event_handler' + users = CouchRest::Changes.new('users') + UserEventHandler.new(users) + users.listen - # Sometimes changes log starts with rev 2. So the - # detection of is currently not working properly - # Working around this until a new version of - # couchrest changes takes this into account. - users.updated do |hash| - logger.debug "Updated user " + hash['id'] - user_database(hash['id']).prepare(config) end - - users.deleted do |hash| - logger.debug "Deleted user " + hash['id'] - user_database(hash['id']).destroy - end - - users.listen end diff --git a/test/test_helper.rb b/test/test_helper.rb index da8b88c..5a3b509 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,13 +1,18 @@ require 'rubygems' require 'minitest/autorun' +require 'pathname' -BASE_DIR = File.expand_path('../..', __FILE__) -$:.unshift File.expand_path('lib', BASE_DIR) +unless defined? BASE_DIR + BASE_DIR = Pathname.new(__FILE__) + '../..' +end + +$:.unshift BASE_DIR + 'lib' require 'mocha/setup' require 'tapicero/version' Tapicero::CONFIGS << "test/config.yaml" +Tapicero::RERAISE = true require 'tapicero' |