Arrea.Policies (Arrea v1.0.0)

Copy Markdown View Source

Policy system for error handling and worker behavior.

Allows configuring specific actions when a worker:

  • Fails a task
  • Emits a warning
  • Reaches a specific state
  • Exceeds retry limits

Policy Types

%{
  on_error: :retry | :stop | :continue | custom_function,
  on_warning: :log | :notify | :continue,
  on_timeout: :retry | :stop | :continue,
  max_retries: 3,
  retry_delay: 1000,
  timeout: 30_000
}

Summary

Functions

Applies a policy to a task function.

Custom policy with an error handling function.

Default policy: retries 3 times with 1s delay.

Determines the action to take on an error.

Determines the action to take on a timeout.

Determines the action to take on a warning.

Creates a new policy with custom values.

Strict policy: stops on the first error without retrying.

Tolerant policy: retries up to 10 times.

Types

on_error_action()

@type on_error_action() ::
  :retry | :stop | :continue | :log_and_continue | (term() -> term())

on_timeout_action()

@type on_timeout_action() :: :retry | :stop | :continue

on_warning_action()

@type on_warning_action() :: :log | :notify | :continue | :promote_to_error

policy_action()

@type policy_action() :: on_error_action() | on_warning_action() | on_timeout_action()

t()

@type t() :: %Arrea.Policies{
  custom_handler: nil | (atom(), term(), map() -> term()),
  id: atom() | String.t(),
  log_errors: boolean(),
  max_retries: non_neg_integer(),
  notify_on_error: boolean(),
  on_error: on_error_action(),
  on_timeout: on_timeout_action(),
  on_warning: on_warning_action(),
  retry_delay: non_neg_integer(),
  timeout: timeout()
}

Functions

apply(policy, task_fn, context \\ %{})

@spec apply(t(), (-> term()), map()) :: {:ok, term()} | {:error, term(), term()}

Applies a policy to a task function.

Executes the function with timeout and returns {:ok, value} or {:error, reason, action} where action is the policy decision.

custom(handler_fn)

@spec custom((term(), term(), map() -> :retry | :stop | :continue)) :: t()

Custom policy with an error handling function.

The function receives (error, retry_count, context) and must return :retry, :stop, or :continue.

default()

@spec default() :: t()

Default policy: retries 3 times with 1s delay.

handle_error(policy, error, retry_count, context \\ %{})

@spec handle_error(t(), term(), non_neg_integer(), map()) ::
  {:retry, non_neg_integer()} | :stop | :continue | {:custom, term()}

Determines the action to take on an error.

Returns {:retry, delay}, :stop, :continue, or {:custom, result}.

handle_timeout(policy, retry_count, context \\ %{})

@spec handle_timeout(t(), non_neg_integer(), map()) ::
  {:retry, non_neg_integer()} | :stop | :continue

Determines the action to take on a timeout.

Returns {:retry, delay}, :stop, or :continue.

handle_warning(policy, warning, context \\ %{})

@spec handle_warning(t(), term(), map()) ::
  :log | :notify | :continue | :promote_to_error

Determines the action to take on a warning.

Returns :log, :notify, :continue, or :promote_to_error.

new(opts \\ [])

@spec new(keyword()) :: t()

Creates a new policy with custom values.

Options

  • :id — Policy identifier
  • :on_error — Action on error (:retry, :stop, :continue, :log_and_continue, or function)
  • :on_warning — Action on warning (:log, :notify, :continue, :promote_to_error)
  • :on_timeout — Action on timeout (:retry, :stop, :continue)
  • :max_retries — Maximum retries (default: 3)
  • :retry_delay — Delay between retries in ms (default: 1000)
  • :timeout — Timeout per task in ms (default: 30_000)
  • :log_errors — Log errors (default: false)
  • :notify_on_error — Notify subscribers on error (default: true)
  • :custom_handler — Custom handler function (error, retry_count, context -> term())

strict()

@spec strict() :: t()

Strict policy: stops on the first error without retrying.

tolerant(opts \\ [])

@spec tolerant(keyword()) :: t()

Tolerant policy: retries up to 10 times.

Options

  • :max_retries — Maximum retries (default: 10)
  • :retry_delay — Delay between retries in ms (default: 2000)