Scala 在akka http中处理SIGTERM
当前(10.1.3)Akka HTTP文档: 使用以下代码示例讨论优雅终止:Scala 在akka http中处理SIGTERM,scala,akka,akka-http,Scala,Akka,Akka Http,当前(10.1.3)Akka HTTP文档: 使用以下代码示例讨论优雅终止: 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 =
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()
}
我想知道这是在什么时候,评论说:
// once ready to terminate the server
这到底意味着什么,即谁/什么决定服务器准备终止
我是否必须将上面的关闭代码放在某个钩子函数中,以便在接收SIGTERM的Akka HTTP上调用它
我已尝试将其放入关机挂钩:
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()
}
}
但是在发送SIGTERM(kill)时,正在进行的请求会立即结束,而不是完成
我还发现了一种与以下方式略有不同的关机方式:
也许我应该用这个来处理这个问题?我不确定
谢谢 根据以下答案得出的解决方案: 对我来说,主要部分是增加akka.coordinated-shutdown.default-phase-timeout,因为完成请求处理的时间比5秒的默认时间长。您还可以增加该阶段的超时时间。我的日志中有以下消息:
Coordinated shutdown phase [service-unbind] timed out after 5000 milliseconds
CoordinatedShutdown(system).addTask(
CoordinatedShutdown.PhaseServiceUnbind, "http_shutdown") { () =>
bind.flatMap(_.terminate(hardDeadline = 1.minute)).map { _ =>
Done
}
}
Coordinated shutdown phase [service-unbind] timed out after 5000 milliseconds