不要怂,就是干,撸起袖子干!

Commit ea4fa212 by Jan Aagaard Meier

Change pg connector manager to use promises correctly

1 parent f2d62129
......@@ -37,9 +37,9 @@ matrix:
fast_finish: true
include:
- node_js: "0.12"
- node_js: "0.11"
env: DB=mysql DIALECT=mysql
allow_failures:
- node_js: "0.12"
- node_js: "0.11"
env: DB=mysql DIALECT=mysql
\ No newline at end of file
......@@ -64,13 +64,12 @@ module.exports = (function() {
ConnectorManager.prototype.connect = function(callback) {
var self = this
var emitter = new Utils.Promise();
return new Utils.Promise(function (resolve, reject) {
// in case database is slow to connect, prevent orphaning the client
// TODO: We really need some sort of queue/flush/drain mechanism
if (this.isConnecting && !this.pooling && this.client === null) {
emitter.emit('success', null)
return emitter
return resolve()
}
this.isConnecting = true
......@@ -86,8 +85,13 @@ module.exports = (function() {
config.reapIntervalMillis = this.config.pool.reapInterval || 1000
config.uuid = this.config.uuid
}
var connectCallback = function(err, client, done) {
var timezoneCallback = function() {
self.isConnected = true
self.client = client
resolve(done)
}
self.isConnecting = false
if (!!err) {
......@@ -98,41 +102,36 @@ module.exports = (function() {
if (err.code) {
switch(err.code) {
case 'ECONNREFUSED':
emitter.emit('error', new Error("Failed to authenticate for PostgresSQL. Please double check your settings."))
reject(new Error("Failed to authenticate for PostgresSQL. Please double check your settings."))
break
case 'ENOTFOUND':
case 'EHOSTUNREACH':
case 'EINVAL':
emitter.emit('error', new Error("Failed to find PostgresSQL server. Please double check your settings."))
reject(new Error("Failed to find PostgresSQL server. Please double check your settings."))
break
default:
emitter.emit('error', err)
reject(err)
break
}
} else {
emitter.emit('error', new Error(err.message))
reject(new Error(err.message))
}
} else if (client) {
var timezoneCallback = function() {
self.isConnected = true
self.client = client
emitter.emit('success', done)
}
if (self.config.keepDefaultTimezone) {
Utils.tick(timezoneCallback)
timezoneCallback()
} else {
self.setTimezone(client, 'UTC', timezoneCallback)
}
} else if (self.config.native) {
self.setTimezone(self.client, 'UTC', function() {
self.isConnected = true
emitter.emit('success', done)
})
if (self.config.keepDefaultTimezone) {
timezoneCallback()
} else {
self.setTimezone(self.client, 'UTC', timezoneCallback)
}
} else {
done && done()
self.client = null
emitter.emit('success')
resolve()
}
}
......@@ -167,8 +166,7 @@ module.exports = (function() {
})
}
}
return emitter
}.bind(this))
}
ConnectorManager.prototype.setTimezone = function(client, timezone, callback) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!