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:
parent
961a958d7b
commit
123bf2e647
1 changed files with 7 additions and 3 deletions
|
@ -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()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue