1
0
Fork 0
mirror of https://github.com/openstf/stf synced 2025-10-06 03:50:04 +02:00

Automatically close database when lifecycle ends.

This commit is contained in:
Simo Kinnunen 2014-04-12 16:00:04 +09:00
parent 961a958d7b
commit 123bf2e647

View file

@ -3,8 +3,10 @@ var rutil = require('../util/rutil')
var logger = require('../util/logger')
var lifecycle = require('../util/lifecycle')
var db = module.exports = Object.create(null)
var log = logger.createLogger('db')
function connect() {
var log = logger.createLogger('db')
return rutil.connect({
host: process.env.RDB_HOST || 'localhost'
, port: process.env.RDB_PORT || 28015
@ -12,6 +14,10 @@ function connect() {
, authKey: process.env.RDB_AUTHKEY
})
.then(function(conn) {
lifecycle.observe(function() {
return db.close()
})
return conn.on('error', function(err) {
log.fatal('Connection error', err.stack)
lifecycle.fatal()
@ -23,8 +29,6 @@ function connect() {
})
}
var db = module.exports = Object.create(null)
// Export memoized connection as a Promise
db.connect = (function() {
var connection = connect()