Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove IO dependency from DB module #987

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ object Main extends ResourceApp.Forever with StrictLogging {
val config: Config = Config.read
Config.log(config)

val xa: Resource[IO, Doobie.Transactor[IO]] = new DB(config.db).transactorResource.map(CorrelationId.correlationIdTransactor)
val xa: Resource[IO, Doobie.Transactor[IO]] = new DB[IO](config.db).transactorResource.map(CorrelationId.correlationIdTransactor)

/** Creating a resource which combines three resources in sequence:
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package com.softwaremill.bootzooka.infrastructure

import java.net.URI
import cats.effect.{IO, Resource}
import cats.effect.Resource
import cats.effect.kernel.Async
import cats.implicits._
import com.softwaremill.bootzooka.config.Sensitive
import com.softwaremill.bootzooka.infrastructure.Doobie._
import com.typesafe.scalalogging.StrictLogging
import doobie.hikari.HikariTransactor
import org.flywaydb.core.Flyway

import scala.concurrent.duration._
import Doobie._
import com.softwaremill.bootzooka.config.Sensitive
import com.softwaremill.macwire.autocats.autowire

import java.net.URI
import scala.concurrent.ExecutionContext
import scala.concurrent.duration._

/** Configures the database, setting up the connection pool and performing migrations. */
class DB(_config: DBConfig) extends StrictLogging {
class DB[F[_]: Async](_config: DBConfig) extends StrictLogging {

private val config: DBConfig = {
// on heroku, the url is passed without the jdbc: prefix, and with a different scheme
Expand All @@ -30,31 +30,33 @@ class DB(_config: DBConfig) extends StrictLogging {
} else _config
}

val transactorResource: Resource[IO, Transactor[IO]] = {
val transactorResource: Resource[F, Transactor[F]] = {
/*
* When running DB operations, there are three thread pools at play:
* (1) connectEC: this is a thread pool for awaiting connections to the database. There might be an arbitrary
* number of clients waiting for a connection, so this should be bounded.
*
* See also: https://tpolecat.github.io/doobie/docs/14-Managing-Connections.html#about-threading
*/
def buildTransactor(ec: ExecutionContext) = HikariTransactor.newHikariTransactor[IO](
def buildTransactor(ec: ExecutionContext) = HikariTransactor.newHikariTransactor[F](
config.driver,
config.url,
config.username,
config.password.value,
ec
)
autowire[Transactor[IO]](
doobie.util.ExecutionContexts.fixedThreadPool[IO](config.connectThreadPoolSize),
buildTransactor _
).evalTap(connectAndMigrate)

ExecutionContexts
.fixedThreadPool[F](config.connectThreadPoolSize)
.flatMap(buildTransactor)
.evalTap(connectAndMigrate)
}

private def connectAndMigrate(xa: Transactor[IO]): IO[Unit] = {
(migrate() >> testConnection(xa) >> IO(logger.info("Database migration & connection test complete"))).onError { e: Throwable =>
logger.warn("Database not available, waiting 5 seconds to retry...", e)
IO.sleep(5.seconds) >> connectAndMigrate(xa)
private def connectAndMigrate(xa: Transactor[F]): F[Unit] = {
(migrate() >> testConnection(xa) >> Async[F].delay(logger.info("Database migration & connection test complete"))).onError {
e: Throwable =>
logger.warn("Database not available, waiting 5 seconds to retry...", e)
Async[F].sleep(5.seconds) >> connectAndMigrate(xa)
}
}

Expand All @@ -65,14 +67,16 @@ class DB(_config: DBConfig) extends StrictLogging {
.load()
}

private def migrate(): IO[Unit] = {
private def migrate(): F[Unit] = {
if (config.migrateOnStart) {
IO(flyway.migrate()).void
} else IO.unit
Async[F].blocking {
flyway.migrate()
}.void
} else Async[F].unit
}

private def testConnection(xa: Transactor[IO]): IO[Unit] =
IO {
private def testConnection(xa: Transactor[F]): F[Unit] =
Async[F].blocking {
sql"select 1".query[Int].unique.transact(xa)
}.void
}