gmile

gmile

Design puzzle when using Ecto.Multi

In my application I need to perform multiple things during transaction:

  1. call HTTP and load some data form a microservice,
  2. update some DB records,
  3. add a new record DB record,
  4. send an SMS verification code.

Ecto.Multi suites this purpose very well, but it looks like I’m facing some roadblocks with it. I need someone else’s opinion on my design.

Currently the code for process above looks like this:

# declaration_controller.ex
def create(conn, %{declaration: declaration_attrs} do
  case DeclarationAPI.create(declaration_attrs) do
    {:ok, %{declaration: declaration} = result} ->
      render(conn, "show.json", result)
    {:error, key, reason, _}
      render(conn, "error.json", %{key => reason})
  end
end

# declaration_api.ex
def create_declaration(declaration_attrs) do
  Repo.transaction(new_declaration_request(declaration_attrs))
end

def new_declaration(declaration_attrs) do
  settings = SettingsMicroserviceAPI.get()

  Multi.new
  |> Multi.update_all(:pending_declarations, pending_declarations_query(declaration_attrs), set: [status: "CANCELLED"])
  |> Multi.insert(:declaration, changeset(declaration_attrs, settings))
  |> Multi.run(:verification_code, Verification, :send_verification_code, [declaration_attrs])
end

There’s a problem with this design. Sometimes SettingsMicroserviceAPI.get() HTTP call will not return expected data: it may stumble upon 404 or even time out. In this case transaction has to be rolled back gracefully, and an error saying something along the lines SettingsMicroserviceAPI has timed out
should be returned to user who requested the initial create action.

So I thought: why don’t I add this HTTP call as a Multi step like this?:

Multi.new
|> Multi.run(:fetch_settings, fn _ -> SettingsMicroserviceAPI.get() end)
|> Multi.update_all(:pending_declarations, pending_declarations_query(declaration_attrs), set: [status: "CANCELLED"])
# ...

However in this case there’s no way I can access the settings in the changeset.

So I see two ways to go from here:

  1. Keep the microservice call where it is now, but add a validation to the Ecto.Multi chain,
  2. Push the microservice call directly to changeset.

I tried both approaches and here’s what learned.

1. Keep the microservice call where it is now, but add a validation to the Ecto.Multi chain

The microservice call will always return {:ok, data} or {:error, :data}. This suites Ecto.Multi.run requirement for function’s return value so my code becomes this:

def new_declaration(declaration_attrs, user_id) do
  settings = SettingsMicroserviceAPI.get()

  Multi.new
  |> Multi.run(:fetch_settings, fn _ -> settings end)
  |> Multi.update_all(:pending_declarations, pending_declarations_query(declaration_attrs), set: [status: "CANCELLED"])
  |> Multi.insert(:declaration, changeset(pending_declaration_attrs, settings))
  |> Multi.run(:verification_code, Verification, :send_verification_code, [declaration_attrs])
end

Now, Ecto.Multi will validate the settings: in case of {:error, _} = settings the transaction will be rolled back, and a nice error will be returned to controller.

There are two problems with this, however:

  1. I have to update the signature of changeset from this:

    def changeset(attrs, settings)
      # ...
    end
    

    to this:

    def changeset(attrs, %{:ok, settings})
      # ...
    end
    

    which looks a little ugly.

  2. In case of {:error, _} = settings, I need to add another changeset function clause, as otherwise the code will raise a no matched function error.

2. Push the microservice call directly to changeset

I can do this (the actual code was reduced for brevity):

def create_changeset(attrs) do
  %Declaration{}
  |> cast(attrs, @required_fields)
  |> fetch_settings()
end

def fetch_settings(changeset) do
  result = SettingsMicroserviceAPI.get()

  case result do
    {:ok, settings} ->
      put_change(changeset, :_settings, settings)
    {:error, reason}
      add_error(changeset, :_settings, reason)
  end
end

This works, but with two caveats:

  1. In case I need to use settings across multi steps, inside verification code step for example, I’d have to rewrite the verification step to something like this:

    # ...
    |> Multi.run(:verification_code, fn multi ->
         settings = multi.declaration.get_change(:_settings)
    
         Verification.send_verification_code(declaration_attrs, settings))
       end)
    
  2. I’m not aware of a standard mechanism for storing arbitrary data alongside the changeset, so I have to use a temporal private _settings key.
    This works right now, but there’s no guarantee changeset becomes more strict in future, to disallow fields not described in the schema?


I’m sticking to the second solution right now as it seems to work (at least on the paper;
I’m yet to implement it). But I wonder how everyone handle HTTP interaction with 3rd parties
during transactions?

Maybe this design is inherently wrong, and there’s an “official way” to deal with these kinds
of situations.

Marked As Solved

wojtekmach

wojtekmach

Hex Core Team

Since the 3rd party call does not depend on any data within Multi I’d definitely keep it out. Seems like a good use case for with which will simply propagate the error from API:

with {:ok, settings} <- settings_api.get() do
  Ecto.Multi.new
  |> Multi.update_all(...)
  |> ...
  |> Repo.transaction
end

Where Next?

Popular in Questions Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;%= ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New

Other popular topics Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30877 112
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

We're in Beta

About us Mission Statement