Oi.CancelToken (oi v0.9.0)

Copy Markdown

Cooperative cancellation token for Oi.execute/2.

An atomics-backed flag: no process lifecycle, shareable across processes and nodes' local callers. Create one per dispatch, keep it, and call cancel/1 from any process to request cancellation.

The Orchestrator checks the token before each stage (the barrier points — same granularity as :checkpoint). A cancelled dispatch stops before the next stage and returns normally:

{:ok, %Oi.Result{status: :cancelled, halted_at: stage_index}}

In-flight steps of the current stage run to completion — this is cooperative cancellation, not preemption. Steps that want finer granularity can receive the token (e.g. via :data or step opts) and poll cancelled?/1 between internal work units.

Example

token = Oi.CancelToken.new()

task = Task.async(fn -> Oi.execute(compiled, data: data, cancel_token: token) end)

# ...later, from any process:
Oi.CancelToken.cancel(token)

{:ok, result} = Task.await(task)
result.status in [:complete, :cancelled]

Summary

Functions

Request cancellation. Idempotent.

Whether cancellation has been requested.

Create a fresh, non-cancelled token.

Types

t()

@type t() :: %Oi.CancelToken{ref: :atomics.atomics_ref()}

Functions

cancel(cancel_token)

@spec cancel(t()) :: :ok

Request cancellation. Idempotent.

cancelled?(cancel_token)

@spec cancelled?(t()) :: boolean()

Whether cancellation has been requested.

new()

@spec new() :: t()

Create a fresh, non-cancelled token.