Skip to content

Commit 7d158ce

Browse files
Benjamin Erichsenjenkins
authored andcommitted
Add ability to register a jvm shutdown hook to close app
Add ability to register a jvm shutdown hook to close app Differential Revision: https://phabricator.twitter.biz/D1229286
1 parent 0c9cc53 commit 7d158ce

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

util-app/src/main/scala/com/twitter/app/App.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import java.util.concurrent.ConcurrentLinkedQueue
1010
import java.util.concurrent.atomic.AtomicReference
1111
import scala.collection.mutable
1212
import scala.jdk.CollectionConverters._
13+
import scala.sys.ShutdownHookThread
1314
import scala.util.control.NonFatal
15+
import com.twitter.finagle.util.enableJvmShutdownHook
1416

1517
/**
1618
* A composable application trait that includes flag parsing as well
@@ -444,6 +446,11 @@ trait App extends ClosableOnce with CloseOnceAwaitably with Lifecycle {
444446
catch {
445447
case _: NoSuchMethodException => None
446448
}
449+
if (enableJvmShutdownHook()) {
450+
ShutdownHookThread {
451+
Await.result(close(defaultCloseGracePeriod))
452+
}
453+
}
447454

448455
// Invoke main() if it exists.
449456
mainMethod.foreach { method =>

util-app/src/main/scala/com/twitter/finagle/util/flags.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.twitter.finagle.util
22

33
import com.twitter.app
4+
import com.twitter.app.GlobalFlag
45

56
/**
67
* A deny list of implementations to ignore. Keys are the fully qualified class names.
@@ -44,3 +45,12 @@ object loadServiceIgnoredPaths
4445
Seq.empty[String],
4546
"Additional packages to be excluded from recursive directory scan"
4647
)
48+
49+
/**
50+
* This flag enables another way for graceful shutdown.
51+
*/
52+
object enableJvmShutdownHook
53+
extends GlobalFlag[Boolean](
54+
false,
55+
"Registers a JVM shutdown hook which invokes close()"
56+
)

0 commit comments

Comments
 (0)