trait App extends ClosableOnce with CloseOnceAwaitably with Lifecycle
A composable application trait that includes flag parsing as well
as basic application lifecycle (pre- and post- main). Flag parsing
is done via com.twitter.app.Flags, an instance of which is
defined in the member flag
. Applications should be constructed
with modularity in mind, and common functionality should be
extracted into mixins.
Flags should only be constructed in the constructor, and should only be read in the premain or later, after they have been parsed.
import com.twitter.app.App object MyApp extends App { val n = flag("n", 100, "Number of items to process") def main(): Unit = { for (i <- 0 until n()) process(i) } }
Note that a missing main
is OK: mixins may provide behavior that
does not require defining a custom main
method.
- Alphabetic
- By Inheritance
- App
- Lifecycle
- CloseOnceAwaitably
- CloseOnceAwaitably0
- Awaitable
- ClosableOnce
- CloseOnce
- Closable
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##: Int
- Definition Classes
- AnyRef → Any
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final val MinGrace: Duration
Minimum duration to allow for exits to be processed.
- def allowUndefinedFlags: Boolean
Whether or not to accept undefined flags
Whether or not to accept undefined flags
- Attributes
- protected
- def args: Array[String]
The remaining, unparsed arguments
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native()
- final def close(deadline: Time): Future[Unit]
Close the resource with the given deadline.
- def close(after: Duration): Future[Unit]
Close the resource with the given timeout.
Close the resource with the given timeout. This timeout is advisory, giving the callee some leeway, for example to drain clients or finish up other tasks.
- Definition Classes
- Closable
- final def close(): Future[Unit]
Close the resource.
Close the resource. The returned Future is completed when the resource has been fully relinquished.
- Definition Classes
- Closable
- final def closeFuture: Future[Unit]
The Future satisfied upon completion of close.
- final def closeOnExit(closable: Closable): Unit
Close
closable
when shutdown is requested.Close
closable
when shutdown is requested. Closables are closed in parallel. - final def closeOnExitLast(closable: Closable): Unit
Register a
closable
to be closed on application shutdown after those registered viacloseOnExit
.Register a
closable
to be closed on application shutdown after those registered viacloseOnExit
.- Note
Application shutdown occurs in two sequential phases to allow explicit encoding of resource lifecycle relationships. Concretely this is useful for encoding that a monitoring resource should outlive a monitored resource. In all cases, the close deadline is enforced.
- def closeOnce(deadline: Time): Future[Unit]
Close the resource with the given deadline exactly once.
Close the resource with the given deadline exactly once. This deadline is advisory, giving the callee some leeway, for example to drain clients or finish up other tasks.
- def defaultCloseGracePeriod: Duration
Default amount of time to wait for shutdown.
Default amount of time to wait for shutdown. This value is not used as a default if
close()
is called without parameters. It simply provides a default value to be passed asclose(grace)
. - final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- def exitOnError(reason: String, details: => String): Unit
The details Fn may be an expensive operation (which could fail).
The details Fn may be an expensive operation (which could fail). We want to ensure that we've at least written the
reason
field to System.err before attempting to write thedetail
field so that users will at a minimum see thereason
for the exit regardless of any extra details.- Attributes
- protected
- def exitOnError(reason: String): Unit
Exit on error with the given
reason
StringExit on error with the given
reason
String- Attributes
- protected
- def exitOnError(throwable: Throwable): Unit
Exit on error with the given Throwable
Exit on error with the given Throwable
- Attributes
- protected
- def failfastOnFlagsNotParsed: Boolean
Users of this code should override this to
true
so that you fail-fast instead of being surprised at runtime by code that is reading from flags before they have been parsed.Users of this code should override this to
true
so that you fail-fast instead of being surprised at runtime by code that is reading from flags before they have been parsed.Ideally this would default to
true
, however, in order to avoid breaking existing users, it was introduced usingfalse
.- Attributes
- protected
- def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable])
- final def flag: Flags
The com.twitter.app.Flags instance associated with this application
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- def includeGlobalFlags: Boolean
Whether or not to include GlobalFlag's when Flags are parsed
Whether or not to include GlobalFlag's when Flags are parsed
- Attributes
- protected
- final def init(f: => Unit): Unit
Invoke
f
before anything else (including flag parsing).Invoke
f
before anything else (including flag parsing).- Attributes
- protected
- final def isClosed: Boolean
Signals whether or not this Closable has been closed.
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- def isReady(implicit permit: CanAwait): Boolean
Is this Awaitable ready? In other words: would calling Awaitable.ready block?
Is this Awaitable ready? In other words: would calling Awaitable.ready block?
- Definition Classes
- CloseOnceAwaitably0 → Awaitable
- def loadServiceBindings: Seq[Binding[_]]
Programmatically specify which implementations to use in
Programmatically specify which implementations to use in
LoadService.apply for the given interfaces. This allows applications to circumvent the standard service loading mechanism when needed. It may be useful if the application has a broad and/or rapidly changing set of dependencies.
For example, to require
SuperCoolMetrics
be used as thecom.twitter.finagle.stats.StatsReceiver
implementation:import com.initech.SuperCoolMetrics import com.twitter.app.App import com.twitter.app.LoadService.Binding import com.twitter.finagle.stats.StatsReceiver class MyApp extends App { val implementationToUse = new SuperCoolMetrics() override protected[this] val loadServiceBindings: Seq[Binding[_]] = { Seq(new Binding(classOf[StatsReceiver], implementationToUse)) } def main(): Unit = { val loaded = LoadService[StatsReceiver]() assert(Seq(implementationToUse) == loaded) } }
If this is called for a
Class[T]
after LoadService.apply has been called for that same interface, anIllegalStateException
will be thrown. For this reason, bindings are done as early as possible in the application lifecycle, before eveninits
and flag parsing.- returns
a mapping from
Class
to the 1 or more implementations to be used by LoadService.apply for that interface.
- Attributes
- protected[this]
- Note
this should not generally be used by "libraries" as it forces their user's implementation choice.
- See also
- final def main(args: Array[String]): Unit
- def name: String
The name of the application, based on the classname
- final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- final def nonExitingMain(args: Array[String]): Unit
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- final def observe(event: Event)(f: => Unit): Unit
Notifies
Observer
s ofEvent
s.Notifies
Observer
s ofEvent
s.- Attributes
- protected
- Definition Classes
- Lifecycle
- final def observeFuture(event: Event)(f: Future[Unit]): Future[Unit]
Notifies
Observer
s of com.twitter.util.FutureEvent
s.Notifies
Observer
s of com.twitter.util.FutureEvent
s.- Attributes
- protected
- Definition Classes
- Lifecycle
- final def onExit(f: => Unit): Unit
Invoke
f
when shutdown is requested.Invoke
f
when shutdown is requested. Exit hooks run in parallel and are executed after all postmains complete. The thread resumes when all exit hooks complete orcloseDeadline
expires.- Attributes
- protected
- See also
runOnExit(Runnable) for a variant that is more suitable for Java.
- final def onExitLast(f: => Unit): Unit
Invoke
f
when shutdown is requested.Invoke
f
when shutdown is requested. Exit hooks run in parallel and are executed after all closeOnExit functions complete. The thread resumes when all exit hooks complete orcloseDeadline
expires.- Attributes
- protected
- See also
runOnExitLast(Runnable) for a variant that is more suitable for Java.
- def parseArgs(args: Array[String]): Unit
Parse the command line arguments as an Array of Strings.
Parse the command line arguments as an Array of Strings. The default implementation parses the given String[] as Flag input values.
Users may override this method to specify different functionality.
- args
String Array which represents the command line input given to the application.
- Attributes
- protected[this]
- final def postmain(f: => Unit): Unit
Invoke
f
after the user's main has exited.Invoke
f
after the user's main has exited.- Attributes
- protected
- final def premain(f: => Unit): Unit
Invoke
f
right before the user's main is invoked.Invoke
f
right before the user's main is invoked.- Attributes
- protected
- def ready(timeout: Duration)(implicit permit: CanAwait): App.this.type
Support for
Await.ready
.Support for
Await.ready
. The use of the implicit permit is an access control mechanism: onlyAwait.ready
may call this method.- Definition Classes
- CloseOnceAwaitably0 → Awaitable
- def result(timeout: Duration)(implicit permit: CanAwait): Unit
Support for
Await.result
.Support for
Await.result
. The use of the implicit permit is an access control mechanism: onlyAwait.result
may call this method.- Definition Classes
- CloseOnceAwaitably0 → Awaitable
- final def runOnExit(runnable: Runnable): Unit
Invoke
runnable.run()
when shutdown is requested.Invoke
runnable.run()
when shutdown is requested. Exit hooks run in parallel and are executed after all postmains complete. The thread resumes when all exit hooks complete orcloseDeadline
expires.This is a Java friendly API to allow Java 8 users to invoke
onExit
with lambda expressions.For example (in Java):
runOnExit(() -> { clientA.close(); clientB.close(); });
- Attributes
- protected
- See also
=> Unit) for a variant that is more suitable for Scala.
- final def runOnExitLast(runnable: Runnable): Unit
Invoke
runnable.run()
when shutdown is requested.Invoke
runnable.run()
when shutdown is requested. Exit hooks run in parallel and are executed after all closeOnExit functions complete. The thread resumes when all exit hooks complete orcloseDeadline
expires.This is a Java friendly API to allow Java 8 users to invoke
onExitLast
with lambda expressions.For example (in Java):
runOnExitLast(() -> { clientA.close(); clientB.close(); });
- Attributes
- protected
- See also
=> Unit) for a variant that is more suitable for Scala.
- lazy val shutdownTimer: Timer
- Attributes
- protected
- def suppressGracefulShutdownErrors: Boolean
By default any failure during the graceful shutdown portion of an App's lifecycle bubbles up and causes non-zero return codes in the process.
By default any failure during the graceful shutdown portion of an App's lifecycle bubbles up and causes non-zero return codes in the process. Setting this to
false
allows an application to suppress these errors and express that graceful shutdown logic should not be a determinant of the process exit code.- Attributes
- protected
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def toString(): String
- Definition Classes
- AnyRef → Any
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()