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
@type on_timeout_action() :: :retry | :stop | :continue
@type on_warning_action() :: :log | :notify | :continue | :promote_to_error
@type policy_action() :: on_error_action() | on_warning_action() | on_timeout_action()
@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
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 policy with an error handling function.
The function receives (error, retry_count, context) and must return
:retry, :stop, or :continue.
@spec default() :: t()
Default policy: retries 3 times with 1s delay.
@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}.
@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.
Determines the action to take on a warning.
Returns :log, :notify, :continue, or :promote_to_error.
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())
@spec strict() :: t()
Strict policy: stops on the first error without retrying.
Tolerant policy: retries up to 10 times.
Options
:max_retries— Maximum retries (default: 10):retry_delay— Delay between retries in ms (default: 2000)