Nebo15 / sage

A dependency-free tool to run distributed transactions in Elixir, inspired by Sagas pattern.
MIT License
912 stars 40 forks source link

Sage 0.6 transaction called with empty `effects_so_far` after a compensation runs #52

Closed mtarnovan closed 4 years ago

mtarnovan commented 4 years ago

SAGE 0.4, after a compensation runs, the transaction is being passed effects_so_far as expected:

iex(1)> (Sage.new()
...(1)> |> Sage.run(:invoice, fn _, _ -> {:ok, "invoice"} end)
...(1)> |> Sage.run(
...(1)>   :some_step,
...(1)>   fn a, b ->
...(1)>     IO.puts "some_step called with a=#{inspect(a)} b=#{inspect(b)}"
...(1)>     if :rand.uniform(10) >= 3 do
...(1)>       IO.puts "error"
...(1)>       {:error, "some_error"}
...(1)>     else
...(1)>       IO.puts "ok"
...(1)>       {:ok, "some_result"}
...(1)>     end
...(1)>   end,
...(1)>   fn _, _, _, _ ->
...(1)>     {:retry, retry_limit: 10, base_backoff: 1, max_backoff: 1, enable_jitter: false}
...(1)>   end
...(1)> )
...(1)> |> Sage.transaction(Ypsilon.Repo))
some_step called with a=%{invoice: "invoice"} b=[]
error
some_step called with a=%{invoice: "invoice"} b=[]
error
some_step called with a=%{invoice: "invoice"} b=[]
error
some_step called with a=%{invoice: "invoice"} b=[]
error
some_step called with a=%{invoice: "invoice"} b=[]
error
some_step called with a=%{invoice: "invoice"} b=[]
ok
{:ok, "some_result", %{invoice: "invoice", some_step: "some_result"}}

SAGE 0.6, after a compensation runs, the transaction is being passed an empty effects_so_far

iex(1)> (Sage.new()
...(1)> |> Sage.run(:invoice, fn _, _ -> {:ok, "invoice"} end)
...(1)> |> Sage.run(
...(1)>   :some_step,
...(1)>   fn a, b ->
...(1)>     IO.puts "some_step called with a=#{inspect(a)} b=#{inspect(b)}"
...(1)>     if :rand.uniform(10) >= 3 do
...(1)>       IO.puts "error"
...(1)>       {:error, "some_error"}
...(1)>     else
...(1)>       IO.puts "ok"
...(1)>       {:ok, "some_result"}
...(1)>     end
...(1)>   end,
...(1)>   fn _, _, _ ->
...(1)>     {:retry, retry_limit: 10, base_backoff: 1, max_backoff: 1, enable_jitter: false}
...(1)>   end
...(1)> )
...(1)> |> Sage.transaction(Ypsilon.Repo))
some_step called with a=%{invoice: "invoice"} b=[]
error
some_step called with a=%{} b=[]
ok
{:ok, "some_result", %{some_step: "some_result"}}
AndrewDryga commented 4 years ago

Should be fixed in #53

mtarnovan commented 4 years ago

Thanks for fixing this so fast, much appreciated

AndrewDryga commented 4 years ago

That thanks should go to @aspett, ❤️