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

You need to sign in or sign up before continuing.
Commit ea4fa212 by Jan Aagaard Meier

Change pg connector manager to use promises correctly

1 parent f2d62129
...@@ -37,9 +37,9 @@ matrix: ...@@ -37,9 +37,9 @@ matrix:
fast_finish: true fast_finish: true
include: include:
- node_js: "0.12" - node_js: "0.11"
env: DB=mysql DIALECT=mysql env: DB=mysql DIALECT=mysql
allow_failures: allow_failures:
- node_js: "0.12" - node_js: "0.11"
env: DB=mysql DIALECT=mysql env: DB=mysql DIALECT=mysql
\ No newline at end of file
...@@ -64,13 +64,12 @@ module.exports = (function() { ...@@ -64,13 +64,12 @@ module.exports = (function() {
ConnectorManager.prototype.connect = function(callback) { ConnectorManager.prototype.connect = function(callback) {
var self = this 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 // in case database is slow to connect, prevent orphaning the client
// TODO: We really need some sort of queue/flush/drain mechanism // TODO: We really need some sort of queue/flush/drain mechanism
if (this.isConnecting && !this.pooling && this.client === null) { if (this.isConnecting && !this.pooling && this.client === null) {
emitter.emit('success', null) return resolve()
return emitter
} }
this.isConnecting = true this.isConnecting = true
...@@ -86,8 +85,13 @@ module.exports = (function() { ...@@ -86,8 +85,13 @@ module.exports = (function() {
config.reapIntervalMillis = this.config.pool.reapInterval || 1000 config.reapIntervalMillis = this.config.pool.reapInterval || 1000
config.uuid = this.config.uuid config.uuid = this.config.uuid
} }
var connectCallback = function(err, client, done) { var connectCallback = function(err, client, done) {
var timezoneCallback = function() {
self.isConnected = true
self.client = client
resolve(done)
}
self.isConnecting = false self.isConnecting = false
if (!!err) { if (!!err) {
...@@ -98,41 +102,36 @@ module.exports = (function() { ...@@ -98,41 +102,36 @@ module.exports = (function() {
if (err.code) { if (err.code) {
switch(err.code) { switch(err.code) {
case 'ECONNREFUSED': 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 break
case 'ENOTFOUND': case 'ENOTFOUND':
case 'EHOSTUNREACH': case 'EHOSTUNREACH':
case 'EINVAL': 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 break
default: default:
emitter.emit('error', err) reject(err)
break break
} }
} else { } else {
emitter.emit('error', new Error(err.message)) reject(new Error(err.message))
} }
} else if (client) { } else if (client) {
var timezoneCallback = function() {
self.isConnected = true
self.client = client
emitter.emit('success', done)
}
if (self.config.keepDefaultTimezone) { if (self.config.keepDefaultTimezone) {
Utils.tick(timezoneCallback) timezoneCallback()
} else { } else {
self.setTimezone(client, 'UTC', timezoneCallback) self.setTimezone(client, 'UTC', timezoneCallback)
} }
} else if (self.config.native) { } else if (self.config.native) {
self.setTimezone(self.client, 'UTC', function() { if (self.config.keepDefaultTimezone) {
self.isConnected = true timezoneCallback()
emitter.emit('success', done) } else {
}) self.setTimezone(self.client, 'UTC', timezoneCallback)
}
} else { } else {
done && done() done && done()
self.client = null self.client = null
emitter.emit('success') resolve()
} }
} }
...@@ -167,8 +166,7 @@ module.exports = (function() { ...@@ -167,8 +166,7 @@ module.exports = (function() {
}) })
} }
} }
}.bind(this))
return emitter
} }
ConnectorManager.prototype.setTimezone = function(client, timezone, callback) { 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!