Skip to content

Instantly share code, notes, and snippets.

@feliperazeek
Created June 16, 2012 22:18
Show Gist options
  • Select an option

  • Save feliperazeek/2942668 to your computer and use it in GitHub Desktop.

Select an option

Save feliperazeek/2942668 to your computer and use it in GitHub Desktop.
trait JDBCer {
this: AkkaAgentMySQLStateServiceComponent =>
val retryTimes = config int "db.extensions.retries" or 5
/**
* Get a random slave connection name for Jeeves
*/
def slaveDB(): Option[String] = {
// Randomize List
val random = Random.shuffle(mysqlStateService.getHealthyNodes())
// Check for the number of available healthy connections to MySQL
if (random.size > 0) {
// Pick the first one (random item)
val connectionName = random(0)
// Log Debug
please debug "Random Slave DB: " + connectionName
// Return Random Slave Connection
Option(connectionName)
} else {
// There are no healthy MySQL nodes available currently
please warn "No healthy MySQL nodes available currently!"
None
}
}
/**
* Issue DB query multiple times, stops when successfull
*/
def retriableQuery[A](queryFunc: Connection => A): A = {
(retryTimes times) attempt {
slaveDB() match {
case Some(dbName) => {
DB.withConnection(dbName) {
implicit connection =>
queryFunc(connection)
}
}
case _ => {
throw new IllegalStateException("No healthy MySQL nodes available currently!")
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment