Guest User

Untitled

a guest
Oct 21st, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. trait JDBCer {
  2. this: AkkaAgentMySQLStateServiceComponent =>
  3.  
  4. val retryTimes = config int "db.extensions.retries" or 5
  5.  
  6. /**
  7. * Get a random slave connection name for Jeeves
  8. */
  9. def slaveDB(): Option[String] = {
  10. // Randomize List
  11. val random = Random.shuffle(mysqlStateService.getHealthyNodes())
  12.  
  13. // Check for the number of available healthy connections to MySQL
  14. if (random.size > 0) {
  15. // Pick the first one (random item)
  16. val connectionName = random(0)
  17.  
  18. // Log Debug
  19. please debug "Random Slave DB: " + connectionName
  20.  
  21. // Return Random Slave Connection
  22. Option(connectionName)
  23.  
  24. } else {
  25. // There are no healthy MySQL nodes available currently
  26. please warn "No healthy MySQL nodes available currently!"
  27. None
  28. }
  29. }
  30.  
  31. /**
  32. * Issue DB query multiple times, stops when successfull
  33. */
  34. def retriableQuery[A](queryFunc: Connection => A): A = {
  35. (retryTimes times) attempt {
  36. slaveDB() match {
  37. case Some(dbName) => {
  38. DB.withConnection(dbName) {
  39. implicit connection =>
  40. queryFunc(connection)
  41. }
  42. }
  43. case _ => {
  44. throw new IllegalStateException("No healthy MySQL nodes available currently!")
  45. }
  46. }
  47. }
  48. }
  49.  
  50. }
Add Comment
Please, Sign In to add comment