title: Handle SIGTERM in akka-http
body: <p>The current (10.1.3) Akka HTTP docs: </p>
  
  <p><a href="https://doc.akka.io/docs/akka-http/current/server-side/graceful-termination.html" rel="nofollow noreferrer">https://doc.akka.io/docs/akka-http/current/server-side/graceful-termination.html</a></p>
  
  <p>talk about graceful termination, using this code sample:</p>
  
  <pre><code>import akka.actor.ActorSystem
  import akka.http.scaladsl.server.Directives._
  import akka.http.scaladsl.server.Route
  import akka.stream.ActorMaterializer
  import scala.concurrent.duration._
  
  implicit val system = ActorSystem()
  implicit val dispatcher = system.dispatcher
  implicit val materializer = ActorMaterializer()
  
  val routes = get {
    complete("Hello world!")
  }
  
  val binding: Future[Http.ServerBinding] =
      Http().bindAndHandle(routes, "127.0.0.1", 8080)
  
  // ...
  // once ready to terminate the server, invoke terminate:
  val onceAllConnectionsTerminated: Future[Http.HttpTerminated] =
  Await.result(binding, 10.seconds)
    .terminate(hardDeadline = 3.seconds)
  
  // once all connections are terminated,
  // - you can invoke coordinated shutdown to tear down the rest of the system:
  onceAllConnectionsTerminated.flatMap { _ ⇒
    system.terminate()
  }
  </code></pre>
  
  <p>I am wondering at what point this get called at, the comment states:</p>
  
  <pre><code>// once ready to terminate the server
  </code></pre>
  
  <p>What does this mean exactly, i.e. who/what determines the server is ready to terminate? </p>
  
  <p>Do I have to put the shutdown code above in some hook function somewhere so that it is invoked on Akka HTTP receiving a SIGTERM?</p>
  
  <p>I’ve tried putting this into the shutdown hook:</p>
  
  <pre><code>CoordinatedShutdown(system).addCancellableJvmShutdownHook{
    // once ready to terminate the server, invoke terminate:
    val onceAllConnectionsTerminated: Future[Http.HttpTerminated] =
    Await.result(binding, 10.seconds)
      .terminate(hardDeadline = 3.seconds)
  
    // once all connections are terminated,
    // - you can invoke coordinated shutdown to tear down the rest of the system:
    onceAllConnectionsTerminated.flatMap { _ ⇒
      system.terminate()
    }
  }
  </code></pre>
  
  <p>But requests in progress are ended immediately upon sending a SIGTERM (kill ), rather than completing.</p>
  
  <p>I also found a slightly different way of shutdown from <a href="https://github.com/akka/akka-http/issues/1210#issuecomment-338825745" rel="nofollow noreferrer">https://github.com/akka/akka-http/issues/1210#issuecomment-338825745</a>:</p>
  
  <pre><code>CoordinatedShutdown(system).addTask(
      CoordinatedShutdown.PhaseServiceUnbind, "http_shutdown") { () =&gt;
  
      bind.flatMap(_.unbind).flatMap { _ =&gt;
        Http().shutdownAllConnectionPools
      }.map { _ =&gt;
        Done
      }
    }
  </code></pre>
  
  <p>Maybe I should using this to handle SIGTERM? I'm not sure..</p>
  
  <p>Thanks!</p>