mathieuprog
`retry_on_stale/2` - retry operations encountering Ecto's stale entry errors
Elixir utility for retrying operations encountering Ecto’s stale entry errors (Ecto.StaleEntryError).
A more lightweight version of the retry library specifically designed to handle stale entry errors when using optimistic locking, requiring subsequent attempts.
import RetryOnStale, 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
)
end
defp 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
GitHub repo:
https://github.com/mathieuprog/retry_on_stale
Most Liked
mayel
You probably know this, but for others reading, you can also let the DB take care of the increment/decrement:
def increase_wallet_balance(wallet_id, by_amount) do
from(w in Wallet,
update: [inc: [balance_amount: ^by_amount]],
where: w.id == ^wallet_id
)
|> Repo.update_all([])
end
def decrease_wallet_balance(wallet_id, by_amount) do
increase_wallet_balance(wallet_id, -by_amount)
end
2
Popular in Announcing
I’ve published the first version of my Makeup library. It’s a syntax highlighter for Elixir in the spirit of Pygments, Currently it highl...
New
PhoenixWS - Websockets over Phoenix Channels
Source code on Github here: GitHub - tmbb/phoenix_ws: Websockets implemented over Phoenix Ch...
New
After working on it for a couple of months and using it in production for most of that time, today I’ve released LiveAdmin, a LiveView ba...
New
Bandit is an HTTP server for Plug and WebSock apps.
Bandit is written entirely in Elixir and is built atop Thousand Island. It can serve...
New
Hi, I wanted share a small library we at Revelry Labs made for rendering react components from the server side. There are instructions fo...
New
I created Kitto a framework for dashboards inspired by Dashing.
The distributed characteristics of Elixir and the low memory footprint...
New
Earmark is a pure-Elixir Markdown converter.
It is intended to be used as a library (just call Earmark.as_html), but can also be used as...
New
WebAuthnLiveComponent WebAuthnComponents
See this post about renaming the package.
Passwordless authentication for Phoenix LiveView app...
New
I released Doggo, a collection of unstyled Phoenix components.
https://github.com/woylie/doggo
Features
Unstyled Phoenix components....
New
Lexical
Lexical is a next-generation language server for the Elixir programming language.
Features
Context aware code completion
As-you...
New
Other popular topics
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode.
The solution seems to be, in a hyphena...
New
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set?
Thanks.
New
In templates/appointment/index.html.eex:
<%= for appointment <- @appointments do %>
<tr>
<td><%= appoi...
New
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
New
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








