KlokkaRuntime

class KlokkaRuntime(store: JobStore, registry: JobRegistry, settings: KlokkaSettings = KlokkaSettings())

The engine: claims work from the store, executes registered handlers, orchestrates retries through CAS transitions, and emits JobEvents. Framework-free: klokka-ktor wires this to the Ktor application lifecycle, but any JVM main function can run it.

Lifecycle: construct, start inside a scope, drain on shutdown. Producer-role instances skip start entirely or start with role = Producer (no workers launched).

Constructors

Link copied to clipboard
constructor(store: JobStore, registry: JobRegistry, settings: KlokkaSettings = KlokkaSettings())

Properties

Link copied to clipboard
val events: SharedFlow<JobEvent>

Hot stream of lifecycle events. Dropped under extreme backpressure, never blocking.

Functions

Link copied to clipboard
suspend fun drain()

Graceful shutdown: stop claiming, wait up to KlokkaSettings.drainTimeout for in-flight jobs, then cancel stragglers and requeue them (transition Running to Enqueued with the fence). Safe to call without start (no-op) and at most once.

Link copied to clipboard
suspend fun <T> enqueue(type: JobType<T>, payload: T, options: JobOptions = JobOptions()): JobId

Persists a job due immediately, on JobOptions.queue if set, otherwise on JobType.queue. Returns the job id (existing id on uniqueKey dedup).

Link copied to clipboard

True when at least one recurring schedule is declared.

Link copied to clipboard
fun <T> recurring(id: String, type: JobType<T>, payload: T, schedule: Schedule, misfire: MisfirePolicy = MisfirePolicy.FireOnce, misfireThreshold: Duration = 1.minutes, overlap: OverlapPolicy = OverlapPolicy.Allow, queue: QueueName? = null)

Declares a recurring schedule: payload is enqueued as a run of type every time schedule fires. Must be called before start, which registers all declared schedules with the store; only nodes that declare a schedule (and run workers) fire it, so declare recurring jobs on the worker fleet, not on producer-only nodes.

Link copied to clipboard
suspend fun <T> schedule(type: JobType<T>, payload: T, at: Instant, options: JobOptions = JobOptions()): JobId

Persists a job due at at; queue resolution as in enqueue.

Link copied to clipboard
fun start(scope: CoroutineScope)

Launches the worker machinery as supervised children of scope: the claim loop, per-queue executors, the heartbeater, the retention sweeper, and (when schedules are declared) the scheduler loop that registers and fires them. No-op for KlokkaRole.Producer: a producer-only node neither runs jobs nor fires schedules. Idempotent: calling twice is an error.

Link copied to clipboard
suspend fun triggerNow(id: String): JobId

Enqueues a run of the recurring schedule id immediately, without shifting the schedule's cadence. Respects the schedule's OverlapPolicy: with SkipIfRunning and a non-terminal run outstanding, this is an idempotent no-op returning that run's id.