Search code examples
scalasbttaskactor

How to exit a sbt task?


I run a sbt task. Normal scenario after it finishes, it should stop in the terminal. But the terminal is not waiting for next input meaning : my task keeps running. I assume some called threads were not exited after the task launched them.

How to exit clean and kill all processes started by the sbt task ? I could debug which service was not stopped but it will take a long time to figure out.

More details

In the build.sbt

lazy val refreshTranslations = taskKey[Unit]("blabla")
fullRunTask(refreshTranslations, Compile, "tasks.TranslationTask")

In the task

object TranslationTask {

  def main(args: Array[String]): Unit = {
    // background stuff with ActorSystem() , AhcWSClient()
    Await.result(service.get, 20.seconds)
    ...
  }
}

Solution

  • I found a way to exit the ActorSystem after the SBT task finishes.

    In the build.sbt , start the task in the different thread.

    lazy val refreshTranslationsFallback = taskKey[Unit]("Refresh the translations file fallback")
    fullRunTask(refreshTranslationsFallback, Compile, "tasks.TranslationTask")
    fork in refreshTranslationsFallback := true
    

    In the task,

    object TranslationTask {
    
      def main(args: Array[String]): Unit = {
        // background stuff with ActorSystem() , AhcWSClient()
        ...
        scala.sys.exit() // important
      }
    }
    

    Special thanks to SBT stop run without exiting