Module: DB
- Includes:
- PactBroker::Logging
- Defined in:
- lib/db.rb
Constant Summary
Constants included from PactBroker::Logging
PactBroker::Logging::LOG_DIR, PactBroker::Logging::LOG_FILE_NAME
Class Method Summary collapse
- .configuration_for_env(env) ⇒ Object
-
.connect(db_credentials) ⇒ Object
Sequel by default does not test connections in its connection pool before handing them to a client.
- .connection_for_env(env) ⇒ Object
- .health_check ⇒ Object
Methods included from PactBroker::Logging
included, #log_error, #logger, #logger=
Class Method Details
.configuration_for_env(env) ⇒ Object
40 41 42 43 44 |
# File 'lib/db.rb', line 40 def self.configuration_for_env env database_yml = PactBroker.project_root.join('config','database.yml') config = YAML.load(ERB.new(File.read(database_yml)).result) config.fetch(env) end |
.connect(db_credentials) ⇒ Object
Sequel by default does not test connections in its connection pool before handing them to a client. To enable connection testing you need to load the “connection_validator” extension like below. The connection validator extension is configurable, by default it only checks connections once per hour:
sequel.rubyforge.org/rdoc-plugins/files/lib/sequel/extensions/connection_validator_rb.html
Because most of our applications so far are accessed infrequently, there is very little overhead in checking each connection when it is requested. This takes care of stale connections.
A gotcha here is that it is not enough to enable the “connection_validator” extension, we also need to specify that we want to use the threaded connection pool, as noted in the documentation for the extension.
27 28 29 30 31 32 33 |
# File 'lib/db.rb', line 27 def self.connect db_credentials con = Sequel.connect(db_credentials.merge(:logger => logger, :pool_class => Sequel::ThreadedConnectionPool, :encoding => 'utf8')) con.extension(:connection_validator) con.pool.connection_validation_timeout = -1 #Check the connection on every request con.timezone = :utc con end |
.connection_for_env(env) ⇒ Object
35 36 37 38 |
# File 'lib/db.rb', line 35 def self.connection_for_env env logger.info "Connecting to #{env} database." connect configuration_for_env(env) end |
.health_check ⇒ Object
48 49 50 51 52 |
# File 'lib/db.rb', line 48 def self.health_check PACT_BROKER_DB.synchronize do |c| c PACT_BROKER_DB.valid_connection? c end end |