From a8194a21cb9f452adb77d24b60434f921c60d496 Mon Sep 17 00:00:00 2001 From: Azul Date: Tue, 4 Feb 2014 14:00:58 +0100 Subject: refactor: init user_database with a couchrest db --- lib/tapicero.rb | 2 +- lib/tapicero/user_database.rb | 42 ++++++++++++++++++++++++------------------ lib/tapicero_daemon.rb | 4 ++-- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/lib/tapicero.rb b/lib/tapicero.rb index 390257d..fd53d64 100644 --- a/lib/tapicero.rb +++ b/lib/tapicero.rb @@ -30,6 +30,6 @@ module Tapicero require 'tapicero/user_database' def self.user_database(id) - UserDatabase.new(config.couch_host, config.options[:db_prefix] + id) + UserDatabase.new(id) end end diff --git a/lib/tapicero/user_database.rb b/lib/tapicero/user_database.rb index 3cfdd70..d35af20 100644 --- a/lib/tapicero/user_database.rb +++ b/lib/tapicero/user_database.rb @@ -4,16 +4,15 @@ require 'json' module Tapicero class UserDatabase - def initialize(host, name) - @host = host - @name = name + def initialize(user_id) + @db = couch.database(config.options[:db_prefix] + user_id) end - def prepare(config) + def prepare create - secure(config.options[:security]) + secure add_design_docs - Tapicero.logger.info "Prepared storage " + name + Tapicero.logger.info "Prepared storage " + db.name end def create @@ -22,7 +21,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,8 +42,7 @@ module Tapicero end def upload_design_doc(file) - url = design_url(file.basename('.json')) - CouchRest.put url, JSON.parse(file.read) + CouchRest.put design_url(file.basename('.json')), JSON.parse(file.read) rescue RestClient::Conflict end @@ -57,25 +56,23 @@ module Tapicero protected def create_db - CouchRest.new(host).create_db(name) - rescue RestClient::PreconditionFailed # database already existed + db.create! || db.info 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 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 +92,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_daemon.rb b/lib/tapicero_daemon.rb index 23248e3..b9f22dc 100644 --- a/lib/tapicero_daemon.rb +++ b/lib/tapicero_daemon.rb @@ -12,7 +12,7 @@ module Tapicero users.created do |hash| logger.debug "Created user " + hash['id'] - user_database(hash['id']).prepare(config) + user_database(hash['id']).prepare end # Sometimes changes log starts with rev 2. So the @@ -21,7 +21,7 @@ module Tapicero # couchrest changes takes this into account. users.updated do |hash| logger.debug "Updated user " + hash['id'] - user_database(hash['id']).prepare(config) + user_database(hash['id']).prepare end users.deleted do |hash| -- cgit v1.2.3 From ecf7188143d09c091f89b1970269e04fc7cba4e8 Mon Sep 17 00:00:00 2001 From: Azul Date: Tue, 4 Feb 2014 14:14:54 +0100 Subject: refactor: separate user event handler --- lib/tapicero.rb | 8 -------- lib/tapicero/user_event_handler.rb | 33 +++++++++++++++++++++++++++++++++ lib/tapicero_daemon.rb | 26 +++++--------------------- 3 files changed, 38 insertions(+), 29 deletions(-) create mode 100644 lib/tapicero/user_event_handler.rb diff --git a/lib/tapicero.rb b/lib/tapicero.rb index fd53d64..1063b85 100644 --- a/lib/tapicero.rb +++ b/lib/tapicero.rb @@ -24,12 +24,4 @@ module Tapicero puts "flags: #{FLAGS}" if FLAGS.any? end - # - # Load Tapicero Parts - # - require 'tapicero/user_database' - - def self.user_database(id) - UserDatabase.new(id) - end end diff --git a/lib/tapicero/user_event_handler.rb b/lib/tapicero/user_event_handler.rb new file mode 100644 index 0000000..337cc06 --- /dev/null +++ b/lib/tapicero/user_event_handler.rb @@ -0,0 +1,33 @@ +require 'tapicero/user_database' +module Tapicero + class UserEventHandler + def initialize(users) + users.created do |hash| + logger.debug "Created user " + hash['id'] + user_database(hash['id']).prepare + 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| + logger.debug "Updated user " + hash['id'] + user_database(hash['id']).prepare + end + + users.deleted do |hash| + logger.debug "Deleted user " + hash['id'] + user_database(hash['id']).destroy + end + 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 b9f22dc..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 - 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 end - - users.deleted do |hash| - logger.debug "Deleted user " + hash['id'] - user_database(hash['id']).destroy - end - - users.listen end -- cgit v1.2.3 From 5cd3df1a9be8e7db2f3e364fd730ed78c8d0e49f Mon Sep 17 00:00:00 2001 From: Azul Date: Tue, 4 Feb 2014 15:01:27 +0100 Subject: refactor: moved prepare and destroy into user_event_handler --- lib/tapicero/user_database.rb | 11 ++++------- lib/tapicero/user_event_handler.rb | 21 +++++++++++++++------ 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/lib/tapicero/user_database.rb b/lib/tapicero/user_database.rb index d35af20..6daccf3 100644 --- a/lib/tapicero/user_database.rb +++ b/lib/tapicero/user_database.rb @@ -8,13 +8,6 @@ module Tapicero @db = couch.database(config.options[:db_prefix] + user_id) end - def prepare - create - secure - add_design_docs - Tapicero.logger.info "Prepared storage " + db.name - end - def create retry_request_once "Creating database" do create_db @@ -53,6 +46,10 @@ module Tapicero end end + def name + db.name + end + protected def create_db diff --git a/lib/tapicero/user_event_handler.rb b/lib/tapicero/user_event_handler.rb index 337cc06..d19e23d 100644 --- a/lib/tapicero/user_event_handler.rb +++ b/lib/tapicero/user_event_handler.rb @@ -3,8 +3,7 @@ module Tapicero class UserEventHandler def initialize(users) users.created do |hash| - logger.debug "Created user " + hash['id'] - user_database(hash['id']).prepare + prepare_db(hash['id']) end # Sometimes changes log starts with rev 2. So the @@ -12,16 +11,26 @@ module Tapicero # 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 + prepare_db(hash['id']) end users.deleted do |hash| - logger.debug "Deleted user " + hash['id'] - user_database(hash['id']).destroy + destroy_db(hash['id']) end end + 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 -- cgit v1.2.3 From 7bb4ab417c0275fcca03abe338b3b81c17b17a6e Mon Sep 17 00:00:00 2001 From: Azul Date: Tue, 4 Feb 2014 15:47:55 +0100 Subject: prevent 409s and 412 and reraise them in tests Including tests that ensure this --- lib/tapicero.rb | 6 +++++- lib/tapicero/user_database.rb | 8 ++++++-- lib/tapicero/user_event_handler.rb | 2 ++ test/test_helper.rb | 9 +++++++-- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/lib/tapicero.rb b/lib/tapicero.rb index 1063b85..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 diff --git a/lib/tapicero/user_database.rb b/lib/tapicero/user_database.rb index 6daccf3..26d013d 100644 --- a/lib/tapicero/user_database.rb +++ b/lib/tapicero/user_database.rb @@ -35,8 +35,9 @@ module Tapicero end def upload_design_doc(file) + old = CouchRest.get design_url(file.basename('.json')) + rescue RestClient::ResourceNotFound CouchRest.put design_url(file.basename('.json')), JSON.parse(file.read) - rescue RestClient::Conflict end @@ -53,7 +54,9 @@ module Tapicero protected def create_db - db.create! || db.info + db.info # test if db exists + rescue RestClient::ResourceNotFound + couch.create_db(db.name) end def delete_db @@ -66,6 +69,7 @@ module Tapicero Tapicero.logger.debug "#{action} #{db.name}" yield rescue RestClient::Exception => exc + raise exc if Tapicero::RERAISE if second_try log_error "#{action} #{db.name} failed twice due to:", exc else diff --git a/lib/tapicero/user_event_handler.rb b/lib/tapicero/user_event_handler.rb index d19e23d..38cf8f8 100644 --- a/lib/tapicero/user_event_handler.rb +++ b/lib/tapicero/user_event_handler.rb @@ -19,6 +19,8 @@ module Tapicero end end + protected + def prepare_db(id) db = user_database(id) db.create 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' -- cgit v1.2.3