Coder Social home page Coder Social logo

Postgres not ready about docker-it-scala HOT 6 CLOSED

fokot avatar fokot commented on July 25, 2024
Postgres not ready

from docker-it-scala.

Comments (6)

viktortnk avatar viktortnk commented on July 25, 2024

Yep, I had the same issue with both Mysql and Postgres. The problem that is log entry "database system is ready to accept connections" (for mysql case) doesn't actually mean that you can connect straight away. So I've got an extended version:

trait HasMysqlDatabase extends DockerTestKit with DockerMysqlService with BeforeAndAfterEach {
  self: Suite =>

  var mysqldb: Database = _

  def bootstrapSchema: Option[DefaultMySQLDriver.DDL] = None

  private val mysqlInitPc = PatienceConfig(Span(5, Seconds), Span(10, Millis))

  private def initDb(port: Int) = {
    val dbConfig =
      s"""
         |main = {
         |  connectionPool = "HikariCP"
         |  dataSourceClass = "com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
         |  properties = {
         |    url = "jdbc:mysql://${dockerExecutor.host}:$port/$MysqlDatabase",
         |    user = "$MysqlUser"
         |    password = "$MysqlPassword"
         |  }
         |  numThreads = 2
         |}
      """.stripMargin
    val db = Database.forConfig("main", ConfigFactory.parseString(dbConfig))
    RetryUtils.looped({
      db.run(sql"select 1".as[Int])
    }, 5, FiniteDuration(500, TimeUnit.MILLISECONDS)).map(_ => db)
  }

  override def beforeAll(): Unit = {
    super.beforeAll()
    val port = mysqlContainer.getPorts().futureValue(mysqlInitPc).apply(MysqlAdvertisedPort)
    mysqldb = initDb(port).futureValue(mysqlInitPc)
  }

  override protected def beforeEach(): Unit = {
    super.beforeEach()
    bootstrapSchema.foreach { schema =>
      mysqldb.run(schema.create).futureValue(mysqlInitPc)
    }
  }


  override protected def afterEach(): Unit = {
    try {
      bootstrapSchema.foreach { schema =>
        mysqldb.run(schema.drop).futureValue(mysqlInitPc)
      }
    } finally {
      super.afterEach()
    }
  }

  override def afterAll(): Unit = {
    Try(mysqldb.close())
    super.afterAll()
  }
}

from docker-it-scala.

viktortnk avatar viktortnk commented on July 25, 2024

I don't want to make the sample project to depend on Slick, but if somebody want to implement this with plain jdbc and send a PR, they are more than welcome

from docker-it-scala.

fokot avatar fokot commented on July 25, 2024

I created even simpler PostgresReadyChecker with pure java/scala libraries (only org.postgresql.Driver needs to be on path)

import java.sql.DriverManager

import com.whisk.docker.{DockerCommandExecutor, DockerContainerState, DockerReadyChecker}

import scala.concurrent.ExecutionContext
import scala.util.Try

class PostgresReadyChecker(dbname: String, username: String, password: String) extends DockerReadyChecker {

override def apply(container: DockerContainerState)(implicit docker: DockerCommandExecutor, ec: ExecutionContext) =
    container.getPorts().map(x => x.values.head).map(port =>
      Try {
        Class.forName("org.postgresql.Driver")
        val connection = DriverManager.getConnection(s"jdbc:postgresql://${docker.host}:$port/$dbname", username, password)
        if (connection != null) {
          connection.close()
          true
        } else {
          false
        }
      }.getOrElse(false)
    )

And then it can be used as loped checker

  val postgresContainer = DockerContainer("postgres:9.5")
    .withPorts((5432, Some(PostgresAdvertisedPort)))
    .withEnv(s"POSTGRES_USER=${db.getString("user")}", s"POSTGRES_PASSWORD=${db.getString("password")}")
    .withReadyChecker(
      new PostgresReadyChecker(DbName, db.getString("user"), db.getString("password")).looped(10, 1 second)
    )

Usually after 6-7 seconds it is started.

Code is here
https://github.com/fokot/docker-scala/blob/master/src/test/scala/fokot/docker/scala/service/PostgresReadyChecker.scala
and here
https://github.com/fokot/docker-scala/blob/master/src/test/scala/fokot/docker/scala/service/CatRepositorySlickTest.scala
I would do a PR but i don't know where (to which package) as I couldn't even find HasMysqlDatabase. So tell me and I can contribute :)

from docker-it-scala.

viktortnk avatar viktortnk commented on July 25, 2024

Cool, thanks. Could you please adding ability to pass port (but keep default).
Otherwise this code can easily fail on multiple port mapping:

container.getPorts().map(x => x.values.head)

Make a PR to samples module please. You can put ReadyChecked into that file
https://github.com/whisklabs/docker-it-scala/blob/master/samples/src/main/scala/com/whisk/docker/DockerPostgresService.scala

from docker-it-scala.

fokot avatar fokot commented on July 25, 2024

I needed to expose port because otherwise java couldn't access postgres other than in another container with link...

from docker-it-scala.

fokot avatar fokot commented on July 25, 2024

PR to https://github.com/whisklabs/docker-it-scala/blob/master/samples/src/main/scala/com/whisk/docker/DockerPostgresService.scala merged

from docker-it-scala.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.