Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guice EDSL for ServiceGraphBuilder #999

Merged
merged 7 commits into from May 21, 2019

Conversation

keeferrourke
Copy link
Collaborator

@keeferrourke keeferrourke commented May 21, 2019

Follow-up for #984. This PR introduces a small EDSL to specify Service dependencies and enhancements.

This provides a ServiceModule which can be installed to other modules, providing an easy way to build Service dependency graphs with Guice.

The new provider method provideServiceGraphBuilder creates a builder for the graph. It is intended to supercede the provideServiceManager method (see note) as we migrate from CoordinatedService to CoordinatedService2.

Next steps will be to deprecate CoordinatedService and DependentService, then migrate modules to specify their graphs using the EDSL, like in the simple example below:

class ExampleModule : KAbstractModule() {
  override configure() {
    // Creates Service graph where ExampleService depends on Provider, and start-up order is:
    // Provider -> ExampleService -> Enhancer; and shut-down order is:
    // Enhancer -> ExampleService -> Provider.
    // Enhancer and Provider services may be installed in this module, or in another module by
    // install(service<X>()).
    install(ServiceModule<ExampleService>()
      .enhancedBy<Enhancer>()
      .dependsOn<Provider>()
    )
  }
}

ServiceGraphs may now be built by installing special modules in a
KAbstractModule#configure() method via a simple API.

e.g. If we have Services{A,B,C,D,E} in the following configuration:

```
ServiceA
  depended on by ServiceB
    enhanced by ServiceC
  depended on by ServiceD
    depended on by ServiceE
```

Then we can register these services in that configuration with a module
as follows:

```
val injector = Guice.createInjector(object: KAbstractModule() {
  override fun configure() {
    install(service<ServiceA>())
    install(service<ServiceB>()
        .dependsOn<ServiceA>()
        .enhancedBy<ServiceC>()
    )
    install(service<ServiceC>())
    install(service<ServiceD>().dependsOn<ServiceA>())
    install(service<ServiceE>().dependsOn<ServiceD>())
  }
})
```

And obtain a service manager by:

```
val builder = injector.getInstance<ServiceGraphBuilder>()
val serviceManager = builder.build()
```

In future commits:
 - Once everything in Misk is migrated to be provided a
   ServiceGraphBuilder, change the implementation of
   provideServiceManager to obtain and build the ServiceGraph instead of
   requiring modules to build the serviceManager themselves
@swankjesse
Copy link
Collaborator

#324

misk/src/main/kotlin/misk/MiskServiceModule.kt Outdated Show resolved Hide resolved
misk/src/main/kotlin/misk/MiskServiceModule.kt Outdated Show resolved Hide resolved
misk/src/main/kotlin/misk/MiskServiceModule.kt Outdated Show resolved Hide resolved
misk/src/main/kotlin/misk/MiskServiceModule.kt Outdated Show resolved Hide resolved
misk/src/main/kotlin/misk/MiskServiceModule.kt Outdated Show resolved Hide resolved
misk/src/main/kotlin/misk/MiskServiceModule.kt Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants