Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ See Clock in the https://rocketraman.github.io/bootable/[documentation].
* `boot-clock` binds the application's clock once, as a `kotlin.time.Clock` and a `java.time.Clock`.
* The `java.time.Clock` wraps the `kotlin.time.Clock`, in UTC, so a test that replaces one replaces both.
* The modules that take a `kotlin.time.Clock` binding, if there is one, take this one.
* The Inara `PeriodicModule`, `KubernetesLeaderElectionModule`, `DbSchedulerModule`, `OpenTelemetryModule`, and `ServiceStartTracingModule` require the `ClockModule`, in place of a clock parameter of their builders.
* `asJavaClock()` and `asKotlinClock()` wrap either clock as the other.
* Koin, Metro, and Inara flavors, and a deprecated Kodein flavor.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import com.github.rocketraman.bootable.boot.setOfAppServices
import com.github.rocketraman.bootable.boot.startPlan
import com.github.rocketraman.bootable.client.http.ktor.koin.httpClientTelemetryModule as koinHttpClientTelemetryModule
import com.github.rocketraman.bootable.client.http.ktor.koin.ktorClientModule as koinKtorClientModule
import com.github.rocketraman.bootable.clock.ClockModule
import com.github.rocketraman.bootable.clock.clockModule
import com.github.rocketraman.bootable.config.ConfigModule
import com.github.rocketraman.bootable.config.hopliteConfigModule
import com.github.rocketraman.bootable.config.common.HostPort
Expand Down Expand Up @@ -261,6 +263,7 @@ object TestTelemetryBindings {
interface InaraTestModule :
ConfigModule,
BootableModule,
ClockModule,
OpenTelemetryModule,
KtorClientModule,
HttpClientTelemetryModule {
Expand Down Expand Up @@ -339,6 +342,7 @@ val HttpClientTelemetryWiringTest by testSuite {
val module = InaraTestModule(
configModule = hopliteConfigModule(),
bootableModule = bootableModule(),
clockModule = clockModule(),
openTelemetryModule = openTelemetryModule(),
ktorClientModule = ktorClientModule(Downstream()),
httpClientTelemetryModule = httpClientTelemetryModule(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import org.kodein.di.inara.DIModule
import kotlin.time.Clock

/**
* The Inara module that provides the application's clock, as a `kotlin.time.Clock` and as a `java.time.Clock`, for
* the application's own modules to require. Compose [clockModule], with `Clock.System`, or, in a test, with a clock
* of its own.
* The Inara module that provides the application's clock, as a `kotlin.time.Clock` and as a `java.time.Clock`. The
* Bootable modules that need a clock require it, such as `PeriodicModule`, and so can the application's own modules.
* Compose [clockModule], with `Clock.System`, or, in a test, with a clock of its own.
*/
@DIModule
interface ClockModule {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,16 @@ class TestClockBindings(private val clock: TestClock) {
@DIModule(composed = true)
interface ClockAppModule : ClockModule, LeaderElectionModule, OpenTelemetryApiModule, PeriodicModule

// a call resolves to the Kodein flavor's clockModule, of the same signature; a reference of this type to Inara's
// the Kodein and Inara flavors' clockModule have the same signature: a reference of each type picks one
private val kodeinClockModule: (Clock) -> DI.Module = ::clockModule
private val inaraClockModule: (Clock) -> ClockModule = ::clockModule

private fun clockAppModule(clock: Clock = Clock.System): ClockAppModule {
val clockModule = inaraClockModule(clock)
return ClockAppModule(
clockModule = clockModule,
leaderElectionModule = AlwaysLeaderModule,
openTelemetryApiModule = NoopOpenTelemetryModule,
periodicModule = periodicModule(clockModule.clock),
)
}
private fun clockAppModule(clock: Clock = Clock.System) = ClockAppModule(
clockModule = inaraClockModule(clock),
leaderElectionModule = AlwaysLeaderModule,
openTelemetryApiModule = NoopOpenTelemetryModule,
periodicModule = periodicModule(),
)

/** Checks [javaClock] wraps [clock], by moving [clock]. */
private fun checkWraps(javaClock: JClock, clock: TestClock) {
Expand All @@ -77,7 +75,7 @@ val ClockWiringTest by testSuite {
test("binds the system clock, as both clocks, for the modules that take a clock") {
val di = DI {
import(bootableModule)
import(clockModule())
import(kodeinClockModule(Clock.System))
import(periodicModule)
}.direct
assert(di.instance<Clock>() == Clock.System)
Expand All @@ -89,7 +87,7 @@ val ClockWiringTest by testSuite {
val testClock = TestClock()
val di = DI {
import(bootableModule)
import(clockModule())
import(kodeinClockModule(Clock.System))
import(periodicModule)
bindSingleton<Clock>(overrides = true) { testClock }
}.direct
Expand All @@ -100,7 +98,7 @@ val ClockWiringTest by testSuite {

test("binds the clock it is given") {
val testClock = TestClock()
val di = DI { import(clockModule(testClock)) }.direct
val di = DI { import(kodeinClockModule(testClock)) }.direct
assert(di.instance<Clock>() === testClock)
checkWraps(di.instance(), testClock)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import com.github.rocketraman.bootable.boot.BootableModule
import com.github.rocketraman.bootable.boot.bootableModule
import com.github.rocketraman.bootable.boot.koin.bootableModule as koinBootableModule
import com.github.rocketraman.bootable.boot.setOfAppServices
import com.github.rocketraman.bootable.clock.ClockModule
import com.github.rocketraman.bootable.clock.clockModule
import com.github.rocketraman.bootable.config.ConfigModule
import com.github.rocketraman.bootable.config.hopliteConfigModule
import com.github.rocketraman.bootable.config.koin.hopliteConfigModule as koinHopliteConfigModule
Expand Down Expand Up @@ -189,6 +191,7 @@ object TestBindings {
interface InaraTestModule :
ConfigModule,
BootableModule,
ClockModule,
OpenTelemetryModule,
JdbcDataSourceModule,
HikariPoolModule,
Expand All @@ -202,6 +205,7 @@ private val inaraJdbcTracingModule: (JdbcTelemetryBuilder.() -> Unit) -> JdbcTra
private fun inaraTestModule(configure: JdbcTelemetryBuilder.() -> Unit = {}) = InaraTestModule(
configModule = hopliteConfigModule(),
bootableModule = bootableModule(),
clockModule = clockModule(),
openTelemetryModule = openTelemetryModule(),
jdbcDataSourceModule = jdbcDataSourceModule(JdbcGenericConfigSource),
hikariPoolModule = hikariPoolModule(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import com.github.rocketraman.bootable.boot.BootableModule
import com.github.rocketraman.bootable.boot.bootableModule
import com.github.rocketraman.bootable.boot.koin.bootableModule as koinBootableModule
import com.github.rocketraman.bootable.boot.setOfAppServices
import com.github.rocketraman.bootable.clock.ClockModule
import com.github.rocketraman.bootable.clock.clockModule
import com.github.rocketraman.bootable.config.ConfigModule
import com.github.rocketraman.bootable.config.hopliteConfigModule
import com.github.rocketraman.bootable.config.koin.hopliteConfigModule as koinHopliteConfigModule
Expand Down Expand Up @@ -119,6 +121,7 @@ object TestBindings {
interface InaraTestModule :
ConfigModule,
BootableModule,
ClockModule,
OpenTelemetryModule,
JdbcDataSourceModule,
HikariPoolModule,
Expand All @@ -130,6 +133,7 @@ interface InaraTestModule :
private fun inaraTestModule() = InaraTestModule(
configModule = hopliteConfigModule(),
bootableModule = bootableModule(),
clockModule = clockModule(),
openTelemetryModule = openTelemetryModule(),
jdbcDataSourceModule = jdbcDataSourceModule(JdbcGenericConfigSource),
hikariPoolModule = hikariPoolModule(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
package com.github.rocketraman.bootable.leader.k8s

import com.github.rocketraman.bootable.boot.AppServices
import com.github.rocketraman.bootable.clock.ClockModule
import com.github.rocketraman.bootable.client.http.ktor.KtorClientDefaults
import com.github.rocketraman.bootable.client.http.ktor.KtorClientModule
import com.github.rocketraman.bootable.client.http.ktor.NamedClientDefaults
import com.github.rocketraman.bootable.config.config
import com.github.rocketraman.bootable.leader.LeaderElectionModule
import org.kodein.di.inara.Binding
import org.kodein.di.inara.DIModule
import kotlin.time.Clock

/**
* The Inara module that provides the application's `LeaderElection`, as a [LeaderElectionModule], with a
Expand All @@ -22,17 +22,20 @@ import kotlin.time.Clock
* by default, or else the default client, of the [KtorClientModule] it requires. In a cluster, that client trusts the
* API server with no TLS config of the application's: see [kubernetesClientDefaults].
*
* Its leases are timed by the clock of the [ClockModule] it requires.
*
* `periodicModule()` (from boot-periodic) gates the application's periodic services on it.
*/
@DIModule
interface KubernetesLeaderElectionModule : LeaderElectionModule, @DIModule.Required KtorClientModule {
interface KubernetesLeaderElectionModule :
LeaderElectionModule, @DIModule.Required KtorClientModule, @DIModule.Required ClockModule {
val kubernetesLeaderElectionConfig: KubernetesLeaderElectionConfig
@AppServices val kubernetesLeaderElection: KubernetesLeaderElection
@NamedClientDefaults val kubernetesClientDefaults: KtorClientDefaults
}

/** A [KubernetesLeaderElectionModule], whose leases are timed by [clock]. */
fun kubernetesLeaderElectionModule(clock: Clock = Clock.System): KubernetesLeaderElectionModulePart =
/** A [KubernetesLeaderElectionModule]. */
fun kubernetesLeaderElectionModule(): KubernetesLeaderElectionModulePart =
KubernetesLeaderElectionModulePart(
kubernetesLeaderElectionConfig = config("leader.kubernetes"),
kubernetesLeaderElection = Binding.single {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import com.github.rocketraman.bootable.client.http.ktor.KtorClientService
import com.github.rocketraman.bootable.client.http.ktor.koin.ktorClientModule as koinKtorClientModule
import com.github.rocketraman.bootable.client.http.ktor.ktorClientModule
import com.github.rocketraman.bootable.client.http.ktor.setOfNamedClientDefaults
import com.github.rocketraman.bootable.clock.ClockModule
import com.github.rocketraman.bootable.clock.clockModule
import com.github.rocketraman.bootable.config.koin.hopliteConfigModule as koinHopliteConfigModule
import com.github.rocketraman.bootable.leader.k8s.koin.kubernetesLeaderElectionModule as koinKubernetesLeaderElectionModule
import dev.zacsweers.metro.BindingContainer
Expand Down Expand Up @@ -59,7 +61,7 @@ object EngineBindings {

/** An application's module, composed as an Inara application composes it. */
@DIModule(composed = true)
interface InaraTestModule : ConfigModule, BootableModule, KtorClientModule, KubernetesLeaderElectionModule
interface InaraTestModule : ConfigModule, BootableModule, ClockModule, KtorClientModule, KubernetesLeaderElectionModule

/** The election stops, releasing its Lease, before the HTTP clients close. */
private suspend fun checkOrder(services: Set<AppService>) {
Expand Down Expand Up @@ -147,6 +149,7 @@ val KubernetesLeaderElectionWiringTest by testSuite {
val module = InaraTestModule(
configModule = hopliteConfigModule(),
bootableModule = bootableModule(),
clockModule = clockModule(),
ktorClientModule = ktorClientModule(JavaClientEngine()),
kubernetesLeaderElectionModule = kubernetesLeaderElectionModule(),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

package com.github.rocketraman.bootable.periodic

import com.github.rocketraman.bootable.clock.ClockModule
import com.github.rocketraman.bootable.leader.LeaderElectionModule
import com.github.rocketraman.bootable.telemetry.otel.OpenTelemetryApiModule
import org.kodein.di.inara.Binding
import org.kodein.di.inara.DIModule
import org.kodein.di.inara.DISet
import kotlin.time.Clock

/** Marks a module member as one of the [PeriodicRunInterceptor]s of the application's periodic services. */
@DISet(of = PeriodicRunInterceptor::class)
Expand All @@ -23,6 +23,7 @@ annotation class PeriodicRunInterceptors
* - each run is traced, with [periodicTracing], through the OpenTelemetry of the [OpenTelemetryApiModule] it requires,
* which with the `NoopOpenTelemetryModule` records nothing.
* - their interceptors are the tracing, outermost, then the members marked [PeriodicRunInterceptors].
* - they're timed by the clock of the [ClockModule] it requires.
*
* Add each loop to the application's services:
*
Expand All @@ -32,13 +33,15 @@ annotation class PeriodicRunInterceptors
* ```
*/
@DIModule(sets = [PeriodicRunInterceptors::class])
interface PeriodicModule : @DIModule.Required LeaderElectionModule, @DIModule.Required OpenTelemetryApiModule {
interface PeriodicModule :
@DIModule.Required LeaderElectionModule,
@DIModule.Required OpenTelemetryApiModule,
@DIModule.Required ClockModule {
val periodicServices: PeriodicServices
}

/** A [PeriodicModule] on [clock], with each run traced as [tracingOptions] says. */
/** A [PeriodicModule], with each run traced as [tracingOptions] says. */
fun periodicModule(
clock: Clock = Clock.System,
tracingOptions: PeriodicTracingOptions = PeriodicTracingOptions(),
): PeriodicModulePart = PeriodicModulePart(
periodicServices = Binding.single {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import com.github.rocketraman.bootable.boot.BootableModule
import com.github.rocketraman.bootable.boot.bootableModule
import com.github.rocketraman.bootable.boot.koin.bootableModule as koinBootableModule
import com.github.rocketraman.bootable.boot.setOfAppServices
import com.github.rocketraman.bootable.clock.ClockModule
import com.github.rocketraman.bootable.clock.clockModule
import com.github.rocketraman.bootable.config.ConfigModule
import com.github.rocketraman.bootable.config.hopliteConfigModule
import com.github.rocketraman.bootable.config.koin.hopliteConfigModule as koinHopliteConfigModule
Expand Down Expand Up @@ -114,7 +116,8 @@ object TracingTestBindings {

/** An application's module, composed as an Inara application composes it, with the SDK and a loop to trace. */
@DIModule(composed = true)
interface InaraTracingModule : ConfigModule, BootableModule, OpenTelemetryModule, LeaderElectionModule, PeriodicModule {
interface InaraTracingModule :
ConfigModule, BootableModule, ClockModule, OpenTelemetryModule, LeaderElectionModule, PeriodicModule {
@OpenTelemetryCustomizers val inMemorySpans: OpenTelemetryCustomizer
@AppServices val tracedLoop: PeriodicAppService
}
Expand All @@ -126,6 +129,7 @@ private fun inaraTracingModule(
InaraTracingModule(
configModule = hopliteConfigModule(),
bootableModule = bootableModule(),
clockModule = clockModule(),
openTelemetryModule = openTelemetryModule(),
leaderElectionModule = AlwaysLeaderModule,
periodicModule = periodicModule(tracingOptions = tracingOptions),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import com.github.rocketraman.bootable.boot.BootableModule
import com.github.rocketraman.bootable.boot.bootableModule
import com.github.rocketraman.bootable.boot.koin.bootableModule as koinBootableModule
import com.github.rocketraman.bootable.boot.setOfAppServices
import com.github.rocketraman.bootable.clock.ClockModule
import com.github.rocketraman.bootable.clock.clockModule
import com.github.rocketraman.bootable.config.ConfigBinder
import com.github.rocketraman.bootable.config.ConfigModule
import com.github.rocketraman.bootable.config.bindConfig
Expand Down Expand Up @@ -84,14 +86,15 @@ object TestBindings {
/** An application's module, composed as an Inara application composes it, with the loop the wiring creates. */
@DIModule(composed = true)
interface InaraWiringModule :
ConfigModule, BootableModule, LeaderElectionModule, OpenTelemetryApiModule, PeriodicModule {
ConfigModule, BootableModule, ClockModule, LeaderElectionModule, OpenTelemetryApiModule, PeriodicModule {
val counter: Counter
@AppServices val counterLoop: PeriodicAppService
}

private fun inaraWiringModule(leaderElectionModule: LeaderElectionModule) = InaraWiringModule(
configModule = hopliteConfigModule(),
bootableModule = bootableModule(),
clockModule = clockModule(),
leaderElectionModule = leaderElectionModule,
openTelemetryApiModule = NoopOpenTelemetryModule,
periodicModule = periodicModule(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import com.github.kagkarlsson.scheduler.Scheduler
import com.github.kagkarlsson.scheduler.SchedulerClient
import com.github.rocketraman.bootable.boot.AppServices
import com.github.rocketraman.bootable.boot.HealthChecks
import com.github.rocketraman.bootable.clock.ClockModule
import com.github.rocketraman.bootable.config.config
import com.github.rocketraman.bootable.db.jdbc.JdbcDataSourceModule
import com.github.rocketraman.bootable.telemetry.otel.OpenTelemetryApiModule
import org.kodein.di.inara.Binding
import org.kodein.di.inara.DIModule
import org.kodein.di.inara.DISet
import kotlin.time.Clock

/** Marks a module member as one of the [DbSchedulerTaskProvider]s, the application's db-scheduler tasks. */
@DISet(of = DbSchedulerTaskProvider::class)
Expand All @@ -29,22 +29,25 @@ annotation class DbSchedulerCustomizers
* matches the `JdbcConfig`'s URL.
*
* Each execution is traced, with [dbSchedulerTracing], through the OpenTelemetry of the [OpenTelemetryApiModule] it
* requires, which with the `NoopOpenTelemetryModule` records nothing.
* requires, which with the `NoopOpenTelemetryModule` records nothing. It's timed by the clock of the [ClockModule] it
* requires.
*
* Add the application's tasks, recurring and one-time, with members marked [DbSchedulerTaskProviders], and adjust
* db-scheduler beyond [DbSchedulerConfig] with members marked [DbSchedulerCustomizers].
*/
@DIModule(sets = [DbSchedulerTaskProviders::class, DbSchedulerCustomizers::class])
interface DbSchedulerModule : @DIModule.Required JdbcDataSourceModule, @DIModule.Required OpenTelemetryApiModule {
interface DbSchedulerModule :
@DIModule.Required JdbcDataSourceModule,
@DIModule.Required OpenTelemetryApiModule,
@DIModule.Required ClockModule {
val dbSchedulerConfig: DbSchedulerConfig
@AppServices val dbSchedulerService: DbSchedulerService
val scheduler: Scheduler get() = dbSchedulerService.scheduler
val schedulerClient: SchedulerClient get() = scheduler
}

/** A [DbSchedulerModule] on [clock], with each execution traced as [tracingOptions] says. */
/** A [DbSchedulerModule], with each execution traced as [tracingOptions] says. */
fun dbSchedulerModule(
clock: Clock = Clock.System,
tracingOptions: DbSchedulerTracingOptions = DbSchedulerTracingOptions(),
): DbSchedulerModulePart = DbSchedulerModulePart(
dbSchedulerConfig = config("db.scheduler"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import com.github.rocketraman.bootable.boot.bootableModule
import com.github.rocketraman.bootable.boot.koin.bootableModule as koinBootableModule
import com.github.rocketraman.bootable.boot.setOfAppServices
import com.github.rocketraman.bootable.boot.startPlan
import com.github.rocketraman.bootable.clock.ClockModule
import com.github.rocketraman.bootable.clock.clockModule
import com.github.rocketraman.bootable.config.ConfigModule
import com.github.rocketraman.bootable.config.hopliteConfigModule
import com.github.rocketraman.bootable.config.koin.hopliteConfigModule as koinHopliteConfigModule
Expand Down Expand Up @@ -185,6 +187,7 @@ object TracingTestBindings {
interface InaraTracingTestModule :
ConfigModule,
BootableModule,
ClockModule,
OpenTelemetryModule,
JdbcDataSourceModule,
HikariPoolModule,
Expand All @@ -197,6 +200,7 @@ interface InaraTracingTestModule :
private fun inaraTracingTestModule() = InaraTracingTestModule(
configModule = hopliteConfigModule(),
bootableModule = bootableModule(),
clockModule = clockModule(),
openTelemetryModule = openTelemetryModule(),
jdbcDataSourceModule = jdbcDataSourceModule(SqliteConfigSource),
hikariPoolModule = hikariPoolModule(),
Expand Down
Loading
Loading