1

I created a scala App using Akka.

When i run it with : scala /Statistics.jar ./server.conf it works perfectly. but if I put it in a server or if I put it in a docker image, when i launch it the app start then stop directly after.

Here are my logs :

    ...
    [DEBUG] [02/01/2017 10:26:13.198] [webserver-akka.actor.default-dispatcher-11] [EventStream(akka://version)] logger log1-Slf4jLogger started
[DEBUG] [02/01/2017 10:26:13.185] [subscription-akka.actor.default-dispatcher-4] [akka://subscription/system] now supervising Actor[akka://subscription/system/log1-Slf4jLogger#731298157]
[DEBUG] [02/01/2017 10:26:13.201] [subscription-akka.actor.default-dispatcher-4] [akka://subscription/system] now watched by Actor[akka://subscription/]
[DEBUG] [02/01/2017 10:26:13.202] [webserver-akka.actor.default-dispatcher-11] [EventStream(akka://version)] Default Loggers started
[DEBUG] [02/01/2017 10:26:13.193] [version-akka.actor.default-dispatcher-2] [akka://version/system] now supervising Actor[akka://version/system/log1-Slf4jLogger#1075717908]
[DEBUG] [02/01/2017 10:26:13.206] [session-akka.actor.default-dispatcher-3] [akka://session/system/UnhandledMessageForwarder] started (akka.event.LoggingBus$$anonfun$startDefaultLoggers$2$$anon$3@1b5d5357)
[DEBUG] [02/01/2017 10:26:13.205] [webserver-akka.actor.default-dispatcher-13] [EventStream(akka://session)] Default Loggers started
[DEBUG] [02/01/2017 10:26:13.205] [webserver-akka.actor.default-dispatcher-8] [EventStream(akka://subscription)] Default Loggers started
[DEBUG] [02/01/2017 10:26:13.206] [subscription-akka.actor.default-dispatcher-4] [akka://subscription/system/UnhandledMessageForwarder] started (akka.event.LoggingBus$$anonfun$startDefaultLoggers$2$$anon$3@51839748)
[DEBUG] [02/01/2017 10:26:13.211] [subscription-akka.actor.default-dispatcher-2] [akka://subscription/system] now supervising Actor[akka://subscription/system/UnhandledMessageForwarder#2056207919]
serv http launch
[DEBUG] [02/01/2017 10:26:15.906] [webserver-akka.actor.default-dispatcher-8] [EventStream] shutting down: StandardOutLogger started
[DEBUG] [02/01/2017 10:26:15.909] [webserver-akka.actor.default-dispatcher-8] [EventStream] all default loggers stopped
[DEBUG] [02/01/2017 10:26:15.909] [webserver-akka.actor.default-dispatcher-3] [akka://webserver/system/log1-Slf4jLogger] stopped
[DEBUG] [02/01/2017 10:26:15.910] [webserver-akka.actor.default-dispatcher-7] [akka://webserver/system/IO-TCP] stopping
[DEBUG] [02/01/2017 10:26:15.910] [webserver-akka.actor.default-dispatcher-12] [akka://webserver/system/UnhandledMessageForwarder] stopped
[DEBUG] [02/01/2017 10:26:15.910] [webserver-akka.actor.default-dispatcher-16] [akka://webserver/system/IO-TCP/selectors] stopping
[DEBUG] [02/01/2017 10:26:15.910] [webserver-akka.actor.default-dispatcher-8] [akka://webserver/system] stopping
[DEBUG] [02/01/2017 10:26:15.910] [webserver-akka.actor.default-dispatcher-3] [akka://webserver/system/deadLetterListener] stopped
[DEBUG] [02/01/2017 10:26:15.910] [webserver-akka.actor.default-dispatcher-18] [akka://webserver/system/IO-TCP/selectors/$a] no longer watched by Actor[akka://webserver/system/IO-TCP/selectors#1324110335]
[DEBUG] [02/01/2017 10:26:15.910] [webserver-akka.actor.default-dispatcher-13] [akka://webserver/system/eventStreamUnsubscriber-1] stopped
[DEBUG] [02/01/2017 10:26:15.911] [webserver-akka.actor.default-dispatcher-18] [akka://webserver/system/IO-TCP/selectors/$a] stopped
[DEBUG] [02/01/2017 10:26:15.911] [webserver-akka.actor.default-dispatcher-13] [akka://webserver/system/IO-TCP/selectors] stopped
[DEBUG] [02/01/2017 10:26:15.912] [webserver-akka.actor.default-dispatcher-8] [akka://webserver/system/IO-TCP] stopped
[DEBUG] [02/01/2017 10:26:15.912] [webserver-akka.actor.default-dispatcher-13] [akka://webserver/system] stopped
[DEBUG] [02/01/2017 10:26:15.912] [webserver-akka.actor.default-dispatcher-18] [akka://webserver/] received AutoReceiveMessage Envelope(Terminated(Actor[akka://webserver/system]),Actor[akka://webserver/system])
[DEBUG] [02/01/2017 10:26:15.913] [webserver-akka.actor.default-dispatcher-18] [akka://webserver/] stopped

It seems I can't launch my app in background. How can i do for launching it in background?

here is my Main.scala:

object WebServer extends App {
    implicit val system = ActorSystem("webserver")
    implicit val materializer = ActorMaterializer()
    // needed for the future flatMap/onComplete in the end
    implicit val executionContext = system.dispatcher

    val ipServer = InetAddress.getLocalHost().getHostAddress()
    val configApplication = ConfigFactory.load("application")
    val serverFile = new File(args(0))
    val configServer = ConfigFactory.parseFile(serverFile)
 val routes =
   new AppActiveRoute(system.actorOf(AppActiveHandler.props(), "AppActiveHandler")).route
    val bindingFuture = Http().bindAndHandle(
        routes,
        ipServer,
        configApplication.getInt("http.port")
    )
    println("serv http launch")
    StdIn.readLine()
    bindingFuture
        .flatMap(_.unbind()) // trigger unbinding from the port
        .onComplete(_ => {
        cluster.close()
        system.terminate()
    })
    bindingFuture.onFailure {
        case ex: Exception =>
            println(ex, "Failed to bind to {}:{}!", ipServer, configApplication.getInt("http.port"))
    }
thomas poidevin
  • 176
  • 2
  • 20

1 Answers1

2

If your application is supposed to be dockerized and run in a container, you probably don't need this part

    StdIn.readLine()
    bindingFuture
        .flatMap(_.unbind()) // trigger unbinding from the port
        .onComplete(_ => {
        cluster.close()
        system.terminate()
    })

Try deleting it.

If you care to gracefully shutdown your actor system when the VM exits, you can use a shutdown hook, as specified in this answer.

Community
  • 1
  • 1
Stefano Bonetti
  • 8,973
  • 1
  • 25
  • 44
  • This will definitely work ! But it is curious that the code snippet comes right out from the official Akka HTTP tutorial and it really does have this issue of not blocking the readLine properly in deed in case you use SBT reStart or run it inside a webserver as described. The scala direct call works fine because it will block the readLine call. – Mário de Sá Vera Jan 08 '18 at 13:37