Skip to content
Leader docs0.4

Ktor integration

Latest stable Based on Leader release 0.4.0

Library module

Integrates a suspend elector with Ktor 3 through LeaderElectionPlugin, leaderScheduled, and an optional management route. It binds job lifetime to the application lifecycle.

Use it when a Ktor service owns coroutine jobs that should run on only one node. Use Spring integration for proxy/annotation-driven methods.

Artifact: io.github.bluetape4k.leader:bluetape4k-leader-ktor

dependencies {
implementation(platform("io.github.bluetape4k:bluetape4k-dependencies:<version>"))
implementation("io.github.bluetape4k.leader:bluetape4k-leader-ktor")
}

The plugin resolves one SuspendLeaderElector; scheduled jobs run in application-owned coroutine scope and stop on shutdown. Contention skips the iteration rather than failing the server.

install(LeaderElectionPlugin) {
leaderElection = mySuspendElector
}
leaderScheduled("projection-refresh", 1.minutes) {
refreshProjection()
}

Install the plugin for elector ownership, call leaderScheduled for periodic suspend work, and expose management routes only under an authenticated operations boundary.

Install once, choose stable lock names, keep actions shorter than the lease or extend safely, and make shutdown cancellation part of the job contract.

Any suspend backend can be supplied. The ktor-app example shows plugin installation, scheduling, and lifecycle together.

Configure the elector in its backend module, then set schedule interval/delay and Ktor scope ownership. Do not create a hidden second client inside each job.

A missing plugin or elector is a startup configuration failure. Direct elector calls propagate backend and action failures. Application.leaderScheduled catches non-cancellation Exception, logs it at WARN, suppresses that iteration, and continues with the next cycle; normal contention still skips. Cancellation stops scheduling and lets the elector release owned state.

Measure scheduled attempts, elected runs, skips, failures, duration, and shutdown completion. Keep management endpoints authenticated and low-cardinality.

Use Ktor test application for plugin configuration and schedule lifecycle, plus backend integration tests for ownership. Verify shutdown during acquire and action.

Run ktor-app, then follow the chosen backend page. Compare with Spring when deciding explicit scheduling versus AOP annotations.

The integration schedules jobs; it does not provide durable scheduling, missed-run recovery, cron persistence, or exactly-once delivery.

These diagrams are loaded directly from README assets published with the 0.4.0 release and pinned to its immutable commit. They describe this manual’s released structure and runtime flows, not later Snapshot changes. Select a preview to open the SVG at the same release commit.

leader ktor Architecture diagram

Release README: leader-ktor/README.md

leader ktor Sequence Flow diagram

Release README: leader-ktor/README.md

Plugin · Scheduling extension · Stable guide