1

I Have an issue when i try to write into a postgres table from an akka actor when it receives the message data. I dont't know exactly the real probem i think that is the async context, because when i call a function with some list data as params the function should write the list data into postgres, but instead of that nothing is executed, it does not mark any error only it does not execute the writing function. The data is from a rest enpoint and i use akka http to make the request.

code actor who handles writing to postgres. the code:

  object ClickUpTeamsActions {
    case class Fetch (writerRef: ActorRef)
    case class Write (teamsData: List[TeamClickUp] )
  }

  class ClickUpTeamsExtractor extends Actor with ActorLogging {
    import ClickUpTeamsActions._

    override def receive: Receive = {
      case Write(teamsData) =>
        println("-------writer")
        println(teamsData)
        val teamsPG: List[Teams] = teamsData.map(data => Teams(data.id, data.name, data.color, data.avatar))
        println(teamsPG)

          val bulkInsert = DoobieTest.insertMany(teamsPG) // function writes into postgres
          val io = bulkInsert.transact(DoobieTest.xa)
          io.unsafeRunSync() // this does not work
        
    }
  }

code is an object it has a function to write to postgres, this function is called from the actor receive:

import cats.effect.IO
import cats.implicits.catsStdInstancesForList
import doobie.{ConnectionIO, ExecutionContexts, Transactor, Update}

object DoobieTest  {

  import doobie.util.ExecutionContexts
  implicit val cs = IO.contextShift(ExecutionContexts.synchronous)

  val xa = Transactor.fromDriverManager[IO](
    "org.postgresql.Driver", // driver classname
    "jdbc:postgresql:zzzzzzz", // connect URL (driver-specific)
    "yyyyyyyyyyy", // user
    "XXXXXXXXXXXX", // password
  )

  def insertMany(ps: List[Teams]): ConnectionIO[Int] = {
    val sql = "insert into Teams (id, name, color, avatar) values ( ? , ? , ? , ?) ON CONFLICT(id) DO UPDATE SET name = EXCLUDED.name"
    Update[Teams](sql).updateMany(ps)
  }
}

The error is in:

// this code does not work when run on receive..
    val bulkInsert = DoobieTest.insertMany(teamsPG) // function writes into postgres
    val io = bulkInsert.transact(DoobieTest.xa)
    io.unsafeRunSync() // this does not work

    

if there is another way to do it , it could help.

javier_orta
  • 457
  • 4
  • 15

0 Answers0