Package-level declarations

Types

Link copied to clipboard

Policy for handling duplicate chain IDs when enqueueing a TaskChain.

Link copied to clipboard
data class ChainProgress(val chainId: String, val stepsJson: String, val currentStep: Int, val totalSteps: Int, val status: String, val updatedAt: Long)

Snapshot of a chain's current state.

Link copied to clipboard
interface ChainRepository

Persistent store for TaskChain progress.

Link copied to clipboard
@Serializable
data class Constraints(val requiresInternet: Boolean = false, val requiresCharging: Boolean = false, val batteryNotLow: Boolean = false, val requiresDeviceIdle: Boolean = false, val contentUris: List<String> = emptyList())

Platform conditions that must be satisfied before a task can execute.

Link copied to clipboard
interface EventStore

Persists task completion events to durable storage so they survive app termination.

Link copied to clipboard
data class ExecutionRecord(val taskId: String, val startedAt: Long, val completedAt: Long, val durationMs: Long, val state: String, val retryCount: Int, val error: String?)

A single execution record for telemetry and analytics.

Link copied to clipboard

Marks an API as experimental in KMPWorker.

Link copied to clipboard
interface KmpWorker

Main entry point for KMPWorker.

Link copied to clipboard

Fluent builder for constructing a configured KmpWorker instance.

Link copied to clipboard
data class KmpWorkerConfig(val maxRetries: Int = 10, val logLevel: KmpWorkerLogger.Level = KmpWorkerLogger.Level.INFO, val logger: KmpWorkerLogger.Logger? = null, val taskTimeout: Duration? = null)

Global configuration for KMPWorker behaviour.

Link copied to clipboard

Base class for all exceptions thrown by KMPWorker.

Link copied to clipboard

Pluggable logging interface for KMPWorker.

Link copied to clipboard
class RateLimiter(val maxConcurrent: Int)

Rate limiter for controlling concurrent task execution.

Link copied to clipboard

Pure retry delay calculator. Stateless — no side effects.

Link copied to clipboard
@Serializable
sealed class RetryPolicy

Defines how a failed task should be retried.

Link copied to clipboard
data class TaskChain(val id: String, val steps: List<TaskRequest>)

Defines a sequential chain of background tasks where each step must succeed before the next one begins. Progress is persisted at every step boundary, so if the app is killed mid-chain (e.g., iOS 30-second limit), execution resumes from the correct step on next launch.

Link copied to clipboard
class TaskChainBuilder(chainId: String)

Fluent DSL builder for constructing TaskChains.

Link copied to clipboard
class TaskChainExecutor(worker: KmpWorker, chainRepository: ChainRepository, scope: CoroutineScope = CoroutineScope(Dispatchers.Default + SupervisorJob()))

Drives the execution of a TaskChain by observing TaskMonitor and advancing the chain one step at a time.

Link copied to clipboard
data class TaskExecutionContext(val taskId: String, val retryCount: Int = 0, val payload: String? = null, val tags: Set<String> = emptySet())

Context provided to a task handler at execution time.

Link copied to clipboard
data class TaskGraph(val id: String, val nodes: List<TaskRequest>, val edges: List<TaskGraph.Edge>)

A directed acyclic graph (DAG) of tasks with dependency edges.

Link copied to clipboard
class TaskGraphBuilder(graphId: String)

DSL builder for TaskGraph.

Link copied to clipboard
class TaskGraphExecutor(worker: KmpWorker, scope: CoroutineScope = CoroutineScope(Dispatchers.Default))

Executes a TaskGraph respecting dependency ordering.

Link copied to clipboard

Broadcasts task state changes across the application.

Link copied to clipboard

Task execution priority hint.

Link copied to clipboard

Thread-safe global registry mapping task IDs to their suspend handler functions.

Link copied to clipboard
@Serializable
data class TaskRequest(val id: String, val type: TaskType, val constraints: Constraints = Constraints(), val retryPolicy: RetryPolicy = RetryPolicy.None, val priority: TaskPriority = TaskPriority.NORMAL, val tags: Set<String> = emptySet(), val payload: String? = null, val label: String? = null)

