Snodo.Extensions.Tasks.RetryPolicy (snodo_tasks v0.1.0)

Copy Markdown View Source

Immutable, JSON-safe retry timing for durable task work.

A policy is an exact, finite list of delays. Entry zero is used for the first retry requested by an executor, entry one for the second, and so on. Persisting the expanded list keeps scheduling deterministic across releases and restarts; no runtime callback is re-evaluated after recovery.

The empty policy returned by none/0 is the default and never retries. Retry delays bound application-requested retries only. Re-delivery after an ambiguous runner or node failure remains part of the runner's at-least-once recovery contract and does not consume this list.

Summary

Functions

Expands a capped exponential policy into exact persisted delays.

Builds an exponential policy or raises ArgumentError.

Builds retries entries with one fixed delay.

Builds a fixed-delay policy or raises ArgumentError.

Decodes and validates a persistence map produced by to_map/1.

Builds a policy from its exact retry delays.

Builds an exact-delay policy or raises ArgumentError.

Returns the next persisted delay without modifying the policy.

Returns the default policy, which never retries executor outcomes.

Encodes a policy as a stable JSON-safe persistence map.

Validates a policy constructed locally or decoded by an adapter.

Types

delay_ms()

@type delay_ms() :: non_neg_integer()

t()

@type t() :: %Snodo.Extensions.Tasks.RetryPolicy{delays_ms: [delay_ms()]}

Functions

exponential(initial_delay_ms, retries, opts \\ [])

@spec exponential(delay_ms(), non_neg_integer(), keyword()) ::
  {:ok, t()} | {:error, term()}

Expands a capped exponential policy into exact persisted delays.

:max_delay_ms defaults to the largest portable timer delay supported by this package. The first retry uses initial_delay_ms, then each subsequent entry doubles until the cap is reached.

exponential!(initial_delay_ms, retries, opts \\ [])

@spec exponential!(delay_ms(), non_neg_integer(), keyword()) :: t()

Builds an exponential policy or raises ArgumentError.

fixed(delay_ms, retries)

@spec fixed(delay_ms(), non_neg_integer()) :: {:ok, t()} | {:error, term()}

Builds retries entries with one fixed delay.

fixed!(delay_ms, retries)

@spec fixed!(delay_ms(), non_neg_integer()) :: t()

Builds a fixed-delay policy or raises ArgumentError.

from_map(encoded)

@spec from_map(term()) :: {:ok, t()} | {:error, term()}

Decodes and validates a persistence map produced by to_map/1.

new(delays_ms)

@spec new([delay_ms()]) :: {:ok, t()} | {:error, term()}

Builds a policy from its exact retry delays.

new!(delays_ms)

@spec new!([delay_ms()]) :: t()

Builds an exact-delay policy or raises ArgumentError.

next_delay(retry_policy, retry_count)

@spec next_delay(t(), non_neg_integer()) :: {:ok, delay_ms()} | :exhausted

Returns the next persisted delay without modifying the policy.

none()

@spec none() :: t()

Returns the default policy, which never retries executor outcomes.

to_map(policy)

@spec to_map(t()) :: map()

Encodes a policy as a stable JSON-safe persistence map.

validate(arg1)

@spec validate(t()) :: :ok | {:error, term()}

Validates a policy constructed locally or decoded by an adapter.