twitter / finatra

Fast, testable, Scala services built on TwitterServer and Finagle
https://twitter.github.io/finatra/
Apache License 2.0
2.27k stars 405 forks source link

Where is closeOnExit #577

Closed telemat closed 2 years ago

telemat commented 2 years ago

In the documentation at https://twitter.github.io/finatra/user-guide/getting-started/modules.html#module-lifecycle, it is not clear how to import closeOnExit to be used inline for closing resources.

cacoco commented 2 years ago

Hi @telemat in the linked document, if you scroll down just a bit it shows how closeOnExit can be used by detailing an example TwitterModule extension. FWIW, is not necessarily something to import as it is a function available on any subclass of TwitterModule. So to answer the underlying question, you extend TwitterModule and then you can call closeOnExit to register a callback that will be executed when the server is closed.

The example code:

import com.google.inject.Provides
import com.twitter.conversions.DurationOps._
import com.twitter.inject.{Injector, TwitterModule}
import com.twitter.inject.annotations.Flag
import com.twitter.util.Await
import javax.inject.Singleton

object MyModule extends TwitterModule {
  flag[Int]("configuration.param1", 42, "This is used to configure an instance of a Wicket")
  flag[Double]("configuration.param2", 123.45d, "This is also used to configure an instance of a Wicket")

  @Provides
  @Singleton
  def providesSomeClient(
    @Flag("configuration.param1") configurationParam1: Int,
    @Flag("configuration.param2") configurationParam2: Double
  ): SomeClient = {
    val client =
      new SomeClient(configurationParam1, configurationParam2)
        .withAnotherParam(b = true)
        .withSomeOtherConfiguration(137)

    closeOnExit {
       Await.result(client.close(), 2.seconds)
    }

    client
  }
}
heligw commented 2 years ago

TwitterModuleLifecycle.closeOnExit has been renamed to OnExit more details here https://github.com/twitter/finatra/commit/b8f00879bc3542e0e68f523c23ef46e4444f8d7b

telemat commented 2 years ago

Thanks, that was really helpful. Would help if the documentation is updated as well.