Skip to content
Permalink
Browse files

Separate errors for db connection and ping (#1112)

  • Loading branch information...
keeferrourke committed Jul 23, 2019
1 parent 0d7e914 commit aa9d7fc64c0c687de8212fa681b4edfd48464db0
@@ -50,8 +50,8 @@ internal class SqsJobConsumer @Inject internal constructor(

for (i in (0 until config.concurrent_receivers_per_queue)) {
taskQueue.scheduleWithBackoff(Duration.ZERO) {
// Don't call handlers until all services are ready, otherwise handlers will crash because the
// services they might need (databases, etc.) won't be ready.
// Don't call handlers until all services are ready, otherwise handlers will crash because
// the services they might need (databases, etc.) won't be ready.
serviceManagerProvider.get().awaitHealthy()
receiver.runOnce()
}
@@ -27,23 +27,35 @@ class PingDatabaseService @Inject constructor(
val dataSource = DriverDataSource(
jdbcUrl, config.type.driverClassName, Properties(), config.username, config.password)
retry(10, ExponentialBackoff(Duration.ofMillis(20), Duration.ofMillis(1000))) {
dataSource.connection.use { c ->
try {
val result =
c.createStatement().executeQuery("SELECT 1 FROM dual").uniqueInt()
val connection = try {
dataSource.connect()
} catch (e: Exception) {
logger.error(e) { "failed to get a data source connection" }
throw RuntimeException("failed to get a data source connection $jdbcUrl", e)
}
try {
connection.use { c ->
val result = c.createStatement().executeQuery("SELECT 1 FROM dual").uniqueInt()
check(result == 1)
} catch (e: Exception) {
logger.error(e) { "error attempting to ping the database" }
val message = e.message
if (message != null && message.contains("table dual not found")) {
throw RuntimeException(
"Something is wrong with your vschema and unfortunately vtcombo does not " +
"currently have good error reporting on this. Please do an ocular inspection.",
e)
}
throw RuntimeException("Problem pinging url $jdbcUrl", e)
}
} catch (e: Exception) {
logger.error(e) { "error attempting to ping the database" }
throw RuntimeException(e.describe(jdbcUrl), e)
}
}
}

/** Kotlin thinks getConnection() is a val but it's really a function. */
@Suppress("UsePropertyAccessSyntax")
private fun DriverDataSource.connect() = getConnection()

private fun Exception.describe(jdbcUrl: String): String {
return when {
message?.contains("table dual not found") ?: false -> {
"Something is wrong with your vschema and unfortunately vtcombo does not " +
"currently have good error reporting on this. Please do an ocular inspection."
}
else -> "Problem pinging url $jdbcUrl"
}
}

0 comments on commit aa9d7fc

Please sign in to comment.
You can’t perform that action at this time.