File tree Expand file tree Collapse file tree
util-app/src/main/scala/com/twitter Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -10,7 +10,9 @@ import java.util.concurrent.ConcurrentLinkedQueue
1010import java .util .concurrent .atomic .AtomicReference
1111import scala .collection .mutable
1212import scala .jdk .CollectionConverters ._
13+ import scala .sys .ShutdownHookThread
1314import 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 =>
Original file line number Diff line number Diff line change 11package com .twitter .finagle .util
22
33import 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+ )
You can’t perform that action at this time.
0 commit comments