diff --git a/lib/db/index.js b/lib/db/index.js index 216e5a77..7394d482 100644 --- a/lib/db/index.js +++ b/lib/db/index.js @@ -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()