TaskChainExecutor

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.

Step-level state restoration

Every time a step completes successfully, ChainRepository.updateStep is called before the next step is enqueued. If the app is killed mid-chain and relaunched, call restorePendingChains to resume from the last committed step.

Usage

val executor = TaskChainExecutor(kmpWorker, chainRepository, appScope)
executor.execute(myChain)

// On app startup (after TaskMonitor.replayPendingEvents()):
executor.restorePendingChains()

Constructors

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

Functions

Link copied to clipboard
suspend fun cancel(chain: TaskChain)

Cancels a running chain and removes its progress record. Any currently-executing step is cancelled via KmpWorker.cancel.

Link copied to clipboard
suspend fun execute(chain: TaskChain, policy: ChainPolicy = ChainPolicy.ALLOW_DUPLICATE)

Starts executing chain from step 0. Registers an internal observer that advances steps on success.

Link copied to clipboard
suspend fun restorePendingChains(chainProvider: suspend (ChainProgress) -> TaskChain?)

Restores all chains that were RUNNING when the app was last terminated. Call at app startup, after TaskMonitor.replayPendingEvents.