3

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)
    ...
  }
}
Raymond Chenon
  • 11,482
  • 15
  • 77
  • 110
  • have you tried crtl + d in the terminal – Agbalaya Rasaq Jan 08 '17 at 21:30
  • 1
    I know how to exit in the terminal with cmd + C . I'm asking specifically in SBT. – Raymond Chenon Jan 08 '17 at 21:48
  • your task is running an externally defined function? Why not define it and run it as a regular Task? – marios Jan 08 '17 at 22:57
  • 1
    Is this related to this: http://stackoverflow.com/a/24600377/1553233 – marios Jan 08 '17 at 23:07
  • @marios , it seems like. But as 2017, it looks quite deprecated for SBT – Raymond Chenon Jan 09 '17 at 10:54
  • If you had a copy-pastable (best word ever?) example I could reproduce it and give it a shot. – marios Jan 09 '17 at 21:19
  • I put the example code already in my post. But yes , I am running a client using ActorSystem(). I don't know what's is not shutting down. So I'd like to kill the whole SBT process. – Raymond Chenon Jan 10 '17 at 11:15
  • 1
    akka ActorSystem uses non-daemon threads. You need to call [terminate](http://doc.akka.io/japi/akka/2.4/akka/actor/ActorSystem.html#terminate--) on the ActorSystem prior to JVM exit. Within sbt, you probably want this in a [shutdown hook](https://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html#addShutdownHook-java.lang.Thread-). – jkinkead Jan 11 '17 at 19:33

1 Answers1

0

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

Community
  • 1
  • 1
Raymond Chenon
  • 11,482
  • 15
  • 77
  • 110