akka-http icon indicating copy to clipboard operation
akka-http copied to clipboard

Document how to keep running an akka-http server in production

Open richardimaoka opened this issue 7 years ago • 12 comments

There was a feedback in Twitter.

https://twitter.com/brianclapper/status/917148072718798848

those past techniques aren’t working w/ this version of Akka HTTP. Documentation for non-toy cases would be awesome. ;-)

https://twitter.com/jlprat/status/917149279399108608

Agree, readLine is just for toy cases. iirc, HttpApp listens to SIGTERM as well as readLine. Thanks for the feedback about docs! 😊

Doc samples use readline to wait, which is not recommended in production.

richardimaoka avatar Oct 09 '17 11:10 richardimaoka

Will work on it if no one else wants to pick this up in the next few days.

richardimaoka avatar Oct 09 '17 11:10 richardimaoka

FYI, since I'm the grumpy developer who lodged the complaint about the documentation :-/, what worked perfectly for me was one of these:

With HttpApp

Override waitForShutdownSignal() like this:

override protected def waitForShutdownSignal(system: ActorSystem)
                                            (implicit ec: ExecutionContext): Future[Done] = {
  Promise[Done].future
}

Rolling your own

Something like this:

val f = for { bindingFuture <- Http().bindAndHandle(routes, host, port)
              waitOnFuture  <- Promise[Done].future }
        yield waitOnFuture
sys.addShutdownHook { 
  // cleanup logic
}
Await.ready(f, Duration.Inf)

bmc avatar Oct 09 '17 16:10 bmc

This should talk about coordinated shutdown https://doc.akka.io/docs/akka/current/scala/actors.html#coordinated-shutdown

Though we're missing that bit still in HTTP actually: https://github.com/akka/akka-http/issues/1210

So I propose to solve #1210 and then document things :)

ktoso avatar Oct 10 '17 03:10 ktoso

I think just adding the documentation for creating a future that never completes would be very helpful in the short term. Having a future that never completes feels like I'm doing something wrong.

Also, I think the current default shutdown strategies have issues when using sbt-revolver. E.g. they start up and instantly shut down. Having a nice setup that works ok for production and with sbt-revolver would really help people get started fast.

Also a link to a page with other shutdown strategies would be excellent. For example with Tomcat, I believe you can shut down the server while not being the user that owns the process. I imagine a server could be shutdown via a config file changing, or a protected http endpoint being hit. Links to some strategies would really be nice.

LogicalTime avatar Jan 03 '18 18:01 LogicalTime

Prefer Future.never over Promise[Done].future as the latter will leak callbacks.

viktorklang avatar Jan 03 '18 21:01 viktorklang

Wasn't aware that existed. (Clearly, I need to make a regular practice of reading the Scala API docs front to back.) Thanks, @viktorklang.

bmc avatar Jan 03 '18 21:01 bmc

@bmc You're most welcome, Brian! :)

viktorklang avatar Jan 03 '18 22:01 viktorklang

Could someone post a snippet of the proper way to block/suspend the main thread when using akka-http from Java?

git4skatz avatar Jan 23 '18 20:01 git4skatz

It's shown in the above snippet already, please read this ticket

ktoso avatar Jan 24 '18 01:01 ktoso

What's shown above appear to me to be Scala, not Java. Also, the documentation recommends against overriding HttpApp (solution shown above) in favor of using Http.BindandHandle. Additionally, as someone new to Akka and Akka-Http a solution give in context would be valuable.

git4skatz avatar Jan 24 '18 01:01 git4skatz

Which is what this ticket is about... This ticket would be solved when we write this in the docs. We'd welcome community help in adding this to the docs, as signaled by the "help wanted" tag.

In Java you get a CompletionStage so it's plain Java operations to await on it.

toCompletableFuture() + get()

https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletionStage.html#toCompletableFuture-- https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html

ktoso avatar Jan 24 '18 01:01 ktoso

I'm an Akka newbie looking at the documentation on https://doc.akka.io/docs/akka-http/current/introduction.html?language=scala#. Agreed that waiting for the user to hit enter on StdIn is not ideal and a pointer to better approaches would be appreciated.

bwbecker avatar Aug 09 '22 20:08 bwbecker