Describes a background task to be scheduled.

Link copied to clipboard
class TaskRequestBuilder(taskId: String)

Builder for configuring individual TaskRequests within a chain DSL.

Link copied to clipboard
sealed class TaskState

Represents the current lifecycle state of a background task.

Link copied to clipboard
@Serializable
sealed class TaskType

Defines how a task is scheduled.

Link copied to clipboard

Collects execution metrics for background tasks.

Properties

Link copied to clipboard
val KmpWorkerJson: Json

Default Json instance used by KMPWorker for typed payload encoding/decoding.

Functions

Link copied to clipboard
suspend fun KmpWorker.chain(chainId: String, policy: ChainPolicy = ChainPolicy.ALLOW_DUPLICATE, block: TaskChainBuilder.() -> Unit)

Builds and enqueues a TaskChain using a fluent DSL.

Link copied to clipboard
inline fun <T> String?.decodeAsPayload(): T?

Decodes a raw JSON string into a strongly-typed object. Useful when you have the payload String directly (e.g., from logging or testing).

Link copied to clipboard

Decodes the payload of this TaskExecutionContext into a strongly-typed object.

Link copied to clipboard

Returns the step IDs decoded from a ChainProgress.stepsJson.

Link copied to clipboard
fun exponentialRetry(initialDelay: Duration, maxRetries: Int = KmpWorkerConfig.current().maxRetries): RetryPolicy.Exponential

Enqueues a task with exponential backoff using Duration-based delay.

Link copied to clipboard

Filters to only TaskState.Failed states.

Link copied to clipboard
suspend fun KmpWorker.graph(graphId: String, block: TaskGraphBuilder.() -> Unit)

Builds and executes a TaskGraph (DAG) using a fluent DSL.

Link copied to clipboard

Enqueues a task with linear retry using Duration-based delay.

Link copied to clipboard
fun Flow<TaskState>.onCancelled(action: suspend () -> Unit): Flow<TaskState>

Invokes action when the task reaches TaskState.Cancelled.

Link copied to clipboard
suspend fun KmpWorker.oneTime(id: String, constraints: Constraints = Constraints(), retryPolicy: RetryPolicy = RetryPolicy.None, payload: String? = null, block: suspend () -> Unit)

Registers and enqueues a one-time task in a single call.

Link copied to clipboard
fun Flow<TaskState>.onFailed(action: suspend (TaskState.Failed) -> Unit): Flow<TaskState>

Invokes action when the task reaches TaskState.Failed.

Link copied to clipboard
fun Flow<TaskState>.onProgress(action: suspend (progress: Float, message: String?) -> Unit): Flow<TaskState>

Invokes action whenever progress is reported via TaskState.Running with non-null progress.

Link copied to clipboard
fun Flow<TaskState>.onRunning(action: suspend () -> Unit): Flow<TaskState>

Invokes action whenever the task enters TaskState.Running.

Link copied to clipboard
fun Flow<TaskState>.onSuccess(action: suspend () -> Unit): Flow<TaskState>

Invokes action when the task reaches TaskState.Success.

Link copied to clipboard
fun Flow<TaskState>.onTerminal(action: suspend (TaskState) -> Unit): Flow<TaskState>

Invokes action when the task reaches any terminal state.

Link copied to clipboard
fun Flow<TaskState>.onTimedOut(action: suspend (TaskState.TimedOut) -> Unit): Flow<TaskState>

Invokes action when the task is stopped due to timeout.

Link copied to clipboard
suspend fun KmpWorker.periodic(id: String, repeatInterval: Duration, constraints: Constraints = Constraints(), retryPolicy: RetryPolicy = RetryPolicy.None, block: suspend () -> Unit)

Registers and enqueues a periodic task using a Duration interval.

Link copied to clipboard

Filters to only TaskState.Running states that carry progress info.

Link copied to clipboard

Filters to only TaskState.Success states.

Link copied to clipboard

Filters to only terminal states.

Link copied to clipboard
inline fun <T> TaskRequest.withPayload(data: T): TaskRequest

Encodes data as JSON and attaches it as the payload for this TaskRequest.