Module: Moose::Inventory::DB
- Extended by:
- DB
- Included in:
- DB
- Defined in:
- lib/moose_inventory/db/db.rb,
lib/moose_inventory/db/models.rb,
lib/moose_inventory/db/exceptions.rb
Overview
Module for DB-related functionality
Defined Under Namespace
Classes: Group, Groupvar, Host, Hostvar, MooseDBException
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the value of attribute db.
-
#exceptions ⇒ Object
readonly
Returns the value of attribute exceptions.
-
#models ⇒ Object
readonly
Returns the value of attribute models.
Class Method Summary collapse
-
.init ⇒ Object
———————-.
-
.reset ⇒ Object
——————–.
-
.transaction ⇒ Object
——————–.
Instance Attribute Details
#db ⇒ Object (readonly)
Returns the value of attribute db.
19 20 21 |
# File 'lib/moose_inventory/db/db.rb', line 19 def db @db end |
#exceptions ⇒ Object (readonly)
Returns the value of attribute exceptions.
21 22 23 |
# File 'lib/moose_inventory/db/db.rb', line 21 def exceptions @exceptions end |
#models ⇒ Object (readonly)
Returns the value of attribute models.
20 21 22 |
# File 'lib/moose_inventory/db/db.rb', line 20 def models @models end |
Class Method Details
.init ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/moose_inventory/db/db.rb', line 24 def self.init # If we allow init more than once, then the db connection is remade, # which changes Sequel:DATABASES[0], thereby invalidating the sequel # models. This causes unexpected behavour. That is to say, because # of the way Sequel initializes models, this method is not idempotent. # In our single-shot application, this shouldn't be a problem. However, # our unit tests like to call init multiple times, which borks things. # So, we allow init only once, gated by whether @db is nil. In effect, # this means we pool the DB connection for the life of the application. # Again, not a problem for our one-shot app, but it may be an issue in # long-running code. Personally, I don't like this pooling regime - # perhaps I'm not understanding how it's supposed to be used? # # QUESTION: can the models be refreshed, to make then again valid? What if # we "load" instead of "require" the models? # ANSWER: Nope, still borks even if we use a load. # # @db = nil # <- fails for unit tests return unless @db.nil? # <- works for unit tests Sequel::Model.plugin :json_serializer connect create_tables # Make our models work Sequel::DATABASES[0] = @db require_relative 'models' # load( load_dir = File.join(File.dirname(__FILE__), "models.rb") ) # For convenience @models = {} @models[:host] = Moose::Inventory::DB::Host @models[:hostvar] = Moose::Inventory::DB::Hostvar @models[:group] = Moose::Inventory::DB::Group @models[:groupvar] = Moose::Inventory::DB::Groupvar @exceptions = {} @exceptions[:moose] = Moose::Inventory::DB::MooseDBException end |
.reset ⇒ Object
119 120 121 122 123 124 |
# File 'lib/moose_inventory/db/db.rb', line 119 def self.reset fail('Database connection has not been established') if @db.nil? # @debug << 'reset' purge create_tables end |
.transaction ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/moose_inventory/db/db.rb', line 66 def self.transaction fail('Database connection has not been established') if @db.nil? tries = 0 begin @db.transaction(savepoint: true) do yield end rescue Sequel::DatabaseError => e # We want to rescue Sqlite3::BusyException. But, sequel catches that # and re-raises it as Sequel::DatabaseError, with a message referencing # the original exception class # We look into e, to see whether it is a BusyException. If not, # we re-raise immediately. raise unless e..include?("BusyException") # Looks like a BusyException, so we retry, after a random delay. tries += 1 case tries when 1..10 if Moose::Inventory::Config._confopts[:trace] == true STDERR.puts e. end sleep rand() retry else warn('The database appears to be locked by another process, and '\ " did not become free after #{tries} tries. Giving up. ") # TODO: Some useful advice to the user, as to what to do about this error. raise end rescue @exceptions[:moose] => e warn 'An error occurred during a transaction, any changes have been rolled back.' if Moose::Inventory::Config._confopts[:trace] == true STDERR.puts $!.backtrace abort("ERROR: #{e}") else abort("ERROR: #{e.}") end rescue Exception => e warn 'An error occurred during a transaction, any changes have been rolled back.' raise e end end |