Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mathieuprog/retry_on
Elixir utility for retrying Ecto operations
https://github.com/mathieuprog/retry_on
Last synced: about 2 months ago
JSON representation
Elixir utility for retrying Ecto operations
- Host: GitHub
- URL: https://github.com/mathieuprog/retry_on
- Owner: mathieuprog
- License: apache-2.0
- Created: 2023-07-25T02:31:26.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-07-25T02:34:09.000Z (over 1 year ago)
- Last Synced: 2024-10-02T07:18:22.066Z (3 months ago)
- Language: Elixir
- Homepage:
- Size: 11.7 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# RetryOn
## `retry_on_stale/2`
`retry_on_stale/2` performs an operation that might raise an `Ecto.StaleEntryError` error and retries
it a specified number of times with a delay between each attempt.## Example
The example features a wallet's `balance_amount`, protected by [optimistic locking](https://hexdocs.pm/ecto/Ecto.Changeset.html#optimistic_lock/3) to prevent race conditions. The `retry_on_stale/2` function handles `Ecto.StaleEntryError` from concurrent transactions, refetching the wallet and retrying the operation.
```elixir
import RetryOn, only: [retry_on_stale: 2]def increase_wallet_balance(%Wallet{} = wallet, amount) do
retry_on_stale(
fn attempt ->
# refetch the latest wallet data for subsequent attempts
wallet = if attempt == 1, do: wallet, else: Repo.get!(Wallet, wallet.id)# this function could raise a StaleEntryError
do_increase_wallet_balance(wallet, amount)
end,
max_attempts: 5, delay_ms: 100
)
enddefp do_increase_wallet_balance(%Wallet{} = wallet, amount) do
new_balance = Decimal.add(wallet.balance_amount, amount)wallet
|> Ecto.Changeset.change(balance_amount: new_balance)
|> Ecto.Changeset.optimistic_lock(:lock_version)
|> Repo.update!()
end
```## Options
`:max_attempts` - The maximum number of attempts to perform the operation
before giving up and re-raising the last `Ecto.StaleEntryError`.`:delay_ms` - The delay in milliseconds between each attempt.
## `retry_on_unique_constraint/3`
`retry_on_unique_constraint/3` performs an operation that might raise an `Ecto.UniqueConstraintError` error and retries
it a specified number of times with a delay between each attempt.## Example
```elixir
import RetryOn, only: [retry_on_unique_constraint: 2]retry_on_unique_constraint(
Repo,
:my_field,
fn _ ->
# Code that performs an operation which may result in a unique constraint error on `:my_field`.
# The function is retried if a unique constraint error is found in a `{:error, changeset}` tuple or
# an `%Ecto.InvalidChangesetError{changeset: changeset}` error.
end,
max_attempts: 2, delay_ms: 500
)
```## Options
`:max_attempts` - The maximum number of attempts to perform the operation
before giving up and re-raising the last Ecto.StaleEntryError.`:delay_ms` - The delay in milliseconds between each attempt.
## Installation
Add `retry_on` for Elixir as a dependency in your `mix.exs` file:
```elixir
def deps do
[
{:retry_on, "~> 0.1.0"}
]
end
```## HexDocs
HexDocs documentation can be found at [https://hexdocs.pm/retry_on](https://hexdocs.pm/retry_on).