{"id":5792,"date":"2018-10-01T01:37:44","date_gmt":"2018-10-01T01:37:44","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=5792"},"modified":"2018-10-01T01:37:44","modified_gmt":"2018-10-01T01:37:44","slug":"use-cats-effects-doobie-run-connect-postgres-docker-image","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/use-cats-effects-doobie-run-connect-postgres-docker-image\/","title":{"rendered":"Use Cats Effects, Doobie to run and connect to a Postgres Docker image"},"content":{"rendered":"<p>The following program will run a Docker image of Postgres, given a password. It will wait 5 seconds, and then connect using Doobie JDBC:<\/p>\n<pre lang=\"scala\">\nimplicit val timer = IO.timer(ExecutionContext.global)\n\n  val info = (msg: String) => println(\"OUT \" + msg)\n  val err = (msg: String) => println(\"ERR \" + msg)\n\n  def putStrlLn(value: String) = IO(println(value))\n  val readLn = IO(scala.io.StdIn.readLine)\n\n  def cmd(cmd: String, runId: String): IO[Int] = {\n\n    IO.cancelable(\n      (cb: (Either[Throwable, Int] => Unit)) => {\n        val isCancelled = new AtomicBoolean(false)\n\n        var process: Option[Process] = None\n\n        val asyncResult = Future {\n          import sys.process._\n          info(s\"${runId} Running `${cmd}`:\")\n\n          val log = ProcessLogger(\n            (msg) => info(s\"${runId}   ${msg}\"),\n            (msg) => err(s\"${runId}   ${msg}\")\n          )\n\n          val proc = Process(cmd)\n          process = Some(proc.run(log))\n\n          process.get.exitValue()\n        }\n\n        asyncResult.onComplete {\n          case Success(value) => cb(Right(value))\n          case Failure(e) => cb(Left(e))\n        }\n\n        IO {\n          isCancelled.set(true)\n\n          process match {\n            case Some(process) => process.destroy()\n            case None => {\n              info(\"No process to cancel\")\n            }\n          }\n\n          info(\"# # # set isCancelled = true\")\n        }\n      }\n    )\n  }\n\n  def docker(id: String, params: String, runId: String) = cmd(s\"docker run --rm -i ${params} ${id}\", runId)\n\n  def main(args: Array[String]): Unit = {\n    val contextShift = IO.contextShift(global)\n\n    import doobie._\n    import doobie.implicits._\n\n    val xa = Transactor.fromDriverManager[IO](\n      \"org.postgresql.Driver\", \"jdbc:postgresql:\/\/localhost:6432\/postgres\", \"postgres\", \"pwd\"\n    )\n\n    val program = for (\n      runId <- FUUID.randomFUUID[IO];\n      IO.race(\n        docker(\"postgres\", \"-p 6432:5432 -e POSTGRES_PASSWORD=pwd\", runId.show),\n        IO.sleep(5 seconds) *> sql\"select 42\".query[Int].unique.transact(xa).flatMap(\n          x => IO({\n            println(x)\n          })\n        )\n      )(contextShift) *>\n      IO({\n        println(\"success\")\n      })) yield program\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Example of using Doobie, Cats Effects, Postgres in Docker<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[102,176,244,437,438,480],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/5792"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=5792"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/5792\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=5792"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=5792"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=5792"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}